Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(watch): --include CLI flag to watch additional files #625

Merged
merged 20 commits into from
Aug 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
});
});
});
Loading