Skip to content

Commit 279dd5e

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

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

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

16331641
<!-- YAML
@@ -1932,6 +1940,7 @@ Node.js options that are allowed are:
19321940
* `--use-openssl-ca`
19331941
* `--v8-pool-size`
19341942
* `--watch-path`
1943+
* `--watch-preserve-output`
19351944
* `--watch`
19361945
* `--zero-fill-buffers`
19371946

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)