-
Notifications
You must be signed in to change notification settings - Fork 60
/
client.ts
64 lines (54 loc) · 1.53 KB
/
client.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
import { fork, ChildProcess } from 'child_process';
import { EventEmitter } from 'events';
import { join } from 'path';
export class TscWatchClient extends EventEmitter {
private tsc: ChildProcess | undefined;
start(...args: string[]) {
const tscWatch = require.resolve(join(process.cwd(), 'dist', 'lib', 'tsc-watch'));
this.tsc = fork(tscWatch, args, { stdio: 'inherit' });
this.tsc.on('message', (msg: string) => {
this.emit(...deserializeTscMessage(msg));
});
this.tsc.on('exit', (code: number, signal: number) => {
this.emit('exit', code, signal);
});
}
kill() {
if (this.tsc && this.tsc.kill) {
this.tsc.kill();
}
this.removeAllListeners();
}
runOnCompilationStartedCommand() {
if (this.tsc) {
this.tsc.send('run-on-compilation-started-command');
}
}
runOnCompilationCompleteCommand() {
if (this.tsc) {
this.tsc.send('run-on-compilation-complete-command');
}
}
runOnFirstSuccessCommand() {
if (this.tsc) {
this.tsc.send('run-on-first-success-command');
}
}
runOnFailureCommand() {
if (this.tsc) {
this.tsc.send('run-on-failure-command');
}
}
runOnSuccessCommand() {
if (this.tsc) {
this.tsc.send('run-on-success-command');
}
}
}
function deserializeTscMessage(strMsg: string): [string, string?] {
const indexOfSeparator = strMsg.indexOf(':');
if (indexOfSeparator === -1) {
return [strMsg];
}
return [strMsg.substring(0, indexOfSeparator), strMsg.substring(indexOfSeparator + 1)];
}