-
-
Notifications
You must be signed in to change notification settings - Fork 123
/
index.ts
157 lines (139 loc) · 3.75 KB
/
index.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
import type { ChildProcess } from 'child_process';
import { fileURLToPath } from 'url';
import { constants as osConstants } from 'os';
import path from 'path';
import { command } from 'cleye';
import { watch } from 'chokidar';
import { run } from '../run';
import { removeArgvFlags } from '../remove-argv-flags';
import {
clearScreen,
debounce,
isDependencyPath,
log,
} from './utils';
const flags = {
noCache: {
type: Boolean,
description: 'Disable caching',
default: false,
},
tsconfig: {
type: String,
description: 'Custom tsconfig.json path',
},
clearScreen: {
type: Boolean,
description: 'Clearing the screen on rerun',
default: true,
},
ignore: {
type: [String],
description: 'Paths & globs to exclude from being watched',
},
} as const;
export const watchCommand = command({
name: 'watch',
parameters: ['<script path>'],
flags,
help: {
description: 'Run the script and watch for changes',
},
}, (argv) => {
const rawArgvs = removeArgvFlags(flags, process.argv.slice(3));
const options = {
noCache: argv.flags.noCache,
tsconfigPath: argv.flags.tsconfig,
clearScreen: argv.flags.clearScreen,
ignore: argv.flags.ignore,
ipc: true,
};
let runProcess: ChildProcess | undefined;
const reRun = debounce(() => {
if (
runProcess
&& (!runProcess.killed && runProcess.exitCode === null)
) {
runProcess.kill();
}
// Not first run
if (runProcess) {
log('rerunning');
}
if (options.clearScreen) {
process.stdout.write(clearScreen);
}
runProcess = run(rawArgvs, options);
runProcess.on('message', (data) => {
// Collect run-time dependencies to watch
if (isDependencyPath(data)) {
const dependencyPath = (
data.path.startsWith('file:')
? fileURLToPath(data.path)
: data.path
);
if (path.isAbsolute(dependencyPath)) {
// console.log('adding', dependencyPath);
watcher.add(dependencyPath);
}
}
});
}, 100);
reRun();
function exit(signal: NodeJS.Signals) {
/**
* In CLI mode where there is only one run, we can inherit the child's exit code.
* But in watch mode, the exit code should reflect the kill signal.
*/
// eslint-disable-next-line unicorn/no-process-exit
process.exit(
/**
* https://nodejs.org/api/process.html#exit-codes
* >128 Signal Exits: If Node.js receives a fatal signal such as SIGKILL or SIGHUP,
* then its exit code will be 128 plus the value of the signal code. This is a
* standard POSIX practice, since exit codes are defined to be 7-bit integers, and
* signal exits set the high-order bit, and then contain the value of the signal
* code. For example, signal SIGABRT has value 6, so the expected exit code will be
* 128 + 6, or 134.
*/
128 + osConstants.signals[signal],
);
}
function relaySignal(signal: NodeJS.Signals) {
// Child is still running
if (runProcess && runProcess.exitCode === null) {
// Wait for child to exit
runProcess.on('close', () => exit(signal));
runProcess.kill(signal);
} else {
exit(signal);
}
}
process.once('SIGINT', relaySignal);
process.once('SIGTERM', relaySignal);
/**
* Ideally, we can get a list of files loaded from the run above
* and only watch those files, but it's not possible to detect
* the full dependency-tree at run-time because they can be hidden
* in a if-condition/async-delay.
*
* As an alternative, we watch cwd and all run-time dependencies
*/
const watcher = watch(
argv._,
{
cwd: process.cwd(),
ignoreInitial: true,
ignored: [
// Hidden directories like .git
'**/.*/**',
// 3rd party packages
'**/{node_modules,bower_components,vendor}/**',
...options.ignore,
],
ignorePermissionErrors: true,
},
).on('all', reRun);
// On "Return" key
process.stdin.on('data', reRun);
});