mirror of
https://github.com/react/react-native-devtools-frontend.git
synced 2026-09-30 01:07:01 +08:00
R=mathias@chromium.org,jacktfranklin@chromium.org Fixed: 1082789 Change-Id: I2cf3c221e748654c296f35c3f149b1f76684f4a3 Reviewed-on: https://chromium-review.googlesource.com/c/devtools/devtools-frontend/+/2612903 Reviewed-by: Jack Franklin <jacktfranklin@chromium.org> Reviewed-by: Mathias Bynens <mathias@chromium.org> Commit-Queue: Tim van der Lippe <tvanderlippe@chromium.org>
62 lines
2.0 KiB
JavaScript
62 lines
2.0 KiB
JavaScript
// Copyright 2020 The Chromium Authors. All rights reserved.
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
// found in the LICENSE file.
|
|
|
|
const fs = require('fs');
|
|
const path = require('path');
|
|
const childProcess = require('child_process');
|
|
const cwd = process.cwd();
|
|
const env = process.env;
|
|
const frontEndDir = path.join(cwd, 'front_end');
|
|
const testsDir = path.join(cwd, 'test');
|
|
|
|
// Extract the target if it's provided.
|
|
let target = 'Default';
|
|
const targetArg = process.argv.find(value => value.startsWith('--target='));
|
|
if (targetArg) {
|
|
target = targetArg.slice('--target='.length);
|
|
}
|
|
|
|
let restartBuild = false;
|
|
let autoninja;
|
|
const changedFiles = new Set();
|
|
const onFileChange = (_, fileName) => {
|
|
// Some filesystems emit multiple events in quick succession for a
|
|
// single file change. Here we track the changed files, and reset
|
|
// after a short timeout.
|
|
if (changedFiles.has(fileName)) {
|
|
return;
|
|
}
|
|
changedFiles.add(fileName);
|
|
setTimeout(() => {
|
|
changedFiles.delete(fileName);
|
|
}, 250);
|
|
|
|
// If the exitCode is null, autoninja is still running so stop it
|
|
// and try to restart it again.
|
|
const ninjaProcessExists = Boolean(autoninja && autoninja.pid);
|
|
if (ninjaProcessExists) {
|
|
const isRunning = ninjaProcessExists && autoninja.exitCode === null;
|
|
if (isRunning) {
|
|
autoninja.kill();
|
|
restartBuild = true;
|
|
}
|
|
return;
|
|
}
|
|
|
|
autoninja = childProcess.spawn('autoninja', ['-C', `out/${target}`], {cwd, env, stdio: 'inherit'});
|
|
autoninja.on('close', () => {
|
|
autoninja = null;
|
|
if (restartBuild) {
|
|
restartBuild = false;
|
|
console.log(`\n${fileName} changed, restarting ninja\n`);
|
|
onFileChange();
|
|
}
|
|
});
|
|
};
|
|
|
|
// Watch the front_end and test folder and build on any change.
|
|
console.log(`Watching for changes in ${frontEndDir} and ${testsDir}; building to out/${target}`);
|
|
fs.watch(`${frontEndDir}`, {recursive: true}, onFileChange);
|
|
fs.watch(`${testsDir}`, {recursive: true}, onFileChange);
|