refactor(cli): Update watching:

- For now hardcode ignore JetBrain's "safe file changes".
- Show events for new files and directories
- Update spec on new files

Considering:
- Put the ignore in package.json as a config value or a separate
  config file that fills watchOptions.
This commit is contained in:
Melvyn Sopacua 2018-11-16 13:37:57 +01:00
parent 4691d78dc0
commit ff8a7f7218

View File

@ -170,7 +170,8 @@ 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);
@ -184,7 +185,21 @@ async function serve(port: number, pathToSpec: string, options: Options = {}) {
log('Updated successfully'); log('Updated successfully');
} catch (e) { } catch (e) {
console.error('Error while updating: ', e.message); console.error('Error while updating: ', e.message);
}}) }
})
.on('add', async path => {
log(`File ${path} added, updating docs`);
try {
spec = await loadAndBundleSpec(pathToSpec);
pageHTML = await getPageHTML(spec, pathToSpec, options);
log('Updated successfully');
} catch (e) {
console.error('Error while updating: ', e.message);
}
})
.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...`));
} }