Skip to content

Commit 4f3625f

Browse files
authoredAug 7, 2022
fix(watch): detect file change on windows (#75)
1 parent fb6c7d5 commit 4f3625f

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed
 

‎src/watch/index.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type { ChildProcess } from 'child_process';
22
import { fileURLToPath } from 'url';
3+
import path from 'path';
34
import { command } from 'cleye';
45
import typeFlag from 'type-flag';
56
import { watch } from 'chokidar';
@@ -79,7 +80,7 @@ export const watchCommand = command({
7980
: data.path
8081
);
8182

82-
if (dependencyPath.startsWith('/')) {
83+
if (path.isAbsolute(dependencyPath)) {
8384
// console.log('adding', dependencyPath);
8485
watcher.add(dependencyPath);
8586
}

‎tests/specs/watch.ts

+30
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import path from 'path';
22
import { testSuite, expect } from 'manten';
3+
import { createFixture } from 'fs-fixture';
34
import { tsx } from '../utils/tsx';
45

56
export default testSuite(async ({ describe }, fixturePath: string) => {
@@ -12,6 +13,35 @@ export default testSuite(async ({ describe }, fixturePath: string) => {
1213
expect(tsxProcess.stderr).toMatch('Error: Missing required parameter "script path"');
1314
});
1415

16+
test('watch files for changes', async () => {
17+
const fixture = await createFixture({
18+
'index.js': 'console.log(1)',
19+
});
20+
21+
const tsxProcess = tsx({
22+
args: [
23+
'watch',
24+
path.join(fixture.path, 'index.js'),
25+
],
26+
});
27+
28+
await new Promise<void>((resolve) => {
29+
async function onStdOut(data: Buffer) {
30+
const chunkString = data.toString();
31+
32+
if (chunkString.match('1\n')) {
33+
await fixture.writeFile('index.js', 'console.log(2)');
34+
} else if (chunkString.match('2\n')) {
35+
tsxProcess.kill();
36+
resolve();
37+
}
38+
}
39+
40+
tsxProcess.stdout!.on('data', onStdOut);
41+
tsxProcess.stderr!.on('data', onStdOut);
42+
});
43+
}, 5000);
44+
1545
test('suppresses warnings & clear screen', async () => {
1646
const tsxProcess = tsx({
1747
args: [

0 commit comments

Comments
 (0)
Please sign in to comment.