-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathwatch-client.js
More file actions
77 lines (69 loc) · 2.3 KB
/
watch-client.js
File metadata and controls
77 lines (69 loc) · 2.3 KB
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
// Watches the client directory
// This script is in JS, not in TS - as we run it _before_ the TypeScript compiler runs
const chokidar = require('chokidar');
const { spawn } = require('child_process');
const Path = require('path');
let isBuilding = false;
function getTimeStr() {
return new Date().toLocaleString('en-US', { hour: 'numeric', minute: 'numeric', second: 'numeric', hour12: true });
}
function runProcess(command, args) {
return new Promise((resolve, reject) => {
const child = spawn(command, args, { stdio: 'inherit' });
child.on('exit', (code) => {
if (code === 0) {
resolve();
}
else {
reject(new Error(`${getTimeStr()} - (client) Compilation failed with code ${code}`));
}
});
});
}
async function runClientBuild() {
isBuilding = true;
try {
await runProcess(Path.join(__dirname, './node_modules/.bin/tsgo'),
['-p', 'client/tsconfig.json', '--incremental']);
await runProcess('node', [Path.join(__dirname, 'build-client-amd.js')]);
}
finally {
isBuilding = false;
}
}
// Watch TS files
chokidar.watch([
'client/',
'js-libs/',
'build-client-amd.js',
], {
ignoreInitial: true,
ignored: 'build/**',
}).on('all', async (event, path) => {
if (isBuilding) {
console.log(`${getTimeStr()} - (client) File changed: ${path}, but rebuild already in progress (ignoring)`);
return;
}
console.log(``);
console.log(`${getTimeStr()} - (client) File ${event}: ${path}. Rebuilding client...`);
try {
await runClientBuild();
console.log(``);
console.log(`${getTimeStr()} - (client) \x1b[32mCompilation done\x1b[0m`);
}
catch (ex) {
console.error(`\x1b[0m${getTimeStr()} - (client) \x1b[31mBuild failed:`, ex.message || ex.toString(), '\x1b[0m');
}
});
// Initial build
(async () => {
try {
console.log(`${getTimeStr()} - (client) Starting incremental compilation...`);
await runClientBuild();
console.log(``);
console.log(`${getTimeStr()} - (client) \x1b[32mCompilation done\x1b[0m`);
}
catch (ex) {
console.error(`\x1b[0m${getTimeStr()} - (client) \x1b[31mBuild failed:`, ex.message || ex.toString(), '\x1b[0m');
}
})();