/
index.ts
140 lines (122 loc) · 2.84 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
import type { ChildProcess } from 'child_process';
import { fileURLToPath } from 'url';
import path from 'path';
import { command } from 'cleye';
import typeFlag from 'type-flag';
import { watch } from 'chokidar';
import { run } from '../run';
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 args = typeFlag(
flags,
process.argv.slice(3),
{ ignoreUnknown: true },
)._;
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(args, 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();
// Kill process on CTRL+C
function exit(exitCode: number) {
if (runProcess) {
runProcess.kill();
}
// eslint-disable-next-line unicorn/no-process-exit
process.exit(exitCode);
}
process.once('SIGINT', () => exit(130));
process.once('SIGTERM', () => exit(143));
/**
* 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._,
{
ignoreInitial: true,
ignored: [
// Hidden directories like .git
'**/.*/**',
// 3rd party packages
'**/{node_modules,bower_components,vendor}/**',
// Distribution files
'**/dist/**',
...options.ignore,
],
ignorePermissionErrors: true,
},
).on('all', reRun);
// On "Return" key
process.stdin.on('data', reRun);
});