Paul Lewis | 64ba20b | 2020-06-29 16:17:21 +0100 | [diff] [blame] | 1 | // Copyright 2020 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | const fs = require('fs'); |
| 6 | const path = require('path'); |
| 7 | const childProcess = require('child_process'); |
| 8 | const cwd = process.cwd(); |
| 9 | const env = process.env; |
Paul Lewis | ab73c6f | 2020-11-27 11:30:40 +0000 | [diff] [blame^] | 10 | const frontEndDir = path.join(cwd, 'front_end'); |
| 11 | const testsDir = path.join(cwd, 'test'); |
Paul Lewis | 64ba20b | 2020-06-29 16:17:21 +0100 | [diff] [blame] | 12 | |
| 13 | // Extract the target if it's provided. |
| 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 | |
Paul Lewis | ab73c6f | 2020-11-27 11:30:40 +0000 | [diff] [blame^] | 20 | let restartBuild = false; |
| 21 | let autoninja; |
| 22 | const changedFiles = new Set(); |
| 23 | const onFileChange = (_, fileName) => { |
| 24 | // Some filesystems emit multiple events in quick succession for a |
| 25 | // single file change. Here we track the changed files, and reset |
| 26 | // after a short timeout. |
| 27 | if (changedFiles.has(fileName)) { |
| 28 | return; |
| 29 | } |
| 30 | changedFiles.add(fileName); |
| 31 | setTimeout(() => { |
| 32 | changedFiles.delete(fileName); |
| 33 | }, 250); |
| 34 | |
| 35 | // If the exitCode is null, autoninja is still running so stop it |
| 36 | // and try to restart it again. |
| 37 | const ninjaProcessExists = !!(autoninja && autoninja.pid); |
| 38 | if (ninjaProcessExists) { |
| 39 | const isRunning = ninjaProcessExists && autoninja.exitCode === null; |
| 40 | if (isRunning) { |
| 41 | autoninja.kill(); |
| 42 | restartBuild = true; |
| 43 | } |
Paul Lewis | 64ba20b | 2020-06-29 16:17:21 +0100 | [diff] [blame] | 44 | return; |
| 45 | } |
| 46 | |
Paul Lewis | ab73c6f | 2020-11-27 11:30:40 +0000 | [diff] [blame^] | 47 | autoninja = childProcess.spawn('autoninja', ['-C', `out/${target}`], {cwd, env, stdio: 'inherit'}); |
Paul Lewis | 64ba20b | 2020-06-29 16:17:21 +0100 | [diff] [blame] | 48 | autoninja.on('close', () => { |
Paul Lewis | ab73c6f | 2020-11-27 11:30:40 +0000 | [diff] [blame^] | 49 | autoninja = null; |
| 50 | if (restartBuild) { |
| 51 | restartBuild = false; |
| 52 | console.log(`\n${fileName} changed, restarting ninja\n`); |
| 53 | onFileChange(); |
Paul Lewis | 64ba20b | 2020-06-29 16:17:21 +0100 | [diff] [blame] | 54 | } |
Paul Lewis | 64ba20b | 2020-06-29 16:17:21 +0100 | [diff] [blame] | 55 | }); |
| 56 | }; |
| 57 | |
Paul Lewis | ab73c6f | 2020-11-27 11:30:40 +0000 | [diff] [blame^] | 58 | // Watch the front_end and test folder and build on any change. |
| 59 | console.log(`Watching for changes in ${frontEndDir} and ${testsDir}; building to out/${target}`); |
| 60 | fs.watch(`${frontEndDir}`, {recursive: true}, onFileChange); |
| 61 | fs.watch(`${testsDir}`, {recursive: true}, onFileChange); |