2
0

watcher.ts 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. import z from "zod"
  2. import { Bus } from "../bus"
  3. import { Flag } from "../flag/flag"
  4. import { Instance } from "../project/instance"
  5. import { Log } from "../util/log"
  6. import { FileIgnore } from "./ignore"
  7. import { Config } from "../config/config"
  8. // @ts-ignore
  9. import { createWrapper } from "@parcel/watcher/wrapper"
  10. import { lazy } from "@/util/lazy"
  11. export namespace FileWatcher {
  12. const log = Log.create({ service: "file.watcher" })
  13. export const Event = {
  14. Updated: Bus.event(
  15. "file.watcher.updated",
  16. z.object({
  17. file: z.string(),
  18. event: z.union([z.literal("add"), z.literal("change"), z.literal("unlink")]),
  19. }),
  20. ),
  21. }
  22. const watcher = lazy(() => {
  23. const binding = require(
  24. `@parcel/watcher-${process.platform}-${process.arch}${process.platform === "linux" ? "-glibc" : ""}`,
  25. )
  26. return createWrapper(binding) as typeof import("@parcel/watcher")
  27. })
  28. const state = Instance.state(
  29. async () => {
  30. if (Instance.project.vcs !== "git") return {}
  31. log.info("init")
  32. const cfg = await Config.get()
  33. const backend = (() => {
  34. if (process.platform === "win32") return "windows"
  35. if (process.platform === "darwin") return "fs-events"
  36. if (process.platform === "linux") return "inotify"
  37. })()
  38. if (!backend) {
  39. log.error("watcher backend not supported", { platform: process.platform })
  40. return {}
  41. }
  42. log.info("watcher backend", { platform: process.platform, backend })
  43. const sub = await watcher().subscribe(
  44. Instance.directory,
  45. (err, evts) => {
  46. if (err) return
  47. for (const evt of evts) {
  48. log.info("event", evt)
  49. if (evt.type === "create") Bus.publish(Event.Updated, { file: evt.path, event: "add" })
  50. if (evt.type === "update") Bus.publish(Event.Updated, { file: evt.path, event: "change" })
  51. if (evt.type === "delete") Bus.publish(Event.Updated, { file: evt.path, event: "unlink" })
  52. }
  53. },
  54. {
  55. ignore: [...FileIgnore.PATTERNS, ...(cfg.watcher?.ignore ?? [])],
  56. backend,
  57. },
  58. )
  59. return { sub }
  60. },
  61. async (state) => {
  62. if (!state.sub) return
  63. await state.sub?.unsubscribe()
  64. },
  65. )
  66. export function init() {
  67. if (!Flag.OPENCODE_EXPERIMENTAL_WATCHER) return
  68. state()
  69. }
  70. }