GeezerVison is showing github.com — watch: add CLI flag to preserve output · nodejs/node@09ea758
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file 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 number | Diff line number | Diff line change |
|---|
@@ -1613,6 +1613,14 @@ This option is only supported on macOS and Windows. | 1613 | 1613 | An `ERR_FEATURE_UNAVAILABLE_ON_PLATFORM` exception will be thrown | 1614 | 1614 | when the option is used on a platform that does not support it. | 1615 | 1615 |
| | 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 | + | 1616 | 1624 | ### `--zero-fill-buffers` | 1617 | 1625 |
| 1618 | 1626 | <!-- YAML | @@ -1915,6 +1923,7 @@ Node.js options that are allowed are: | 1915 | 1923 | * `--use-openssl-ca` | 1916 | 1924 | * `--v8-pool-size` | 1917 | 1925 | * `--watch-path` | | 1926 | +* `--watch-preserve-output` | 1918 | 1927 | * `--watch` | 1919 | 1928 | * `--zero-fill-buffers` | 1920 | 1929 |
| |
Collapse file Copy file name to clipboardExpand all lines: lib/internal/main/watch_mode.js +4-2Lines changed: 4 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -32,10 +32,11 @@ markBootstrapComplete(); | 32 | 32 | const kKillSignal = 'SIGTERM'; | 33 | 33 | const kShouldFilterModules = getOptionValue('--watch-path').length === 0; | 34 | 34 | const kWatchedPaths = ArrayPrototypeMap(getOptionValue('--watch-path'), (path) => resolve(path)); | | 35 | +const kPreserveOutput = getOptionValue('--watch-preserve-output'); | 35 | 36 | const kCommand = ArrayPrototypeSlice(process.argv, 1); | 36 | 37 | const kCommandStr = inspect(ArrayPrototypeJoin(kCommand, ' ')); | 37 | 38 | 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'); | 39 | 40 | ArrayPrototypePushApply(args, kCommand); | 40 | 41 |
| 41 | 42 | const watcher = new FilesWatcher({ throttle: 500, mode: kShouldFilterModules ? 'filter' : 'all' }); | @@ -98,7 +99,8 @@ async function stop() { | 98 | 99 | } | 99 | 100 |
| 100 | 101 | 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`); | 102 | 104 | await stop(); | 103 | 105 | start(); | 104 | 106 | } | |
Collapse file Copy file name to clipboardExpand all lines: src/node_options.cc +4Lines changed: 4 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -623,6 +623,10 @@ EnvironmentOptionsParser::EnvironmentOptionsParser() { | 623 | 623 | "path to watch", | 624 | 624 | &EnvironmentOptions::watch_mode_paths, | 625 | 625 | kAllowedInEnvironment); | | 626 | + AddOption("--watch-preserve-output", | | 627 | + "preserve outputs on watch mode restart", | | 628 | + &EnvironmentOptions::watch_mode_preserve_output, | | 629 | + kAllowedInEnvironment); | 626 | 630 | Implies("--watch-path", "--watch"); | 627 | 631 | AddOption("--check", | 628 | 632 | "syntax check script without executing", | |
Collapse file Copy file name to clipboardExpand all lines: src/node_options.h +1Lines changed: 1 addition & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -178,6 +178,7 @@ class EnvironmentOptions : public Options { | 178 | 178 |
| 179 | 179 | bool watch_mode = false; | 180 | 180 | bool watch_mode_report_to_parent = false; | | 181 | + bool watch_mode_preserve_output = false; | 181 | 182 | std::vector<std::string> watch_mode_paths; | 182 | 183 |
| 183 | 184 | bool syntax_check_only = false; | |
Collapse file Copy file name to clipboardExpand all lines: test/sequential/test-watch-mode.mjs +25Lines changed: 25 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -282,4 +282,29 @@ describe('watch mode', { concurrency: false, timeout: 60_000 }, () => { | 282 | 282 |
| 283 | 283 | await failWriteSucceed({ file: dependant, watchedFile: dependency }); | 284 | 284 | }); | | 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 | + }); | 285 | 310 | }); |
|