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

fix(watch): detect file change on windows #75

Merged
merged 3 commits into from Aug 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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