diff --git a/src/watch/index.ts b/src/watch/index.ts index 4579137c..aaba93ba 100644 --- a/src/watch/index.ts +++ b/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'; @@ -79,7 +80,7 @@ export const watchCommand = command({ : data.path ); - if (dependencyPath.startsWith('/')) { + if (path.isAbsolute(dependencyPath)) { // console.log('adding', dependencyPath); watcher.add(dependencyPath); } diff --git a/tests/specs/watch.ts b/tests/specs/watch.ts index 3ba42dd7..aab5f201 100644 --- a/tests/specs/watch.ts +++ b/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) => { @@ -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((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: [