fix: do not add a config folder to api path (#1187)

This commit is contained in:
Ihor Karpiuk
2023-07-26 14:37:07 +03:00
committed by GitHub
parent 0976fbabab
commit 2e02eda23b
11 changed files with 113 additions and 10 deletions

View File

@@ -0,0 +1,23 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf8" />
<title>{{title}}</title>
<!-- needed for adaptive design -->
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
body {
padding: 0;
margin: 0;
}
</style>
{{{redocHead}}}
{{#unless disableGoogleFont}}<link href="https://fonts.googleapis.com/css?family=Montserrat:300,400,700|Roboto:300,400,700" rel="stylesheet">{{/unless}}
</head>
<body>
{{{redocHTML}}}
</body>
</html>

View File

@@ -0,0 +1,4 @@
type: object
properties:
message:
type: string

View File

@@ -0,0 +1,19 @@
openapi: 3.1.0
servers:
- url: http://localhost
info:
title: Sample API
version: 1.0.0
paths:
/hello:
get:
operationId: getMessage
security: []
summary: Get a greeting message
responses:
200:
description: OK
content:
application/json:
schema:
$ref: ./message-schema.yaml

View File

@@ -0,0 +1,5 @@
extends:
- minimal
theme:
openapi:
htmlTemplate: ../index.hbs

View File

@@ -0,0 +1,10 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`E2E build-docs build docs with config option 1`] = `
🎉 bundled successfully in: nested/redoc-static.html (38 KiB) [⏱ <test>ms].
Found nested/redocly.yaml and using theme.openapi options
Prerendering docs
`;

View File

@@ -0,0 +1 @@
../../../resources/pets.yaml

View File

@@ -0,0 +1,10 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`E2E build-docs simple build-docs 1`] = `
🎉 bundled successfully in: redoc-static.html (318 KiB) [⏱ <test>ms].
Found undefined and using theme.openapi options
Prerendering docs
`;

View File

@@ -3,6 +3,7 @@ import { join, relative } from 'path';
//@ts-ignore //@ts-ignore
import { toMatchSpecificSnapshot } from './specific-snapshot'; import { toMatchSpecificSnapshot } from './specific-snapshot';
import { getCommandOutput, getEntrypoints, callSerializer, getParams } from './helpers'; import { getCommandOutput, getEntrypoints, callSerializer, getParams } from './helpers';
import * as fs from 'fs';
expect.extend({ expect.extend({
toMatchExtendedSpecificSnapshot(received, snapshotFile) { toMatchExtendedSpecificSnapshot(received, snapshotFile) {
@@ -372,4 +373,31 @@ describe('E2E', () => {
(<any>expect(result)).toMatchSpecificSnapshot(join(testPath, 'snapshot.js')); (<any>expect(result)).toMatchSpecificSnapshot(join(testPath, 'snapshot.js'));
}); });
}); });
describe('build-docs', () => {
const folderPath = join(__dirname, 'build-docs');
test('simple build-docs', () => {
const testPath = join(folderPath, 'simple-build-docs');
const args = getParams('../../../packages/cli/src/index.ts', 'build-docs', ['pets.yaml']);
const result = getCommandOutput(args, testPath);
(<any>expect(result)).toMatchSpecificSnapshot(join(testPath, 'snapshot.js'));
expect(fs.existsSync(join(testPath, 'redoc-static.html'))).toEqual(true);
});
test('build docs with config option', () => {
const testPath = join(folderPath, 'build-docs-with-config-option');
const args = getParams('../../../packages/cli/src/index.ts', 'build-docs', [
'nested/openapi.yaml',
'--config=nested/redocly.yaml',
'-o=nested/redoc-static.html',
]);
const result = getCommandOutput(args, testPath);
(<any>expect(result)).toMatchSpecificSnapshot(join(testPath, 'snapshot.js'));
expect(fs.existsSync(join(testPath, 'nested/redoc-static.html'))).toEqual(true);
expect(fs.statSync(join(testPath, 'nested/redoc-static.html')).size).toEqual(38839);
});
});
}); });

View File

@@ -14,7 +14,8 @@ type CLICommands =
| 'preview-docs' | 'preview-docs'
| 'push' | 'push'
| 'split' | 'split'
| 'stats'; | 'stats'
| 'build-docs';
export function getParams( export function getParams(
indexEntryPoint: string, indexEntryPoint: string,

View File

@@ -30,12 +30,13 @@ export const handlerBuildCommand = async (argv: BuildDocsArgv, configFromFile: C
try { try {
const elapsed = getExecutionTime(startedAt); const elapsed = getExecutionTime(startedAt);
const api = await loadAndBundleSpec( const api = await loadAndBundleSpec(isAbsoluteUrl(pathToApi) ? pathToApi : resolve(pathToApi));
isAbsoluteUrl(pathToApi) const pageHTML = await getPageHTML(
? pathToApi api,
: resolve(argv.config ? dirname(argv.config) : '', pathToApi) pathToApi,
{ ...options, redocCurrentVersion },
argv.config
); );
const pageHTML = await getPageHTML(api, pathToApi, { ...options, redocCurrentVersion });
mkdirSync(dirname(options.output), { recursive: true }); mkdirSync(dirname(options.output), { recursive: true });
writeFileSync(options.output, pageHTML); writeFileSync(options.output, pageHTML);

View File

@@ -5,7 +5,7 @@ import { Config, isAbsoluteUrl } from '@redocly/openapi-core';
import { renderToString } from 'react-dom/server'; import { renderToString } from 'react-dom/server';
import { ServerStyleSheet } from 'styled-components'; import { ServerStyleSheet } from 'styled-components';
import { compile } from 'handlebars'; import { compile } from 'handlebars';
import { join } from 'path'; import { dirname, join, resolve } from 'path';
import { existsSync, lstatSync, readFileSync } from 'fs'; import { existsSync, lstatSync, readFileSync } from 'fs';
import type { BuildDocsOptions } from './types'; import type { BuildDocsOptions } from './types';
@@ -57,9 +57,10 @@ export async function getPageHTML(
templateOptions, templateOptions,
redocOptions = {}, redocOptions = {},
redocCurrentVersion, redocCurrentVersion,
}: BuildDocsOptions }: BuildDocsOptions,
configPath?: string
) { ) {
process.stderr.write('Prerendering docs'); process.stderr.write('Prerendering docs\n');
const apiUrl = redocOptions.specUrl || (isAbsoluteUrl(pathToApi) ? pathToApi : undefined); const apiUrl = redocOptions.specUrl || (isAbsoluteUrl(pathToApi) ? pathToApi : undefined);
const store = await createStore(api, apiUrl, redocOptions); const store = await createStore(api, apiUrl, redocOptions);
@@ -72,7 +73,7 @@ export async function getPageHTML(
templateFileName = templateFileName templateFileName = templateFileName
? templateFileName ? templateFileName
: redocOptions?.htmlTemplate : redocOptions?.htmlTemplate
? (redocOptions.htmlTemplate as string) ? resolve(configPath ? dirname(configPath) : '', redocOptions.htmlTemplate)
: join(__dirname, './template.hbs'); : join(__dirname, './template.hbs');
const template = compile(readFileSync(templateFileName).toString()); const template = compile(readFileSync(templateFileName).toString());
return template({ return template({