-
Notifications
You must be signed in to change notification settings - Fork 0
/
watcher.ts
56 lines (46 loc) · 1.35 KB
/
watcher.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import chalk from 'chalk';
import { watch } from 'chokidar';
import { $, ExecaError } from 'execa';
import { globSync } from 'node:fs';
const $$ = $({
stdout: process.stdout,
stderr: process.stderr,
env: { FORCE_COLOR: '1' },
verbose: 'short'
});
const apps = [
{ name: 'eslint-server', cwd: 'packages/eslint-server', cmd: 'out/index.js' },
] as const;
function handleError(error: ExecaError) {
if ('originalMessage' in error && error.originalMessage === 'RELOAD') {
return;
}
process.exit(1);
}
/**
* AbortController can't be reused to restart a process,
* so a new one has to be created each time.
*/
function runApps() {
const controller = new AbortController();
for (const app of apps) {
console.log(chalk.bgBlue(` Starting ${app.name} `));
$$({ cancelSignal: controller.signal, cwd: app.cwd })`node --trace-uncaught ${app.cmd}`.catch(handleError)
}
return controller;
}
async function rebuild() {
await $$`pnpm exec nx run-many -t build`;
console.log(chalk.bgGreen(" Rebuild complete "));
}
await rebuild();
try {
let controller = runApps();
watch(globSync('packages/**/src/**/*.{ts,tsx,css,js}')).on("change", async (path) => {
await rebuild();
controller.abort('RELOAD');
controller = runApps();
})
} catch (error) {
console.error(error);
}