few corrections:

This commit is contained in:
Kamil Tunkiewicz 2020-10-16 19:05:12 +02:00
parent 91bd11bccb
commit 9a5a4774d4

View File

@ -76,6 +76,7 @@ YargsParser.command(
yargs.option('static', { yargs.option('static', {
type: 'string', type: 'string',
describe: 'Add folder to be served statically'
}); });
yargs.demandOption('spec'); yargs.demandOption('spec');
@ -95,7 +96,7 @@ YargsParser.command(
console.log(config); console.log(config);
try { try {
await serve(argv.port as number, argv.spec as string, argv.static as string, config); await serve(argv.port as number, argv.spec as string, config);
} catch (e) { } catch (e) {
handleError(e); handleError(e);
} }
@ -169,7 +170,7 @@ YargsParser.command(
describe: 'ReDoc options, use dot notation, e.g. options.nativeScrollbars', describe: 'ReDoc options, use dot notation, e.g. options.nativeScrollbars',
}).argv; }).argv;
async function serve(port: number, pathToSpec: string, staticFolder: string, options: Options = {}) { async function serve(port: number, pathToSpec: string, options: Options = {}) {
let spec = await loadAndBundleSpec(pathToSpec); let spec = await loadAndBundleSpec(pathToSpec);
let pageHTML = await getPageHTML(spec, pathToSpec, options); let pageHTML = await getPageHTML(spec, pathToSpec, options);
@ -201,10 +202,11 @@ async function serve(port: number, pathToSpec: string, staticFolder: string, op
'Content-Type': 'application/json', 'Content-Type': 'application/json',
}); });
} else { } else {
if (staticFolder !== '' && request.url.split('/').shift() === staticFolder) { if (options.static && options.static !== '' && request.url?.startsWith('/' + options.static)) {
const filePath = join(dirname(pathToSpec), request.url); const filePath = join(dirname(pathToSpec), request.url);
const fileExists = existsSync(filePath); const fileExists = existsSync(filePath);
if (fileExists) { if (fileExists) {
respondWithGzip( respondWithGzip(
createReadStream(filePath, 'utf8'), createReadStream(filePath, 'utf8'),
request, request,