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 15 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
7 changes: 6 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: 'Paths & globs to include in 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,7 @@ 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
55 changes: 55 additions & 0 deletions tests/specs/watch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ import type { NodeApis } from '../utils/tsx.js';
import { processInteract } from '../utils/process-interact.js';
import { createPackageJson } from '../fixtures.js';

// reRun debounce from src/watch/index.ts
const watchDebounce = 100;

export default testSuite(async ({ describe }, { tsx }: NodeApis) => {
describe('watch', async ({ test, describe, onFinish }) => {
const fixture = await createFixture({
Expand Down Expand Up @@ -300,5 +303,57 @@ export default testSuite(async ({ describe }, { tsx }: NodeApis) => {
expect(p.stderr).toBe('');
}, 10_000);
});

describe('include', ({ 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(([a, b]) => {
console.log(a + ' ' + b);
}).catch(console.error);
`.trim(),
[fileA]: 'content-a',
[fileB]: 'content-b',
});

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

tsxProcess.stdout?.on('data', async (data: Buffer) => {
const chunkString = data.toString();
if (chunkString.includes('content-a content-b')) {
await fixture.writeFile(fileA, 'update-a');
await setTimeout(watchDebounce + 10);
await fixture.writeFile(fileB, 'update-b');
} else if (chunkString.includes('update-a update-b')) {
await fixture.writeFile(entryFile, 'console.log("TERMINATE")');
} else if (chunkString.includes('TERMINATE')) {
tsxProcess.kill();
}
});

const tsxProcessResolved = await tsxProcess;
const stdout = stripAnsi(tsxProcessResolved.stdout);
expect(stdout).toContain(`change in ./${fileA}`);
expect(stdout).toContain(`change in ./${fileB}`);
mdmower-csnw marked this conversation as resolved.
Show resolved Hide resolved
expect(stdout).toContain(`change in ./${entryFile}`);
expect(tsxProcessResolved.stderr).toBe('');
}, 10_000);
});
});
});
Loading