-
Notifications
You must be signed in to change notification settings - Fork 8
/
worker.ts
75 lines (68 loc) · 2.76 KB
/
worker.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
import { WorkerCommand, IWorkerOptions, IResults } from './interfaces';
import { inspectCode } from './inspectCode';
import { printResult } from './printResult';
import { watchSrc } from './watchSrc';
import { printSettings } from './printSettings';
import { debugPrint } from './debugPrint';
let lastResult: IResults;
let printErrorTotal: number;
// listen for messages
process.on('message', function(msg: IWorkerOptions) {
debugPrint(`worker message recived ${msg.type}`);
switch (msg.type) {
// tell checker to inspect code
case WorkerCommand.inspectCode:
debugPrint('worker inspectCode');
if (msg.options) {
lastResult = inspectCode(msg.options, lastResult && lastResult.oldProgram);
} else {
throw new Error('You tried to inspect code without ts/lint options');
}
break;
// tell checker to inspect code
case WorkerCommand.inspectCodeAndPrint:
debugPrint('worker inspectCodeAndPrint');
if (msg.options) {
lastResult = inspectCode(msg.options, lastResult && lastResult.oldProgram);
printErrorTotal = printResult(msg.options, lastResult);
printErrorTotal = printErrorTotal;
} else {
throw new Error('You tried to inspect code without ts/lint options');
}
break;
case WorkerCommand.printResult:
debugPrint('worker printResult');
if (msg.options && lastResult) {
printErrorTotal = printResult(msg.options, lastResult);
} else {
throw new Error(
'You tried to print code without ts/lint options or without having inspected code'
);
}
break;
case WorkerCommand.printSettings:
debugPrint('worker printSettings');
if (msg.options) {
printSettings(msg.options);
} else {
throw new Error(
'You tried to print settings without ts/lint options or without having inspected code'
);
}
break;
case WorkerCommand.watch:
debugPrint('worker watch');
if (msg.options) {
watchSrc(msg.pathToWatch, msg.options, () => {
lastResult = inspectCode(msg.options, lastResult && lastResult.oldProgram);
printErrorTotal = printResult(msg.options, lastResult);
});
} else {
throw new Error(
'You tried to print code without ts/lint options or without having inspected code'
);
}
break;
}
});
debugPrint('worker started');