Skip to content

Commit

Permalink
fix(watch): detect file change on windows (#75)
Browse files Browse the repository at this point in the history
  • Loading branch information
privatenumber committed Aug 7, 2022
1 parent fb6c7d5 commit 4f3625f
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/watch/index.ts
@@ -1,5 +1,6 @@
import type { ChildProcess } from 'child_process';
import { fileURLToPath } from 'url';
import path from 'path';
import { command } from 'cleye';
import typeFlag from 'type-flag';
import { watch } from 'chokidar';
Expand Down Expand Up @@ -79,7 +80,7 @@ export const watchCommand = command({
: data.path
);

if (dependencyPath.startsWith('/')) {
if (path.isAbsolute(dependencyPath)) {
// console.log('adding', dependencyPath);
watcher.add(dependencyPath);
}
Expand Down
30 changes: 30 additions & 0 deletions tests/specs/watch.ts
@@ -1,5 +1,6 @@
import path from 'path';
import { testSuite, expect } from 'manten';
import { createFixture } from 'fs-fixture';
import { tsx } from '../utils/tsx';

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

test('watch files for changes', async () => {
const fixture = await createFixture({
'index.js': 'console.log(1)',
});

const tsxProcess = tsx({
args: [
'watch',
path.join(fixture.path, 'index.js'),
],
});

await new Promise<void>((resolve) => {
async function onStdOut(data: Buffer) {
const chunkString = data.toString();

if (chunkString.match('1\n')) {
await fixture.writeFile('index.js', 'console.log(2)');
} else if (chunkString.match('2\n')) {
tsxProcess.kill();
resolve();
}
}

tsxProcess.stdout!.on('data', onStdOut);
tsxProcess.stderr!.on('data', onStdOut);
});
}, 5000);

test('suppresses warnings & clear screen', async () => {
const tsxProcess = tsx({
args: [
Expand Down

0 comments on commit 4f3625f

Please sign in to comment.