From a38e4a8932e20de1cf8acaddfae724092335f56f Mon Sep 17 00:00:00 2001 From: Moshe Atlow Date: Wed, 6 Mar 2024 23:07:29 +0200 Subject: [PATCH] watch: batch file restarts Co-authored-by: Matthieu --- lib/internal/main/watch_mode.js | 35 ++++++++++++------------ lib/internal/watch_mode/files_watcher.js | 2 +- 2 files changed, 19 insertions(+), 18 deletions(-) diff --git a/lib/internal/main/watch_mode.js b/lib/internal/main/watch_mode.js index 3be329c57af6db..05433c934b6c03 100644 --- a/lib/internal/main/watch_mode.js +++ b/lib/internal/main/watch_mode.js @@ -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(); @@ -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. diff --git a/lib/internal/watch_mode/files_watcher.js b/lib/internal/watch_mode/files_watcher.js index bbc13e67cc9f0d..9e018045e520fd 100644 --- a/lib/internal/watch_mode/files_watcher.js +++ b/lib/internal/watch_mode/files_watcher.js @@ -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']);