Skip to content

Commit c29982b

Browse files
committed
src,lib: added --watch-pattern option
1 parent bf21496 commit c29982b

File tree

5 files changed

+39
-14
lines changed

5 files changed

+39
-14
lines changed

lib/internal/fs/glob.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -783,5 +783,4 @@ module.exports = {
783783
__proto__: null,
784784
Glob,
785785
matchGlobPattern,
786-
createMatcher,
787786
};

lib/internal/main/watch_mode.js

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
'use strict';
22
const {
3+
ArrayPrototypeConcat,
34
ArrayPrototypeFlatMap,
45
ArrayPrototypeForEach,
56
ArrayPrototypeJoin,
@@ -28,29 +29,43 @@ const { inspect } = require('util');
2829
const { setTimeout, clearTimeout } = require('timers');
2930
const { resolve } = require('path');
3031
const { once } = require('events');
31-
const { createMatcher } = require('internal/fs/glob');
3232
const { globSync } = require('fs');
3333

3434
prepareMainThreadExecution(false, false);
3535
markBootstrapComplete();
3636

37-
function hasGlobPattern(path) {
38-
return createMatcher(path).hasMagic();
37+
const kWatchPath = getOptionValue('--watch-path');
38+
const kWatchPattern = getOptionValue('--watch-pattern');
39+
40+
function isOptionNotGiven(option) {
41+
return option.length === 0;
42+
}
43+
44+
function isOptionGiven(option) {
45+
return option.length > 0;
3946
}
4047

41-
function handleWatchedPath(path) {
42-
if (hasGlobPattern(path)) {
43-
const matchedFilesFromGlob = globSync(path);
44-
const resolvedMatchedFiles = ArrayPrototypeMap(matchedFilesFromGlob, (path) => resolve(path));
45-
return resolvedMatchedFiles;
48+
function handleWatchedPaths() {
49+
const watchedPaths = [];
50+
if (isOptionGiven(kWatchPath)) {
51+
ArrayPrototypeForEach(kWatchPath, (path) => {
52+
ArrayPrototypePush(watchedPaths, resolve(path));
53+
});
54+
}
55+
if (isOptionGiven(kWatchPattern)) {
56+
ArrayPrototypeForEach(kWatchPattern, (path) => {
57+
const matchedPaths = globSync(path);
58+
ArrayPrototypeForEach(matchedPaths, (matchedPathFromGlobPattern) =>
59+
ArrayPrototypePush(watchedPaths, resolve(matchedPathFromGlobPattern)));
60+
});
4661
}
47-
return resolve(path);
62+
return watchedPaths;
4863
}
4964

5065
const kKillSignal = convertToValidSignal(getOptionValue('--watch-kill-signal'));
51-
const kShouldFilterModules = getOptionValue('--watch-pattern').length === 0;
66+
const kShouldFilterModules = isOptionNotGiven(kWatchPath) && isOptionNotGiven(kWatchPattern);
5267
const kEnvFile = getOptionValue('--env-file') || getOptionValue('--env-file-if-exists');
53-
const kWatchedPaths = ArrayPrototypeFlatMap(getOptionValue('--watch-path'), (path) => handleWatchedPath(path));
68+
const kWatchedPaths = handleWatchedPaths();
5469
const kPreserveOutput = getOptionValue('--watch-preserve-output');
5570
const kCommand = ArrayPrototypeSlice(process.argv, 1);
5671
const kCommandStr = inspect(ArrayPrototypeJoin(kCommand, ' '));
@@ -72,6 +87,12 @@ for (let i = 0; i < process.execArgv.length; i++) {
7287
}
7388
continue;
7489
}
90+
if (StringPrototypeStartsWith(arg, '--watch-pattern')) {
91+
if (arg['--watch-pattern'.length] !== '=') {
92+
i++;
93+
}
94+
continue;
95+
}
7596
if (StringPrototypeStartsWith(arg, '--watch-path')) {
7697
const lengthOfWatchPathStr = 12;
7798
if (arg[lengthOfWatchPathStr] !== '=') {

src/node_options.cc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1006,6 +1006,9 @@ EnvironmentOptionsParser::EnvironmentOptionsParser() {
10061006
"path to watch",
10071007
&EnvironmentOptions::watch_mode_paths,
10081008
kAllowedInEnvvar);
1009+
AddOption("--watch-pattern",
1010+
"paths to watch (can include a glob pattern)",
1011+
&EnvironmentOptions::watch_mode_paths_with_glob_patterns);
10091012
AddOption("--watch-kill-signal",
10101013
"kill signal to send to the process on watch mode restarts"
10111014
"(default: SIGTERM)",
@@ -1016,6 +1019,7 @@ EnvironmentOptionsParser::EnvironmentOptionsParser() {
10161019
&EnvironmentOptions::watch_mode_preserve_output,
10171020
kAllowedInEnvvar);
10181021
Implies("--watch-path", "--watch");
1022+
Implies("--watch-pattern", "--watch");
10191023
AddOption("--check",
10201024
"syntax check script without executing",
10211025
&EnvironmentOptions::syntax_check_only);

src/node_options.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,7 @@ class EnvironmentOptions : public Options {
234234
bool watch_mode_preserve_output = false;
235235
std::string watch_mode_kill_signal = "SIGTERM";
236236
std::vector<std::string> watch_mode_paths;
237+
std::vector<std::string> watch_mode_paths_with_glob_patterns;
237238

238239
bool syntax_check_only = false;
239240
bool has_eval_string = false;

test/sequential/test-watch-mode.mjs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -814,7 +814,7 @@ process.on('message', (message) => {
814814
]);
815815
});
816816

817-
it('should watch files from a given glob pattern --watch-path=./**/*.js', async () => {
817+
it('should watch files from a given glob pattern --watch-pattern=./**/*.js', async () => {
818818

819819
const tmpDirForGlobTest = tmpdir.resolve('glob-test-dir');
820820
mkdirSync(tmpDirForGlobTest);
@@ -835,7 +835,7 @@ process.on('message', (message) => {
835835

836836
const mainJsFile = createTmpFile('console.log(\'running\')', '.js', tmpDirForGlobTest);
837837

838-
const args = ['--watch-path', globPattern, mainJsFile];
838+
const args = ['--watch-pattern', globPattern, mainJsFile];
839839
const watchedFiles = [tmpJsFile1, tmpJsFile2, tmpJsFile3, tmpJsFile4, tmpJsFile5];
840840

841841
const { stderr, stdout } = await runWriteSucceed({

0 commit comments

Comments
 (0)