Skip to content

Commit

Permalink
feat(watch): --include CLI flag to watch additional files (#625)
Browse files Browse the repository at this point in the history
Co-authored-by: hiroki osame <hiroki.osame@gmail.com>
Co-authored-by: boxizeng <boxizeng@tencent.com>
Co-authored-by: Box Tsang <boxizen@gmail.com>
  • Loading branch information
4 people authored Aug 24, 2024
1 parent af370e7 commit 474ea71
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/watch/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ const flags = {
type: [String],
description: 'Paths & globs to exclude from being watched',
},
include: {
type: [String],
description: 'Additional paths & globs to watch',
},
} as const;

export const watchCommand = command({
Expand All @@ -60,6 +64,7 @@ export const watchCommand = command({
tsconfigPath: argv.flags.tsconfig,
clearScreen: argv.flags.clearScreen,
ignore: argv.flags.ignore,
include: argv.flags.include,
ipc: true,
};

Expand Down Expand Up @@ -195,7 +200,10 @@ export const watchCommand = command({
* As an alternative, we watch cwd and all run-time dependencies
*/
const watcher = watch(
argv._,
[
...argv._,
...options.include,
],
{
cwd: process.cwd(),
ignoreInitial: true,
Expand Down
59 changes: 59 additions & 0 deletions tests/specs/watch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -300,5 +300,64 @@ export default testSuite(async ({ describe }, { tsx }: NodeApis) => {
expect(p.stderr).toBe('');
}, 10_000);
});

describe('watch additional files', ({ test }) => {
test('file path & glob', async () => {
const entryFile = 'index.js';
const fileA = 'file-a';
const fileB = 'directory/file-b';
await using fixture = await createFixture({
[entryFile]: `
import fs from 'fs/promises';
Promise.all([
fs.readFile('./${fileA}', 'utf8'),
fs.readFile('./${fileB}', 'utf8')
]).then(console.log, console.error);
`.trim(),
[fileA]: 'content-a',
[fileB]: 'content-b',
});

const tsxProcess = tsx(
[
'watch',
'--clear-screen=false',
`--include=${fileA}`,
'--include=directory/*',
entryFile,
],
fixture.path,
);

await processInteract(
tsxProcess.stdout!,
[
(data) => {
if (data.includes("'content-a', 'content-b'")) {
fixture.writeFile(fileA, 'update-a');
return true;
}
},
(data) => {
if (data.includes("'update-a', 'content-b'")) {
fixture.writeFile(fileB, 'update-b');
return true;
}
},
(data) => {
if (data.includes("'update-a', 'update-b'")) {
return true;
}
},
],
9000,
);

tsxProcess.kill();

const tsxProcessResolved = await tsxProcess;
expect(tsxProcessResolved.stderr).toBe('');
}, 10_000);
});
});
});

0 comments on commit 474ea71

Please sign in to comment.