Skip to content

Commit

Permalink
refactor(cli): Update watching (#716)
Browse files Browse the repository at this point in the history
- For now hardcode ignore JetBrain's "safe file changes".
- Show events for new files and directories
- Update spec on new files
  • Loading branch information
Melvyn Sopacua authored and RomanHotsiy committed Sep 30, 2019
1 parent 0360dce commit 8632b19
Showing 1 changed file with 23 additions and 10 deletions.
33 changes: 23 additions & 10 deletions cli/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,8 @@ YargsParser.command(
yargs.demandOption('spec');
return yargs;
},
async argv => {
const config: Options = {
async (argv: any) => {
const config = {
ssr: true,
output: argv.o as string,
cdn: argv.cdn as boolean,
Expand Down Expand Up @@ -188,21 +188,34 @@ async function serve(port: number, pathToSpec: string, options: Options = {}) {
if (options.watch && existsSync(pathToSpec)) {
const pathToSpecDirectory = resolve(dirname(pathToSpec));
const watchOptions = {
ignored: /(^|[\/\\])\../,
ignored: [/(^|[\/\\])\../, /___jb_[a-z]+___$/],
ignoreInitial: true,
};

const watcher = watch(pathToSpecDirectory, watchOptions);
const log = console.log.bind(console);

const handlePath = async path => {
try {
spec = await loadAndBundleSpec(pathToSpec);
pageHTML = await getPageHTML(spec, pathToSpec, options);
log('Updated successfully');
} catch (e) {
console.error('Error while updating: ', e.message);
}
};

watcher
.on('change', async path => {
log(`${path} changed, 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);
}
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('ready', () => log(`👀 Watching ${pathToSpecDirectory} for changes...`));
Expand Down

0 comments on commit 8632b19

Please sign in to comment.