GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — watch: add CLI flag to preserve output · nodejs/node@09ea758

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • doc/api
    • cli.md
  • lib/internal/main
    • watch_mode.js
  • src
    • node_options.cc
    • node_options.h
  • test/sequential
    • test-watch-mode.mjs
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎doc/api/cli.md‎

Copy file name to clipboardExpand all lines: doc/api/cli.md
+9Lines changed: 9 additions & 0 deletions
  • Display the source diff
  • Display the rich diff
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

Collapse file

‎lib/internal/main/watch_mode.js‎

Copy file name to clipboardExpand all lines: lib/internal/main/watch_mode.js
+4-2Lines changed: 4 additions & 2 deletions
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
}
Collapse file

‎src/node_options.cc‎

Copy file name to clipboardExpand all lines: src/node_options.cc
+4Lines changed: 4 additions & 0 deletions
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",
Collapse file

‎src/node_options.h‎

Copy file name to clipboardExpand all lines: src/node_options.h
+1Lines changed: 1 addition & 0 deletions
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;
Collapse file

‎test/sequential/test-watch-mode.mjs‎

Copy file name to clipboardExpand all lines: test/sequential/test-watch-mode.mjs
+25Lines changed: 25 additions & 0 deletions
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 (0)
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.