Files
react-native-devtools-frontend/scripts/watch_build.js
T
Victor Porof 3393236ac7 Use chokidar as the file watcher
DISABLE_THIRD_PARTY_CHECK=tvanderlippe

Our file watcher that was rebuilding devtools on changes relied on a
recursive option. The recursive option was only supported on macOS and
Windows, and an ERR_FEATURE_UNAVAILABLE_ON_PLATFORM exception was be
thrown when the option is used on a platform that does not support it.

One of those unsupported platforms was linux, which happens to be a
favorite around these places.

This CL switches to a cross-platform file watcher instead.

Bug: 1229622
Change-Id: Id38d1568999cdff8f054a1e716ee2525fed62fe7
Signed-off-by: Victor Porof <victorporof@chromium.org>
Reviewed-on: https://chromium-review.googlesource.com/c/devtools/devtools-frontend/+/3030767
Reviewed-by: Tim van der Lippe <tvanderlippe@chromium.org>
2021-07-15 16:12:46 +00:00

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 chokidar = require('chokidar');
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}`);
chokidar.watch(frontEndDir).on('all', onFileChange);
chokidar.watch(testsDir).on('all', onFileChange);