From e76740c31f747a3cb32d91497c5c984b172145ee Mon Sep 17 00:00:00 2001 From: Chi Vinh Le Date: Fri, 13 Jul 2018 11:37:56 -0300 Subject: [PATCH] Make filter only a pure function --- scripts/watcher/watch.ts | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/scripts/watcher/watch.ts b/scripts/watcher/watch.ts index 6777b7fd3..d7a477b8e 100644 --- a/scripts/watcher/watch.ts +++ b/scripts/watcher/watch.ts @@ -1,4 +1,5 @@ import Joi from "joi"; +import { pickBy } from "lodash"; import SaneWatcher from "./SaneWatcher"; import { Config, configSchema, Options, WatchConfig, Watcher } from "./types"; @@ -43,31 +44,36 @@ function setupCleanup(watcher: Watcher, config: Config) { ); } -function filterOnly(config: Config, only: string[]) { - for (const key of Object.keys(config.watchers)) { +function filterOnly( + watchers: Config["watchers"], + only: string[] +): Config["watchers"] { + return pickBy(watchers, (value, key) => { if (only.indexOf(key) === -1) { // tslint:disable-next-line:no-console console.log(`Disabled watcher "${key}"`); - delete config.watchers[key]; + return false; } - } + return true; + }) as Config["watchers"]; } export default async function watch(config: Config, options?: Options) { Joi.assert(config, configSchema); const watcher: Watcher = config.backend || new SaneWatcher(); const rootDir = config.rootDir || process.cwd(); + let watchersConfigs = config.watchers; if (options && options.only && options.only.length > 0) { - filterOnly(config, options.only); + watchersConfigs = filterOnly(watchersConfigs, options.only); } setupCleanup(watcher, config); if (watcher.onInit) { await watcher.onInit(); } - for (const key of Object.keys(config.watchers)) { + for (const key of Object.keys(watchersConfigs)) { // tslint:disable-next-line:no-console console.log(`Start watcher "${key}"`); - const watcherConfig = config.watchers[key]; + const watcherConfig = watchersConfigs[key]; beginWatch(watcher, key, watcherConfig, rootDir); } }