diff --git a/scripts/watcher/ChokidarWatcher.ts b/scripts/watcher/ChokidarWatcher.ts index 700eeba45..65a6ad701 100644 --- a/scripts/watcher/ChokidarWatcher.ts +++ b/scripts/watcher/ChokidarWatcher.ts @@ -17,9 +17,6 @@ export default class ChokidarWatcher implements Watcher { options: WatchOptions = {} ): AsyncIterable { const resolvedPaths = prependRootDir(rootDir, paths); - const client = chokidar.watch(resolvedPaths, { - ignored: options.ignore && prependRootDir(rootDir, options.ignore), - }); // An array to hold all changes, that has not yet been yield. const queue: string[] = []; @@ -30,31 +27,38 @@ export default class ChokidarWatcher implements Watcher { | ({ resolve: (result: string) => void; reject: (error: Error) => void }) | null = null; - // Listen for errors - client.on("error", (error: Error) => { - // Resolve pending request. - if (pending) { - pending.reject(error); - pending = null; - return; - } - if (!firstError) { - firstError = error; - } - }); + // Only start client if we have something to watch. + if (paths.length) { + const client = chokidar.watch(resolvedPaths, { + ignored: options.ignore && prependRootDir(rootDir, options.ignore), + }); - // Listen for changes - client.on("change", (pathFile: string) => { - // Resolve pending request. - if (pending) { - pending.resolve(pathFile); - pending = null; - return; - } + // Listen for errors + client.on("error", (error: Error) => { + // Resolve pending request. + if (pending) { + pending.reject(error); + pending = null; + return; + } + if (!firstError) { + firstError = error; + } + }); - // There is no pending request, save it into the queue. - queue.unshift(pathFile); - }); + // Listen for changes + client.on("change", (pathFile: string) => { + // Resolve pending request. + if (pending) { + pending.resolve(pathFile); + pending = null; + return; + } + + // There is no pending request, save it into the queue. + queue.unshift(pathFile); + }); + } return { [Symbol.asyncIterator]() { return { diff --git a/scripts/watcher/SaneWatcher.ts b/scripts/watcher/SaneWatcher.ts index 299736841..5c729ae93 100644 --- a/scripts/watcher/SaneWatcher.ts +++ b/scripts/watcher/SaneWatcher.ts @@ -43,32 +43,35 @@ export default class SaneWatcher implements Watcher { paths: ReadonlyArray, options: WatchOptions = {} ): AsyncIterable { - // Setup client - const client = sane(rootDir, { - glob: paths as string[], - ignored: options.ignore as string[], - watchman: this.watchman, - poll: this.poll, - }); - // An array to hold all changes, that has not yet been yield. const queue: string[] = []; // If this is set, a pending promise is waiting for the next result. let pending: ({ resolve: (result: string) => void }) | null = null; - // Listen for changes - client.on("change", (pathFile: string) => { - // Resolve pending request. - if (pending) { - pending.resolve(pathFile); - pending = null; - return; - } + // Only start client if we have something to watch. + if (paths.length) { + // Setup client + const client = sane(rootDir, { + glob: paths as string[], + ignored: options.ignore as string[], + watchman: this.watchman, + poll: this.poll, + }); - // There is no pending request, save it into the queue. - queue.unshift(pathFile); - }); + // Listen for changes + client.on("change", (pathFile: string) => { + // Resolve pending request. + if (pending) { + pending.resolve(pathFile); + pending = null; + return; + } + + // There is no pending request, save it into the queue. + queue.unshift(pathFile); + }); + } return { [Symbol.asyncIterator]() { diff --git a/scripts/watcher/watch.ts b/scripts/watcher/watch.ts index 8adfbfcb8..6777b7fd3 100644 --- a/scripts/watcher/watch.ts +++ b/scripts/watcher/watch.ts @@ -28,14 +28,16 @@ async function beginWatch( function setupCleanup(watcher: Watcher, config: Config) { ["SIGINT", "SIGTERM"].forEach(signal => process.once(signal as any, async () => { + const cleanups = []; if (watcher.onCleanup) { - await watcher.onCleanup(); + cleanups.push(watcher.onCleanup()); } for (const key of Object.keys(config.watchers)) { if (config.watchers[key].executor.onCleanup) { - await config.watchers[key].executor.onCleanup!(); + cleanups.push(config.watchers[key].executor.onCleanup!()); } } + await Promise.all(cleanups); process.exit(0); }) );