Files
talk/scripts/watcher/CommandExecutor.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

82 lines
2.1 KiB
TypeScript

import spawn from "cross-spawn";
import { Cancelable, debounce } from "lodash";
import { Executor } from "./types";
interface CommandExecutorOptions {
args?: ReadonlyArray<string>;
// If true, allow spawning multiple processes.
spawnMutiple?: boolean;
// Specify the period in which the process is started at max once.
debounce?: number | false;
// If true, will run command upon initialization.
runOnInit?: boolean;
}
export default class CommandExecutor implements Executor {
private cmd: string;
private args?: ReadonlyArray<string>;
private spawnMultiple: boolean;
private runOnInit: boolean;
private isRunning: boolean = false;
private shouldRespawn: boolean = false;
private spawnProcessDebounced?: (() => void) & Cancelable;
constructor(cmd: string, opts: CommandExecutorOptions = {}) {
this.cmd = cmd;
this.args = opts.args;
this.spawnMultiple = opts.spawnMutiple || false;
this.runOnInit = opts.runOnInit || false;
const wait = opts.debounce === undefined ? 500 : opts.debounce;
if (wait) {
this.spawnProcessDebounced = debounce(() => this.spawnProcess(), wait);
}
}
// This is called before watching starts.
public onInit(): void {
if (this.runOnInit) {
this.spawnProcessPotentiallyDebounced();
}
}
private spawnProcessPotentiallyDebounced() {
if (this.spawnProcessDebounced) {
this.spawnProcessDebounced();
return;
}
this.spawnProcess();
}
private spawnProcess() {
if (this.isRunning && !this.spawnMultiple) {
this.shouldRespawn = true;
return;
}
this.isRunning = true;
this.shouldRespawn = false;
const child = spawn(this.cmd, this.args as string[], {
stdio: "inherit",
shell: !this.args,
});
child.on("close", (code: number) => {
this.isRunning = false;
if (code !== 0) {
// tslint:disable-next-line: no-console
console.log(`We had an error building ${code}`);
}
if (this.shouldRespawn) {
this.spawnProcessPotentiallyDebounced();
}
});
}
public execute(filePath: string) {
this.spawnProcessPotentiallyDebounced();
}
}