Skip to content

Commit f5d9257

Browse files
MoLowtargos
authored andcommitted
watch: fix arguments parsing
PR-URL: #52760 Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: Raz Luvaton <rluvaton@gmail.com>
1 parent 6b4dac3 commit f5d9257

File tree

2 files changed

+48
-4
lines changed

2 files changed

+48
-4
lines changed

lib/internal/main/watch_mode.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ const {
66
ArrayPrototypePush,
77
ArrayPrototypePushApply,
88
ArrayPrototypeSlice,
9-
StringPrototypeIncludes,
109
StringPrototypeStartsWith,
1110
} = primordials;
1211

@@ -44,8 +43,10 @@ const argsWithoutWatchOptions = [];
4443
for (let i = 0; i < process.execArgv.length; i++) {
4544
const arg = process.execArgv[i];
4645
if (StringPrototypeStartsWith(arg, '--watch')) {
47-
if (!StringPrototypeIncludes(arg, '=')) {
48-
i++;
46+
i++;
47+
const nextArg = process.execArgv[i];
48+
if (nextArg && StringPrototypeStartsWith(nextArg, '-')) {
49+
ArrayPrototypePush(argsWithoutWatchOptions, nextArg);
4950
}
5051
continue;
5152
}

test/sequential/test-watch-mode.mjs

+44-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,9 @@ async function runWriteSucceed({
3939
options = {},
4040
shouldFail = false
4141
}) {
42-
const child = spawn(execPath, [watchFlag, '--no-warnings', ...args], { encoding: 'utf8', stdio: 'pipe', ...options });
42+
args.unshift('--no-warnings');
43+
if (watchFlag !== null) args.unshift(watchFlag);
44+
const child = spawn(execPath, args, { encoding: 'utf8', stdio: 'pipe', ...options });
4345
let completes = 0;
4446
let cancelRestarts = () => {};
4547
let stderr = '';
@@ -531,4 +533,45 @@ console.log(values.random);
531533
`Completed running ${inspect(file)}`,
532534
]);
533535
});
536+
537+
it('should run when `--watch --inspect`', async () => {
538+
const file = createTmpFile();
539+
const args = ['--watch', '--inspect', file];
540+
const { stdout, stderr } = await runWriteSucceed({ file, watchedFile: file, watchFlag: null, args });
541+
542+
assert.match(stderr, /listening on ws:\/\//);
543+
assert.deepStrictEqual(stdout, [
544+
'running',
545+
`Completed running ${inspect(file)}`,
546+
`Restarting ${inspect(file)}`,
547+
'running',
548+
`Completed running ${inspect(file)}`,
549+
]);
550+
});
551+
552+
it('should run when `--watch -r ./foo.js`', async () => {
553+
const projectDir = tmpdir.resolve('project7');
554+
mkdirSync(projectDir);
555+
556+
const dir = path.join(projectDir, 'watched-dir');
557+
mkdirSync(dir);
558+
writeFileSync(path.join(projectDir, 'some.js'), "console.log('hello')");
559+
560+
const file = createTmpFile("console.log('running');", '.js', projectDir);
561+
const args = ['--watch', '-r', './some.js', file];
562+
const { stdout, stderr } = await runWriteSucceed({
563+
file, watchedFile: file, watchFlag: null, args, options: { cwd: projectDir }
564+
});
565+
566+
assert.strictEqual(stderr, '');
567+
assert.deepStrictEqual(stdout, [
568+
'hello',
569+
'running',
570+
`Completed running ${inspect(file)}`,
571+
`Restarting ${inspect(file)}`,
572+
'hello',
573+
'running',
574+
`Completed running ${inspect(file)}`,
575+
]);
576+
});
534577
});

0 commit comments

Comments
 (0)