-
Notifications
You must be signed in to change notification settings - Fork 22
/
build.js
99 lines (91 loc) · 2.42 KB
/
build.js
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
import { spawn } from 'child_process';
function cmd(program, args) {
console.log('CMD:', program, args);
const p = spawn(program, args.flat()); // NOTE: flattening the args array enables you to group related arguments for better self-documentation of the running command
p.stdout.on('data', (data) => process.stdout.write(data));
p.stderr.on('data', (data) => process.stderr.write(data));
p.on('close', (code) => {
if (code !== 0) {
console.error(program, args, 'exited with', code);
}
});
return p;
}
const commonTscFlags = [
'--strict',
'--removeComments',
'--skipLibCheck',
];
const mainTs = [
'ts/eval.ts',
'ts/filters.ts',
'ts/grecha.ts',
'ts/index.ts'
];
function tscMain(...extraParams) {
cmd('tsc', [
...commonTscFlags,
['--outDir', 'js'],
...extraParams,
mainTs,
]);
}
function tscServiceWorker(...extraParams) {
cmd('tsc', [
...commonTscFlags,
['--lib', 'webworker'],
['--outFile', 'serviceworker.js'],
...extraParams,
'serviceworker.ts'
]);
}
function build(part, ...args) {
switch (part) {
case undefined:
tscServiceWorker();
tscMain();
break;
case 'main':
tscMain();
break;
case 'serviceworker':
tscServiceWorker();
break;
default:
throw new Error(`Unknown build part ${part}. Available parts: main, serviceworker.`);
}
}
function watch(part, ...args) {
switch (part) {
case undefined:
tscMain('-w', '--preserveWatchOutput');
tscServiceWorker('-w', '--preserveWatchOutput');
break;
case 'main':
tscMain('-w', '--preserveWatchOutput');
break;
case 'serviceworker':
tscServiceWorker('-w', '--preserveWatchOutput');
break;
default:
throw new Error(`Unknown watch part ${part}. Available parts: main, serviceworker.`);
}
}
const [nodePath, scriptPath, command, ...args] = process.argv;
switch (command) {
case undefined:
case 'build':
build(...args);
break;
case 'watch':
watch(...args);
break;
case 'serve':
// TODO: maybe replace Python with something from Node itself?
// Python is a pretty unreasonable dependency.
cmd('python3', [['-m', 'http.server'], '6969']);
watch();
break;
default:
throw new Error(`Unknown command ${command}. Available commands: build, watch.`);
}