Skip to content

Commit 342f161

Browse files
committed
Fix for bun
1 parent 7ff7085 commit 342f161

2 files changed

Lines changed: 34 additions & 8 deletions

File tree

src/ops/watch.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ test("FsWatcher watches for file changes", async () => {
1414
await writeFile(filePath, "Hello");
1515
}, 1000);
1616
for await (const event of watcher.watch(tempdir)) {
17-
if (event.kind === "modify" && filePath == event.paths[0]) {
17+
if (event.kind === "rename" && filePath == event.paths[0]) {
1818
events.push(event);
1919
break; // Stop watching after the creation event
2020
}
@@ -23,6 +23,6 @@ test("FsWatcher watches for file changes", async () => {
2323
watcher.close();
2424
await rm(tempdir, { recursive: true });
2525
assertEquals(events.length, 1);
26-
assertEquals(events[0].kind, "modify");
26+
assertEquals(events[0].kind, "rename");
2727
assertEquals(events[0].paths[0], filePath);
2828
});

src/ops/watch.ts

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ export type FileSystemEventKind =
1111
| "error"
1212
| "any"
1313
| "access"
14-
| "create"
1514
| "modify"
1615
| "remove"
16+
| "rename"
1717
| "other";
1818
export interface FileSystemEvent {
1919
kind: FileSystemEventKind;
@@ -29,6 +29,7 @@ export interface Watcher {
2929
export function FsWatcher(): Watcher {
3030
let denoWatcher: Deno.FsWatcher | undefined;
3131
let nodeWatcher: AsyncIterable<unknown>;
32+
let bunWatcher: AsyncIterable<unknown>;
3233
const ac = new AbortController();
3334
return {
3435
async *watch(
@@ -39,11 +40,15 @@ export function FsWatcher(): Watcher {
3940
if (CurrentRuntime === Runtime.Deno) {
4041
denoWatcher = Deno.watchFs(path, options);
4142
for await (const event of denoWatcher) {
42-
yield event;
43+
const generatedEvent: FileSystemEvent = {
44+
kind: (event.kind === "create"
45+
? "rename"
46+
: event.kind) as FileSystemEventKind,
47+
paths: event.paths,
48+
};
49+
yield generatedEvent;
4350
}
44-
} else if (
45-
CurrentRuntime === Runtime.Node || CurrentRuntime === Runtime.Bun
46-
) {
51+
} else if (CurrentRuntime === Runtime.Node) {
4752
const usedOptions: FileSystemWatcherOptions = options
4853
? options
4954
: { recursive: true };
@@ -64,6 +69,27 @@ export function FsWatcher(): Watcher {
6469
yield generatedEvent;
6570
}
6671
}
72+
} else if (CurrentRuntime === Runtime.Bun) {
73+
const usedOptions: FileSystemWatcherOptions = options
74+
? options
75+
: { recursive: true };
76+
if (!options?.signal) usedOptions.signal = ac.signal;
77+
bunWatcher = await nodeWatch(path, usedOptions as WatchOptions);
78+
for await (const event of bunWatcher) {
79+
//@ts-ignore cross-runtime
80+
if (event.filename) {
81+
// @ts-ignore cross-runtime
82+
let kind = event.eventType;
83+
if (kind === "change") kind = "modify";
84+
const generatedEvent = {
85+
//@ts-ignore cross-runtime
86+
kind: kind as FileSystemEventKind,
87+
//@ts-ignore cross-runtime
88+
paths: [join(path, event.filename?.toString())],
89+
};
90+
yield generatedEvent;
91+
}
92+
}
6793
} else {
6894
throw new Error("cross/watchFs: Runtime not supported.");
6995
}
@@ -83,7 +109,7 @@ export function FsWatcher(): Watcher {
83109
denoWatcher.close();
84110
} catch (_e) { /* Ignore */ }
85111
}
86-
if (nodeWatcher) {
112+
if (nodeWatcher || bunWatcher) {
87113
ac?.abort();
88114
}
89115
},

0 commit comments

Comments
 (0)