Skip to content

Commit cee6f38

Browse files
debadree25targos
authored andcommitted
watch: add CLI flag to preserve output
Fixes: #45713 PR-URL: #45717 Reviewed-By: Antoine du Hamel <[email protected]> Reviewed-By: Moshe Atlow <[email protected]>
1 parent 5c4475d commit cee6f38

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
@@ -1627,6 +1627,14 @@ This option is only supported on macOS and Windows.
16271627
An `ERR_FEATURE_UNAVAILABLE_ON_PLATFORM` exception will be thrown
16281628
when the option is used on a platform that does not support it.
16291629

1630+
### `--watch-preserve-output`
1631+
1632+
Disable the clearing of the console when watch mode restarts the process.
1633+
1634+
```console
1635+
$ node --watch --watch-preserve-output test.js
1636+
```
1637+
16301638
### `--zero-fill-buffers`
16311639

16321640
<!-- YAML
@@ -1931,6 +1939,7 @@ Node.js options that are allowed are:
19311939
* `--use-openssl-ca`
19321940
* `--v8-pool-size`
19331941
* `--watch-path`
1942+
* `--watch-preserve-output`
19341943
* `--watch`
19351944
* `--zero-fill-buffers`
19361945

lib/internal/main/watch_mode.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,11 @@ markBootstrapComplete();
3434
const kKillSignal = 'SIGTERM';
3535
const kShouldFilterModules = getOptionValue('--watch-path').length === 0;
3636
const kWatchedPaths = ArrayPrototypeMap(getOptionValue('--watch-path'), (path) => resolve(path));
37+
const kPreserveOutput = getOptionValue('--watch-preserve-output');
3738
const kCommand = ArrayPrototypeSlice(process.argv, 1);
3839
const kCommandStr = inspect(ArrayPrototypeJoin(kCommand, ' '));
3940
const args = ArrayPrototypeFilter(process.execArgv, (arg, i, arr) =>
40-
arg !== '--watch-path' && arr[i - 1] !== '--watch-path' && arg !== '--watch');
41+
arg !== '--watch-path' && arr[i - 1] !== '--watch-path' && arg !== '--watch' && arg !== '--watch-preserve-output');
4142
ArrayPrototypePushApply(args, kCommand);
4243

4344
const watcher = new FilesWatcher({ throttle: 500, mode: kShouldFilterModules ? 'filter' : 'all' });
@@ -100,7 +101,8 @@ async function stop() {
100101
}
101102

102103
async function restart() {
103-
process.stdout.write(`${clear}${green}Restarting ${kCommandStr}${white}\n`);
104+
if (!kPreserveOutput) process.stdout.write(clear);
105+
process.stdout.write(`${green}Restarting ${kCommandStr}${white}\n`);
104106
await stop();
105107
start();
106108
}

src/node_options.cc

+4
Original file line numberDiff line numberDiff line change
@@ -613,6 +613,10 @@ EnvironmentOptionsParser::EnvironmentOptionsParser() {
613613
"path to watch",
614614
&EnvironmentOptions::watch_mode_paths,
615615
kAllowedInEnvvar);
616+
AddOption("--watch-preserve-output",
617+
"preserve outputs on watch mode restart",
618+
&EnvironmentOptions::watch_mode_preserve_output,
619+
kAllowedInEnvvar);
616620
Implies("--watch-path", "--watch");
617621
AddOption("--check",
618622
"syntax check script without executing",

src/node_options.h

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

178178
bool watch_mode = false;
179179
bool watch_mode_report_to_parent = false;
180+
bool watch_mode_preserve_output = false;
180181
std::vector<std::string> watch_mode_paths;
181182

182183
bool syntax_check_only = false;

test/sequential/test-watch-mode.mjs

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

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

0 commit comments

Comments
 (0)