-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathwatch-client.js
More file actions
67 lines (58 loc) · 2.06 KB
/
watch-client.js
File metadata and controls
67 lines (58 loc) · 2.06 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
// 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 runTscBuild() {
return new Promise((resolve, reject) => {
isBuilding = true;
const tsc = spawn(Path.join(__dirname, './node_modules/.bin/tsc'), ['--build', 'client', '--incremental'], { stdio: 'inherit' });
tsc.on('exit', (code) => {
isBuilding = false;
if (code === 0) {
resolve();
}
else {
reject(new Error(`${getTimeStr()} - (client) Compilation failed with code ${code}`));
}
});
});
}
// Watch TS files
chokidar.watch([
'client/',
], {
ignoreInitial: true, ignored: 'build/**'
}).on('change', async (path) => {
if (!path.endsWith('.ts')) return;
if (isBuilding) {
console.log(`${getTimeStr()} - (client) File changed: ${path}, but rebuild already in progress (ignoring)`);
return;
}
console.log(``);
console.log(`${getTimeStr()} - (client) File changed: ${path}. Rebuilding client...`);
try {
await runTscBuild();
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 runTscBuild();
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');
}
})();