From 342f16120d0cd328ea798efbc9b2ff3d3dfd4e5f Mon Sep 17 00:00:00 2001 From: Hexagon Date: Mon, 29 Apr 2024 19:38:25 +0000 Subject: [PATCH] Fix for bun --- src/ops/watch.test.ts | 4 ++-- src/ops/watch.ts | 38 ++++++++++++++++++++++++++++++++------ 2 files changed, 34 insertions(+), 8 deletions(-) diff --git a/src/ops/watch.test.ts b/src/ops/watch.test.ts index 5e00932..953b92c 100644 --- a/src/ops/watch.test.ts +++ b/src/ops/watch.test.ts @@ -14,7 +14,7 @@ test("FsWatcher watches for file changes", async () => { await writeFile(filePath, "Hello"); }, 1000); for await (const event of watcher.watch(tempdir)) { - if (event.kind === "modify" && filePath == event.paths[0]) { + if (event.kind === "rename" && filePath == event.paths[0]) { events.push(event); break; // Stop watching after the creation event } @@ -23,6 +23,6 @@ test("FsWatcher watches for file changes", async () => { watcher.close(); await rm(tempdir, { recursive: true }); assertEquals(events.length, 1); - assertEquals(events[0].kind, "modify"); + assertEquals(events[0].kind, "rename"); assertEquals(events[0].paths[0], filePath); }); diff --git a/src/ops/watch.ts b/src/ops/watch.ts index 1dbb907..0284393 100644 --- a/src/ops/watch.ts +++ b/src/ops/watch.ts @@ -11,9 +11,9 @@ export type FileSystemEventKind = | "error" | "any" | "access" - | "create" | "modify" | "remove" + | "rename" | "other"; export interface FileSystemEvent { kind: FileSystemEventKind; @@ -29,6 +29,7 @@ export interface Watcher { export function FsWatcher(): Watcher { let denoWatcher: Deno.FsWatcher | undefined; let nodeWatcher: AsyncIterable; + let bunWatcher: AsyncIterable; const ac = new AbortController(); return { async *watch( @@ -39,11 +40,15 @@ export function FsWatcher(): Watcher { if (CurrentRuntime === Runtime.Deno) { denoWatcher = Deno.watchFs(path, options); for await (const event of denoWatcher) { - yield event; + const generatedEvent: FileSystemEvent = { + kind: (event.kind === "create" + ? "rename" + : event.kind) as FileSystemEventKind, + paths: event.paths, + }; + yield generatedEvent; } - } else if ( - CurrentRuntime === Runtime.Node || CurrentRuntime === Runtime.Bun - ) { + } else if (CurrentRuntime === Runtime.Node) { const usedOptions: FileSystemWatcherOptions = options ? options : { recursive: true }; @@ -64,6 +69,27 @@ export function FsWatcher(): Watcher { yield generatedEvent; } } + } else if (CurrentRuntime === Runtime.Bun) { + const usedOptions: FileSystemWatcherOptions = options + ? options + : { recursive: true }; + if (!options?.signal) usedOptions.signal = ac.signal; + bunWatcher = await nodeWatch(path, usedOptions as WatchOptions); + for await (const event of bunWatcher) { + //@ts-ignore cross-runtime + if (event.filename) { + // @ts-ignore cross-runtime + let kind = event.eventType; + if (kind === "change") kind = "modify"; + const generatedEvent = { + //@ts-ignore cross-runtime + kind: kind as FileSystemEventKind, + //@ts-ignore cross-runtime + paths: [join(path, event.filename?.toString())], + }; + yield generatedEvent; + } + } } else { throw new Error("cross/watchFs: Runtime not supported."); } @@ -83,7 +109,7 @@ export function FsWatcher(): Watcher { denoWatcher.close(); } catch (_e) { /* Ignore */ } } - if (nodeWatcher) { + if (nodeWatcher || bunWatcher) { ac?.abort(); } },