Skip to content

Commit

Permalink
watch: batch file restarts
Browse files Browse the repository at this point in the history
Co-authored-by: Matthieu <matthieusieben@users.noreply.github.com>
PR-URL: #51992
Reviewed-By: Nitzan Uziely <linkgoron@gmail.com>
Reviewed-By: Chemi Atlow <chemi@atlow.co.il>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
  • Loading branch information
MoLow and matthieusieben committed Mar 9, 2024
1 parent e11c1d2 commit 257f322
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 18 deletions.
35 changes: 18 additions & 17 deletions lib/internal/main/watch_mode.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ const { spawn } = require('child_process');
const { inspect } = require('util');
const { setTimeout, clearTimeout } = require('timers');
const { resolve } = require('path');
const { once, on } = require('events');
const { once } = require('events');

prepareMainThreadExecution(false, false);
markBootstrapComplete();
Expand Down Expand Up @@ -103,27 +103,28 @@ async function stop() {
clearGraceReport();
}

let restarting = false;
async function restart() {
if (!kPreserveOutput) process.stdout.write(clear);
process.stdout.write(`${green}Restarting ${kCommandStr}${white}\n`);
await stop();
start();
}

(async () => {
emitExperimentalWarning('Watch mode');

if (restarting) return;
restarting = true;
try {
if (!kPreserveOutput) process.stdout.write(clear);
process.stdout.write(`${green}Restarting ${kCommandStr}${white}\n`);
await stop();
start();
} finally {
restarting = false;
}
}

// eslint-disable-next-line no-unused-vars
for await (const _ of on(watcher, 'changed')) {
await restart();
}
} catch (error) {
emitExperimentalWarning('Watch mode');
start();
watcher
.on('changed', restart)
.on('error', (error) => {
watcher.off('changed', restart);
triggerUncaughtException(error, true /* fromPromise */);
}
})();
});

// Exiting gracefully to avoid stdout/stderr getting written after
// parent process is killed.
Expand Down
2 changes: 1 addition & 1 deletion lib/internal/watch_mode/files_watcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class FilesWatcher extends EventEmitter {
#signal;

constructor({ debounce = 200, mode = 'filter', signal } = kEmptyObject) {
super();
super({ __proto__: null, captureRejections: true });

validateNumber(debounce, 'options.debounce', 0, TIMEOUT_MAX);
validateOneOf(mode, 'options.mode', ['filter', 'all']);
Expand Down

0 comments on commit 257f322

Please sign in to comment.