Files
talk/scripts/watcher/watch.ts
T
Kiwi 044e1c2863 Watcher infrastructure (#1724)
* wip

* Adding chokidar and types

* specifiying build tasks

* new structure, new types, executor and watchers

* Adding log

* Fully implemented watchers

* adapt vscode launc

* Add .babelrc.js to toplevel tsconfig project

* Typo

* Get schema path from .graphqlconfig

* Use watcher binary

* Add joi validation to watcher

* Remove fb-watchman for now

* Use correct ignore path

* Fix dist folder

* Allow setting watcher

* Per default only spawn one process at a time

* Support runOnInit

* Rename RestartingExecutor to LongRunningExecutor

* Use debounce instead of throttle

* Remove console log

* Debounce command execution

* Simplify debounce

* Watcher name change

* Typos

* Rename "watcher" root level config to "backend"
2018-07-03 12:21:58 -06:00

59 lines
1.7 KiB
TypeScript

import Joi from "joi";
import path from "path";
import ChokidarWatcher from "./ChokidarWatcher";
import { Config, configSchema, WatchConfig, Watcher } from "./types";
// Polyfill the asyncIterator symbol.
if (Symbol.asyncIterator === undefined) {
(Symbol as any).asyncIterator = Symbol.for("asyncIterator");
}
async function beginWatch(watcher: Watcher, key: string, config: WatchConfig) {
const { paths, ignore, executor } = config;
if (executor.onInit) {
executor.onInit();
}
for await (const filePath of watcher.watch(paths, { ignore })) {
// tslint:disable-next-line:no-console
console.log(`Execute "${key}"`);
executor.execute(filePath);
}
}
function prependRootDir(prepend: string, cfg: WatchConfig): WatchConfig {
const prependFunc = (p: string) => path.resolve(prepend, p);
return {
...cfg,
paths: cfg.paths.map(prependFunc),
ignore: cfg.ignore ? cfg.ignore.map(prependFunc) : undefined,
};
}
function setupCleanup(config: Config) {
["SIGINT", "SIGTERM"].forEach(signal =>
process.once(signal as any, () => {
for (const key of Object.keys(config.watchers)) {
if (config.watchers[key].executor.onCleanup) {
config.watchers[key].executor.onCleanup!();
}
}
process.exit(0);
})
);
}
export default async function watch(config: Config) {
Joi.assert(config, configSchema);
const watcher = config.backend || new ChokidarWatcher();
setupCleanup(config);
for (const key of Object.keys(config.watchers)) {
// tslint:disable-next-line:no-console
console.log(`Start watcher "${key}"`);
const watcherConfig = config.rootDir
? prependRootDir(config.rootDir, config.watchers[key])
: config.watchers[key];
beginWatch(watcher, key, watcherConfig);
}
}