Skip to content

Commit 5b28a09

Browse files
debadree25danielleadams
authored andcommitted
watch: add CLI flag to preserve output
Fixes: #45713 PR-URL: #45717 Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com> Reviewed-By: Moshe Atlow <moshe@atlow.co.il>
1 parent b76f1ae commit 5b28a09

File tree

5 files changed

+43
-2
lines changed

5 files changed

+43
-2
lines changed

doc/api/cli.md

+9
Original file line numberDiff line numberDiff line change
@@ -1613,6 +1613,14 @@ This option is only supported on macOS and Windows.
16131613
An `ERR_FEATURE_UNAVAILABLE_ON_PLATFORM` exception will be thrown
16141614
when the option is used on a platform that does not support it.
16151615

1616+
### `--watch-preserve-output`
1617+
1618+
Disable the clearing of the console when watch mode restarts the process.
1619+
1620+
```console
1621+
$ node --watch --watch-preserve-output test.js
1622+
```
1623+
16161624
### `--zero-fill-buffers`
16171625

16181626
<!-- YAML
@@ -1915,6 +1923,7 @@ Node.js options that are allowed are:
19151923
* `--use-openssl-ca`
19161924
* `--v8-pool-size`
19171925
* `--watch-path`
1926+
* `--watch-preserve-output`
19181927
* `--watch`
19191928
* `--zero-fill-buffers`
19201929

lib/internal/main/watch_mode.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,11 @@ markBootstrapComplete();
3232
const kKillSignal = 'SIGTERM';
3333
const kShouldFilterModules = getOptionValue('--watch-path').length === 0;
3434
const kWatchedPaths = ArrayPrototypeMap(getOptionValue('--watch-path'), (path) => resolve(path));
35+
const kPreserveOutput = getOptionValue('--watch-preserve-output');
3536
const kCommand = ArrayPrototypeSlice(process.argv, 1);
3637
const kCommandStr = inspect(ArrayPrototypeJoin(kCommand, ' '));
3738
const args = ArrayPrototypeFilter(process.execArgv, (arg, i, arr) =>
38-
arg !== '--watch-path' && arr[i - 1] !== '--watch-path' && arg !== '--watch');
39+
arg !== '--watch-path' && arr[i - 1] !== '--watch-path' && arg !== '--watch' && arg !== '--watch-preserve-output');
3940
ArrayPrototypePushApply(args, kCommand);
4041

4142
const watcher = new FilesWatcher({ throttle: 500, mode: kShouldFilterModules ? 'filter' : 'all' });
@@ -98,7 +99,8 @@ async function stop() {
9899
}
99100

100101
async function restart() {
101-
process.stdout.write(`${clear}${green}Restarting ${kCommandStr}${white}\n`);
102+
if (!kPreserveOutput) process.stdout.write(clear);
103+
process.stdout.write(`${green}Restarting ${kCommandStr}${white}\n`);
102104
await stop();
103105
start();
104106
}

src/node_options.cc

+4
Original file line numberDiff line numberDiff line change
@@ -623,6 +623,10 @@ EnvironmentOptionsParser::EnvironmentOptionsParser() {
623623
"path to watch",
624624
&EnvironmentOptions::watch_mode_paths,
625625
kAllowedInEnvironment);
626+
AddOption("--watch-preserve-output",
627+
"preserve outputs on watch mode restart",
628+
&EnvironmentOptions::watch_mode_preserve_output,
629+
kAllowedInEnvironment);
626630
Implies("--watch-path", "--watch");
627631
AddOption("--check",
628632
"syntax check script without executing",

src/node_options.h

+1
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,7 @@ class EnvironmentOptions : public Options {
178178

179179
bool watch_mode = false;
180180
bool watch_mode_report_to_parent = false;
181+
bool watch_mode_preserve_output = false;
181182
std::vector<std::string> watch_mode_paths;
182183

183184
bool syntax_check_only = false;

test/sequential/test-watch-mode.mjs

+25
Original file line numberDiff line numberDiff line change
@@ -282,4 +282,29 @@ describe('watch mode', { concurrency: false, timeout: 60_000 }, () => {
282282

283283
await failWriteSucceed({ file: dependant, watchedFile: dependency });
284284
});
285+
286+
it('should preserve output when --watch-preserve-output flag is passed', async () => {
287+
const file = createTmpFile();
288+
const { stderr, stdout } = await spawnWithRestarts({
289+
file,
290+
args: ['--watch-preserve-output', file],
291+
});
292+
293+
assert.strictEqual(stderr, '');
294+
// Checks if the existing output is preserved
295+
assertRestartedCorrectly({
296+
stdout,
297+
messages: {
298+
inner: 'running',
299+
restarted: `Restarting ${inspect(file)}`,
300+
completed: `Completed running ${inspect(file)}`,
301+
},
302+
});
303+
// Remove the first 3 lines from stdout
304+
const lines = stdout.split(/\r?\n/).filter(Boolean).slice(3);
305+
assert.deepStrictEqual(lines, [
306+
'running',
307+
`Completed running ${inspect(file)}`,
308+
]);
309+
});
285310
});

0 commit comments

Comments
 (0)