refactor(cli): Update watching (#716)

- For now hardcode ignore JetBrain's "safe file changes".
- Show events for new files and directories
- Update spec on new files
This commit is contained in:
Melvyn Sopacua 2019-09-30 09:51:12 +02:00 committed by Roman Hotsiy
parent 0360dcee5a
commit 8632b193b2

View File

@ -115,8 +115,8 @@ YargsParser.command(
yargs.demandOption('spec'); yargs.demandOption('spec');
return yargs; return yargs;
}, },
async argv => { async (argv: any) => {
const config: Options = { const config = {
ssr: true, ssr: true,
output: argv.o as string, output: argv.o as string,
cdn: argv.cdn as boolean, cdn: argv.cdn as boolean,
@ -188,14 +188,14 @@ async function serve(port: number, pathToSpec: string, options: Options = {}) {
if (options.watch && existsSync(pathToSpec)) { if (options.watch && existsSync(pathToSpec)) {
const pathToSpecDirectory = resolve(dirname(pathToSpec)); const pathToSpecDirectory = resolve(dirname(pathToSpec));
const watchOptions = { const watchOptions = {
ignored: /(^|[\/\\])\../, ignored: [/(^|[\/\\])\../, /___jb_[a-z]+___$/],
ignoreInitial: true,
}; };
const watcher = watch(pathToSpecDirectory, watchOptions); const watcher = watch(pathToSpecDirectory, watchOptions);
const log = console.log.bind(console); const log = console.log.bind(console);
watcher
.on('change', async path => { const handlePath = async path => {
log(`${path} 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);
@ -203,6 +203,19 @@ async function serve(port: number, pathToSpec: string, options: Options = {}) {
} catch (e) { } catch (e) {
console.error('Error while updating: ', e.message); console.error('Error while updating: ', e.message);
} }
};
watcher
.on('change', async path => {
log(`${path} changed, updating docs`);
handlePath(path);
})
.on('add', async path => {
log(`File ${path} added, updating docs`);
handlePath(path);
})
.on('addDir', path => {
log(`↗ Directory ${path} added. Files in here will trigger reload.`);
}) })
.on('error', error => console.error(`Watcher error: ${error}`)) .on('error', error => console.error(`Watcher error: ${error}`))
.on('ready', () => log(`👀 Watching ${pathToSpecDirectory} for changes...`)); .on('ready', () => log(`👀 Watching ${pathToSpecDirectory} for changes...`));