11import z from "zod/v4"
22import { Bus } from "../bus"
3- import chokidar from "chokidar"
43import { Flag } from "../flag/flag"
54import { Instance } from "../project/instance"
65import { Log } from "../util/log"
76import { FileIgnore } from "./ignore"
87import { Config } from "../config/config"
8+ // @ts -ignore
9+ import { createWrapper } from "@parcel/watcher/wrapper"
10+ import { lazy } from "@/util/lazy"
911
1012export namespace FileWatcher {
1113 const log = Log . create ( { service : "file.watcher" } )
@@ -20,37 +22,38 @@ export namespace FileWatcher {
2022 ) ,
2123 }
2224
25+ const watcher = lazy ( ( ) => {
26+ const binding = require (
27+ `@parcel/watcher-${ process . platform } -${ process . arch } ${ process . platform === "linux" ? "-glibc" : "" } ` ,
28+ )
29+ return createWrapper ( binding ) as typeof import ( "@parcel/watcher" )
30+ } )
31+
2332 const state = Instance . state (
2433 async ( ) => {
2534 if ( Instance . project . vcs !== "git" ) return { }
2635 log . info ( "init" )
2736 const cfg = await Config . get ( )
28- const ignore = ( cfg . watcher ?. ignore ?? [ ] ) . map ( ( v ) => new Bun . Glob ( v ) )
29- const watcher = chokidar . watch ( Instance . directory , {
30- ignoreInitial : true ,
31- ignored : ( filepath ) => {
32- return FileIgnore . match ( filepath , {
33- whitelist : [ new Bun . Glob ( "**/.git/{index,logs/HEAD}" ) ] ,
34- extra : ignore ,
35- } )
37+ const sub = await watcher ( ) . subscribe (
38+ Instance . directory ,
39+ ( err , evts ) => {
40+ if ( err ) return
41+ for ( const evt of evts ) {
42+ log . info ( "event" , evt )
43+ if ( evt . type === "create" ) Bus . publish ( Event . Updated , { file : evt . path , event : "add" } )
44+ if ( evt . type === "update" ) Bus . publish ( Event . Updated , { file : evt . path , event : "change" } )
45+ if ( evt . type === "delete" ) Bus . publish ( Event . Updated , { file : evt . path , event : "unlink" } )
46+ }
47+ } ,
48+ {
49+ ignore : [ ...FileIgnore . PATTERNS , ...( cfg . watcher ?. ignore ?? [ ] ) ] ,
50+ backend : "inotify" ,
3651 } ,
37- } )
38- watcher . on ( "change" , ( file ) => {
39- Bus . publish ( Event . Updated , { file, event : "change" } )
40- } )
41- watcher . on ( "add" , ( file ) => {
42- Bus . publish ( Event . Updated , { file, event : "add" } )
43- } )
44- watcher . on ( "unlink" , ( file ) => {
45- Bus . publish ( Event . Updated , { file, event : "unlink" } )
46- } )
47- watcher . on ( "ready" , ( ) => {
48- log . info ( "ready" )
49- } )
50- return { watcher }
52+ )
53+ return { sub }
5154 } ,
5255 async ( state ) => {
53- state . watcher ?. close ( )
56+ state . sub ?. unsubscribe ( )
5457 } ,
5558 )
5659
0 commit comments