|
1 | 1 | import z from "zod" |
2 | 2 | import { Bus } from "../bus" |
3 | | -import { Flag } from "../flag/flag" |
4 | 3 | import { Instance } from "../project/instance" |
5 | 4 | import { Log } from "../util/log" |
6 | 5 | import { FileIgnore } from "./ignore" |
7 | 6 | import { Config } from "../config/config" |
8 | 7 | // @ts-ignore |
9 | 8 | import { createWrapper } from "@parcel/watcher/wrapper" |
10 | 9 | import { lazy } from "@/util/lazy" |
| 10 | +import type ParcelWatcher from "@parcel/watcher" |
11 | 11 |
|
12 | 12 | export namespace FileWatcher { |
13 | 13 | const log = Log.create({ service: "file.watcher" }) |
@@ -44,32 +44,46 @@ export namespace FileWatcher { |
44 | 44 | return {} |
45 | 45 | } |
46 | 46 | log.info("watcher backend", { platform: process.platform, backend }) |
47 | | - const sub = await watcher().subscribe( |
48 | | - Instance.directory, |
49 | | - (err, evts) => { |
50 | | - if (err) return |
51 | | - for (const evt of evts) { |
52 | | - log.info("event", evt) |
53 | | - if (evt.type === "create") Bus.publish(Event.Updated, { file: evt.path, event: "add" }) |
54 | | - if (evt.type === "update") Bus.publish(Event.Updated, { file: evt.path, event: "change" }) |
55 | | - if (evt.type === "delete") Bus.publish(Event.Updated, { file: evt.path, event: "unlink" }) |
56 | | - } |
57 | | - }, |
58 | | - { |
59 | | - ignore: [...FileIgnore.PATTERNS, ...(cfg.watcher?.ignore ?? [])], |
| 47 | + const subscribe: ParcelWatcher.SubscribeCallback = (err, evts) => { |
| 48 | + if (err) return |
| 49 | + for (const evt of evts) { |
| 50 | + log.info("event", evt) |
| 51 | + if (evt.type === "create") Bus.publish(Event.Updated, { file: evt.path, event: "add" }) |
| 52 | + if (evt.type === "update") Bus.publish(Event.Updated, { file: evt.path, event: "change" }) |
| 53 | + if (evt.type === "delete") Bus.publish(Event.Updated, { file: evt.path, event: "unlink" }) |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + const subs = [] |
| 58 | + const cfgIgnores = cfg.watcher?.ignore ?? [] |
| 59 | + |
| 60 | + subs.push( |
| 61 | + await watcher().subscribe(Instance.directory, subscribe, { |
| 62 | + ignore: [...FileIgnore.PATTERNS, ...cfgIgnores], |
60 | 63 | backend, |
61 | | - }, |
| 64 | + }), |
62 | 65 | ) |
63 | | - return { sub } |
| 66 | + |
| 67 | + const vcsDir = Instance.project.vcsDir |
| 68 | + if (vcsDir && !cfgIgnores.includes(".git") && !cfgIgnores.includes(vcsDir)) { |
| 69 | + subs.push( |
| 70 | + await watcher().subscribe(vcsDir, subscribe, { |
| 71 | + ignore: ["hooks", "info", "logs", "objects", "refs", "worktrees", "modules", "lfs"], |
| 72 | + backend, |
| 73 | + }), |
| 74 | + ) |
| 75 | + } |
| 76 | + |
| 77 | + return { subs } |
64 | 78 | }, |
65 | 79 | async (state) => { |
66 | | - if (!state.sub) return |
67 | | - await state.sub?.unsubscribe() |
| 80 | + if (!state.subs) return |
| 81 | + await Promise.all(state.subs.map((sub) => sub?.unsubscribe())) |
68 | 82 | }, |
69 | 83 | ) |
70 | 84 |
|
71 | 85 | export function init() { |
72 | | - if (!Flag.OPENCODE_EXPERIMENTAL_WATCHER) return |
| 86 | + // if (!Flag.OPENCODE_EXPERIMENTAL_WATCHER) return |
73 | 87 | state() |
74 | 88 | } |
75 | 89 | } |
0 commit comments