Skip to content

Commit

Permalink
Fix for bun
Browse files Browse the repository at this point in the history
  • Loading branch information
Hexagon committed Apr 29, 2024
1 parent 7ff7085 commit 342f161
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 8 deletions.
4 changes: 2 additions & 2 deletions src/ops/watch.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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);
});
38 changes: 32 additions & 6 deletions src/ops/watch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ export type FileSystemEventKind =
| "error"
| "any"
| "access"
| "create"
| "modify"
| "remove"
| "rename"
| "other";
export interface FileSystemEvent {
kind: FileSystemEventKind;
Expand All @@ -29,6 +29,7 @@ export interface Watcher {
export function FsWatcher(): Watcher {
let denoWatcher: Deno.FsWatcher | undefined;
let nodeWatcher: AsyncIterable<unknown>;
let bunWatcher: AsyncIterable<unknown>;
const ac = new AbortController();
return {
async *watch(
Expand All @@ -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 };
Expand All @@ -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.");
}
Expand All @@ -83,7 +109,7 @@ export function FsWatcher(): Watcher {
denoWatcher.close();
} catch (_e) { /* Ignore */ }
}
if (nodeWatcher) {
if (nodeWatcher || bunWatcher) {
ac?.abort();
}
},
Expand Down

0 comments on commit 342f161

Please sign in to comment.