forked from anomalyco/opencode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwatcher.ts
More file actions
61 lines (57 loc) · 1.68 KB
/
watcher.ts
File metadata and controls
61 lines (57 loc) · 1.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import z from "zod/v4"
import { Bus } from "../bus"
import chokidar from "chokidar"
import { Flag } from "../flag/flag"
import { Instance } from "../project/instance"
import { Log } from "../util/log"
import { FileIgnore } from "./ignore"
import { Config } from "../config/config"
export namespace FileWatcher {
const log = Log.create({ service: "file.watcher" })
export const Event = {
Updated: Bus.event(
"file.watcher.updated",
z.object({
file: z.string(),
event: z.union([z.literal("add"), z.literal("change"), z.literal("unlink")]),
}),
),
}
const state = Instance.state(
async () => {
if (Instance.project.vcs !== "git") return {}
log.info("init")
const cfg = await Config.get()
const ignore = (cfg.watcher?.ignore ?? []).map((v) => new Bun.Glob(v))
const watcher = chokidar.watch(Instance.directory, {
ignoreInitial: true,
ignored: (filepath) => {
return FileIgnore.match(filepath, {
whitelist: [new Bun.Glob("**/.git/{index,logs/HEAD}")],
extra: ignore,
})
},
})
watcher.on("change", (file) => {
Bus.publish(Event.Updated, { file, event: "change" })
})
watcher.on("add", (file) => {
Bus.publish(Event.Updated, { file, event: "add" })
})
watcher.on("unlink", (file) => {
Bus.publish(Event.Updated, { file, event: "unlink" })
})
watcher.on("ready", () => {
log.info("ready")
})
return { watcher }
},
async (state) => {
state.watcher?.close()
},
)
export function init() {
if (!Flag.OPENCODE_EXPERIMENTAL_WATCHER) return
state()
}
}