@@ -11,9 +11,9 @@ export type FileSystemEventKind =
1111 | "error"
1212 | "any"
1313 | "access"
14- | "create"
1514 | "modify"
1615 | "remove"
16+ | "rename"
1717 | "other" ;
1818export interface FileSystemEvent {
1919 kind : FileSystemEventKind ;
@@ -29,6 +29,7 @@ export interface Watcher {
2929export 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