Skip to content

Commit

Permalink
feat(watch): add --ignore flag (#110)
Browse files Browse the repository at this point in the history
Co-authored-by: hiroki osame <[email protected]>
  • Loading branch information
romanlamsal and privatenumber authored Oct 6, 2022
1 parent edde6b1 commit 214a879
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 1 deletion.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,13 @@ tsx watch ./file.ts
All imported files are watched except from the following directories:
`node_modules`, `bower_components`, `vendor`, `dist`, and `.*` (hidden directories).

#### Ignore files from watch

To exclude files from being watched, pass in a path or glob to the `--ignore` flag:
```sh
tsx watch --ignore ./ignore-me.js --ignore ./ignore-me-too.js ./file.ts
```

#### Tips
- Press <kbd>Return</kbd> to manually rerun
- Pass in `--clear-screen=false` to disable clearing the screen on rerun
Expand Down
9 changes: 8 additions & 1 deletion src/watch/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,11 @@ const flags = {
description: 'Clearing the screen on rerun',
default: true,
},
};
ignore: {
type: [String],
description: 'Paths & globs to exclude from being watched',
},
} as const;

export const watchCommand = command({
name: 'watch',
Expand All @@ -47,6 +51,7 @@ export const watchCommand = command({
noCache: argv.flags.noCache,
tsconfigPath: argv.flags.tsconfig,
clearScreen: argv.flags.clearScreen,
ignore: argv.flags.ignore,
ipc: true,
};

Expand Down Expand Up @@ -123,6 +128,8 @@ export const watchCommand = command({

// Distribution files
'**/dist/**',

...options.ignore,
],
ignorePermissionErrors: true,
},
Expand Down
54 changes: 54 additions & 0 deletions tests/specs/watch.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import path from 'path';
import { setTimeout } from 'timers/promises';
import { testSuite, expect } from 'manten';
import { createFixture } from 'fs-fixture';
import { tsx } from '../utils/tsx';
Expand Down Expand Up @@ -141,5 +142,58 @@ export default testSuite(async ({ describe }, fixturePath: string) => {
// await tsxProcess;
// }, 2000);
});

describe('ignore', ({ test }) => {
test('file path & glob', async () => {
const entryFile = 'index.js';
const fileA = 'file-a.js';
const fileB = 'directory/file-b.js';
let value = Date.now();

const fixture = await createFixture({
[entryFile]: `
import valueA from './${fileA}'
import valueB from './${fileB}'
console.log(valueA, valueB)
`.trim(),
[fileA]: `export default ${value}`,
[fileB]: `export default ${value}`,
});

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

tsxProcess.stdout!.on('data', async (data: Buffer) => {
const chunkString = data.toString();
if (chunkString === `${value} ${value}\n`) {
value = Date.now();
await Promise.all([
fixture.writeFile(fileA, `export default ${value}`),
fixture.writeFile(fileB, `export default ${value}`),
]);

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(`${value} ${value}`);
expect(tsxProcessResolved.stderr).toBe('');
}, 5000);
});
});
});

0 comments on commit 214a879

Please sign in to comment.