1 |
|
2 |
|
3 |
|
4 |
|
5 | const chokidar = require('chokidar');
|
6 | const path = require('path');
|
7 | const childProcess = require('child_process');
|
8 | const cwd = process.cwd();
|
9 | const env = process.env;
|
10 | const frontEndDir = path.join(cwd, 'front_end');
|
11 | const testsDir = path.join(cwd, 'test');
|
12 |
|
13 |
|
14 | let target = 'Default';
|
15 | const targetArg = process.argv.find(value => value.startsWith('--target='));
|
16 | if (targetArg) {
|
17 | target = targetArg.slice('--target='.length);
|
18 | }
|
19 |
|
20 | let restartBuild = false;
|
21 | let autoninja;
|
22 | const changedFiles = new Set();
|
23 | const onFileChange = (_, fileName) => {
|
24 |
|
25 |
|
26 |
|
27 | if (changedFiles.has(fileName)) {
|
28 | return;
|
29 | }
|
30 | changedFiles.add(fileName);
|
31 | setTimeout(() => {
|
32 | changedFiles.delete(fileName);
|
33 | }, 250);
|
34 |
|
35 |
|
36 |
|
37 | const ninjaProcessExists = Boolean(autoninja && autoninja.pid);
|
38 | if (ninjaProcessExists) {
|
39 | const isRunning = ninjaProcessExists && autoninja.exitCode === null;
|
40 | if (isRunning) {
|
41 | autoninja.kill();
|
42 | restartBuild = true;
|
43 | }
|
44 | return;
|
45 | }
|
46 |
|
47 | autoninja = childProcess.spawn('autoninja', ['-C', `out/${target}`], {cwd, env, stdio: 'inherit'});
|
48 | autoninja.on('close', () => {
|
49 | autoninja = null;
|
50 | if (restartBuild) {
|
51 | restartBuild = false;
|
52 | console.log(`\n${fileName} changed, restarting ninja\n`);
|
53 | onFileChange();
|
54 | }
|
55 | });
|
56 | };
|
57 |
|
58 |
|
59 | console.log(`Watching for changes in ${frontEndDir} and ${testsDir}; building to out/${target}`);
|
60 | chokidar.watch(frontEndDir).on('all', onFileChange);
|
61 | chokidar.watch(testsDir).on('all', onFileChange);
|