From ff8a7f7218363cf2b226e50d368ba8e969af35d1 Mon Sep 17 00:00:00 2001 From: Melvyn Sopacua Date: Fri, 16 Nov 2018 13:37:57 +0100 Subject: [PATCH] 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. --- cli/index.ts | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/cli/index.ts b/cli/index.ts index d3609499..3d8ccd97 100644 --- a/cli/index.ts +++ b/cli/index.ts @@ -170,7 +170,8 @@ 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); @@ -184,7 +185,21 @@ async function serve(port: number, pathToSpec: string, options: Options = {}) { log('Updated successfully'); } catch (e) { 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('ready', () => log(`👀 Watching ${pathToSpecDirectory} for changes...`)); }