mirror of
https://github.com/wassname/talk.git
synced 2026-07-03 06:20:19 +08:00
b9a8fdb77b
Merge pull request #1731 from coralproject/next-watcher-flags Watcher --only Use JSDocs comments (#1727) Merge branch 'next' into prevent-compile-loop-relay Merge pull request #1726 from coralproject/prevent-compile-loop-relay [next] Adapt relay watch config [next] Remove nodemon (#1725) * Remove old nodemon configs * Remove nodemon [next] Jest implementation for React Components (#1733) * Make jest testing work with custom path and css modules * Add first test * feat: added unit tests to ci * fix: updated package-lock.json * Update cssTransform.js * Update cssTransform.js * Fix test in ci Adapt files.exclude (#1736) Permalink ui Adding Copy to clipboard functionality WIP clean request wip progress progress work in progress wip ui functionality Translations :/ wip Merge branch 'permalink' of github.com:coralproject/talk into permalink * 'permalink' of github.com:coralproject/talk: (42 commits) [next] Support server side jest testing (#1747) Update snapshots Add comments Remove precss Move react-responsive to dev deps Remove comment Mobile first approach Support standard css variables, dynamically set spacing-unit Add docs Fully implement Flex and MatchMedia Responsive Components <3 fix: linting fix: adjusted pageInfo Remove obsoloe snapshot Move jsdom to dev deps Mark comments as always returning a value Add comment Fix unit tests Translate, concept for translation and id strings Add aria props ... Adding Attachment and Popover Component merge conflicts progress Any Working Support for refs Ready Merge branch 'permalink' of github.com:coralproject/talk into permalink * 'permalink' of github.com:coralproject/talk: (101 commits) Ready Support for refs Working Any progress merge conflicts Make timeagoFormatter optional More colors Colors Short circuit endless respawn fix: new mongo parser Remove jest from watcher config, as it doesnt run well inside Add jest set Apply suggestions Move react-timeago to dev Upgrade docz Cleanup docz scripts Support watcher sets [next] Support server side jest testing (#1747) Make filter only a pure function ...
115 lines
2.7 KiB
TypeScript
115 lines
2.7 KiB
TypeScript
import chalk from "chalk";
|
|
import { ChildProcess } from "child_process";
|
|
import spawn from "cross-spawn";
|
|
import { Cancelable, debounce } from "lodash";
|
|
import psTree from "pstree.remy";
|
|
|
|
import { Executor } from "./types";
|
|
|
|
interface LongRunningExecutorOptions {
|
|
args?: ReadonlyArray<string>;
|
|
|
|
/** Specify the period in which the process is restarted at max once. */
|
|
debounce?: number;
|
|
}
|
|
|
|
export default class LongRunningExecutor implements Executor {
|
|
private cmd: string;
|
|
private args?: ReadonlyArray<string>;
|
|
private process: ChildProcess | null = null;
|
|
private isRunning: boolean = false;
|
|
private shouldRestart: boolean = false;
|
|
private restartDebounced: (() => void) & Cancelable;
|
|
|
|
constructor(cmd: string, opts: LongRunningExecutorOptions = {}) {
|
|
this.cmd = cmd;
|
|
this.args = opts.args;
|
|
this.restartDebounced = debounce(
|
|
() => this.restart(),
|
|
opts.debounce || 500
|
|
);
|
|
}
|
|
|
|
private spawnProcess() {
|
|
this.isRunning = true;
|
|
this.process = spawn(this.cmd, this.args as string[], {
|
|
stdio: "inherit",
|
|
shell: !this.args,
|
|
});
|
|
|
|
this.process!.on("exit", (code: number) => {
|
|
this.isRunning = false;
|
|
|
|
if (code !== 0 && code !== null) {
|
|
// tslint:disable-next-line: no-console
|
|
console.log(chalk.red(`Command exited with ${code}`));
|
|
return;
|
|
}
|
|
if (this.shouldRestart) {
|
|
this.shouldRestart = false;
|
|
this.spawnProcess();
|
|
}
|
|
});
|
|
}
|
|
|
|
private restart() {
|
|
this.shouldRestart = true;
|
|
return this.internalKill();
|
|
}
|
|
|
|
private kill() {
|
|
this.shouldRestart = false;
|
|
return this.internalKill();
|
|
}
|
|
|
|
private async internalKill(): Promise<void> {
|
|
return new Promise<void>((resolve, reject) => {
|
|
const signal = "SIGTERM";
|
|
if (process.platform === "win32") {
|
|
// Force kill (/F) the whole child tree (/T) by PID
|
|
spawn.sync("taskkill", [
|
|
"/pid",
|
|
this.process!.pid.toString(),
|
|
"/T",
|
|
"/F",
|
|
]);
|
|
resolve();
|
|
return;
|
|
}
|
|
|
|
psTree(this.process!.pid, (err, kids) => {
|
|
if (err) {
|
|
reject(err);
|
|
}
|
|
spawn.sync("kill", [
|
|
`-${signal}`,
|
|
this.process!.pid.toString(),
|
|
...kids.map(p => p.PID.toString()),
|
|
]);
|
|
resolve();
|
|
});
|
|
});
|
|
}
|
|
|
|
// This is called before watching starts.
|
|
public onInit(): void {
|
|
this.spawnProcess();
|
|
}
|
|
|
|
// This is called before exiting.
|
|
public async onCleanup() {
|
|
this.restartDebounced.cancel();
|
|
if (this.isRunning) {
|
|
await this.kill();
|
|
}
|
|
}
|
|
|
|
public execute(filePath: string) {
|
|
if (this.isRunning) {
|
|
this.restartDebounced();
|
|
return;
|
|
}
|
|
this.spawnProcess();
|
|
}
|
|
}
|