Watch the directory that contains the specified spec instead of the spec itself

This commit is contained in:
Steffen Kreutz 2018-05-30 08:41:39 +02:00
parent 8e7f27b16d
commit 786ab60ecc

View File

@ -108,7 +108,7 @@ YargsParser.command(
redocOptions: argv.options || {}, redocOptions: argv.options || {},
}); });
}, },
) )
.demandCommand() .demandCommand()
.options('t', { .options('t', {
alias: 'template', alias: 'template',
@ -159,11 +159,17 @@ async function serve(port: number, pathToSpec: string, options: Options = {}) {
server.listen(port, () => console.log(`Server started: http://127.0.0.1:${port}`)); server.listen(port, () => console.log(`Server started: http://127.0.0.1:${port}`));
if (options.watch && existsSync(pathToSpec)) { if (options.watch && existsSync(pathToSpec)) {
const pathToSpecDirectory = dirname(pathToSpec);
const watchOptions = {
recursive: true
};
watch( watch(
pathToSpec, pathToSpecDirectory,
watchOptions,
debounce(async (event, filename) => { debounce(async (event, filename) => {
if (event === 'change' || (event === 'rename' && existsSync(filename))) { if (event === 'change' || event === 'rename') {
console.log(`${pathToSpec} changed, updating docs`); console.log(`${join(pathToSpecDirectory, filename)} changed, updating docs`);
try { try {
spec = await loadAndBundleSpec(pathToSpec); spec = await loadAndBundleSpec(pathToSpec);
pageHTML = await getPageHTML(spec, pathToSpec, options); pageHTML = await getPageHTML(spec, pathToSpec, options);
@ -174,7 +180,7 @@ async function serve(port: number, pathToSpec: string, options: Options = {}) {
} }
}, 2200), }, 2200),
); );
console.log(`👀 Watching ${pathToSpec} for changes...`); console.log(`👀 Watching ${pathToSpecDirectory} for changes...`);
} }
} }
@ -229,13 +235,13 @@ async function getPageHTML(
ssr ssr
? 'hydrate(__redoc_state, container);' ? 'hydrate(__redoc_state, container);'
: `init("spec.json", ${JSON.stringify(redocOptions)}, container)` : `init("spec.json", ${JSON.stringify(redocOptions)}, container)`
}; };
</script>`, </script>`,
redocHead: ssr redocHead: ssr
? (cdn ? (cdn
? '<script src="https://unpkg.com/redoc@next/bundles/redoc.standalone.js"></script>' ? '<script src="https://unpkg.com/redoc@next/bundles/redoc.standalone.js"></script>'
: `<script>${redocStandaloneSrc}</script>`) + css : `<script>${redocStandaloneSrc}</script>`) + css
: '<script src="redoc.standalone.js"></script>', : '<script src="redoc.standalone.js"></script>',
title, title,
}); });