Skip to content

Commit

Permalink
watch: fix arguments parsing
Browse files Browse the repository at this point in the history
PR-URL: nodejs#52760
Reviewed-By: Benjamin Gruenbaum <[email protected]>
Reviewed-By: Raz Luvaton <[email protected]>
  • Loading branch information
MoLow authored and Whitecx committed May 2, 2024
1 parent 87b896b commit ee5c037
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 4 deletions.
7 changes: 4 additions & 3 deletions lib/internal/main/watch_mode.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ const {
ArrayPrototypePush,
ArrayPrototypePushApply,
ArrayPrototypeSlice,
StringPrototypeIncludes,
StringPrototypeStartsWith,
} = primordials;

Expand Down Expand Up @@ -44,8 +43,10 @@ const argsWithoutWatchOptions = [];
for (let i = 0; i < process.execArgv.length; i++) {
const arg = process.execArgv[i];
if (StringPrototypeStartsWith(arg, '--watch')) {
if (!StringPrototypeIncludes(arg, '=')) {
i++;
i++;
const nextArg = process.execArgv[i];
if (nextArg && StringPrototypeStartsWith(nextArg, '-')) {
ArrayPrototypePush(argsWithoutWatchOptions, nextArg);
}
continue;
}
Expand Down
45 changes: 44 additions & 1 deletion test/sequential/test-watch-mode.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ async function runWriteSucceed({
options = {},
shouldFail = false
}) {
const child = spawn(execPath, [watchFlag, '--no-warnings', ...args], { encoding: 'utf8', stdio: 'pipe', ...options });
args.unshift('--no-warnings');
if (watchFlag !== null) args.unshift(watchFlag);
const child = spawn(execPath, args, { encoding: 'utf8', stdio: 'pipe', ...options });
let completes = 0;
let cancelRestarts = () => {};
let stderr = '';
Expand Down Expand Up @@ -531,4 +533,45 @@ console.log(values.random);
`Completed running ${inspect(file)}`,
]);
});

it('should run when `--watch --inspect`', async () => {
const file = createTmpFile();
const args = ['--watch', '--inspect', file];
const { stdout, stderr } = await runWriteSucceed({ file, watchedFile: file, watchFlag: null, args });

assert.match(stderr, /listening on ws:\/\//);
assert.deepStrictEqual(stdout, [
'running',
`Completed running ${inspect(file)}`,
`Restarting ${inspect(file)}`,
'running',
`Completed running ${inspect(file)}`,
]);
});

it('should run when `--watch -r ./foo.js`', async () => {
const projectDir = tmpdir.resolve('project7');
mkdirSync(projectDir);

const dir = path.join(projectDir, 'watched-dir');
mkdirSync(dir);
writeFileSync(path.join(projectDir, 'some.js'), "console.log('hello')");

const file = createTmpFile("console.log('running');", '.js', projectDir);
const args = ['--watch', '-r', './some.js', file];
const { stdout, stderr } = await runWriteSucceed({
file, watchedFile: file, watchFlag: null, args, options: { cwd: projectDir }
});

assert.strictEqual(stderr, '');
assert.deepStrictEqual(stdout, [
'hello',
'running',
`Completed running ${inspect(file)}`,
`Restarting ${inspect(file)}`,
'hello',
'running',
`Completed running ${inspect(file)}`,
]);
});
});

0 comments on commit ee5c037

Please sign in to comment.