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 & --exclude CLI flags #561

Closed
wants to merge 9 commits into from
Closed
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
13 changes: 11 additions & 2 deletions src/watch/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,14 @@ const flags = {
type: [String],
description: 'Paths & globs to exclude from being watched',
},
include: {
type: [String],
description: 'Paths & globs to include from being watched',
},
exclude: {
type: [String],
description: 'Paths & globs to exclude from being watched (same as ignore flag)',
},
} as const;

export const watchCommand = command({
Expand All @@ -62,7 +70,6 @@ export const watchCommand = command({
ignore: argv.flags.ignore,
ipc: true,
};

let runProcess: ChildProcess | undefined;
let exiting = false;

Expand Down Expand Up @@ -195,7 +202,7 @@ export const watchCommand = command({
* As an alternative, we watch cwd and all run-time dependencies
*/
const watcher = watch(
argv._,
[...argv._, ...argv.flags.include],
{
cwd: process.cwd(),
ignoreInitial: true,
Expand All @@ -210,6 +217,8 @@ export const watchCommand = command({
'**/{node_modules,bower_components,vendor}/**',

...options.ignore,

...argv.flags.exclude,
],
ignorePermissionErrors: true,
},
Expand Down
54 changes: 54 additions & 0 deletions tests/specs/watch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -300,5 +300,59 @@ export default testSuite(async ({ describe }, { tsx }: NodeApis) => {
expect(p.stderr).toBe('');
}, 10_000);
});

describe('include', ({ test }) => {
test('watch files for changes', async () => {
const entryFile = 'index.js';
const file = 'file';
const value = 'test';
const append = `${value}append`;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see you're trying to couple these together via variables but I think the test will be much more readable if you just hard-code them

const fixture = await createFixture({
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use the await using syntax so you can remove the fixture.rm() below

[entryFile]: `
import fs from 'fs';
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
import fs from 'fs';
import fs from 'fs/promises';

fs.readFile('./${file}', (err, data) => {
if (!err) {
console.log(data.toString());
} else {
console.log(error);
}
});
`.trim(),
[file]: `${value}`,
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
[file]: `${value}`,
[file]: value,

});

const tsxProcess = tsx({
cwd: fixture.path,
args: [
'watch',
'--clear-screen=false',
`--include=${file}`,
entryFile,
],
});

tsxProcess.stdout?.on('data', async (data: Buffer) => {
const chunkString = data.toString();
if (chunkString === `${value}\n`) {
await fixture.writeFile(file, `${append}`);
}

if (chunkString === `${append}\n`) {
await setTimeout(500);
await fixture.writeFile(entryFile, 'console.log("TERMINATE")');
}

if (chunkString === 'TERMINATE\n') {
tsxProcess.kill();
}
});

const tsxProcessResolved = await tsxProcess;
await fixture.rm();

expect(tsxProcessResolved.stdout).not.toMatch(`current date: ${value}`);
expect(tsxProcessResolved.stderr).toBe('');
}, 10_000);
});
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add a test for exclude?

});
});