watcher.ts 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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")
  51. Bus.publish(Event.Updated, { file: evt.path, event: "change" })
  52. if (evt.type === "delete")
  53. Bus.publish(Event.Updated, { file: evt.path, event: "unlink" })
  54. }
  55. },
  56. {
  57. ignore: [...FileIgnore.PATTERNS, ...(cfg.watcher?.ignore ?? [])],
  58. backend,
  59. },
  60. )
  61. return { sub }
  62. },
  63. async (state) => {
  64. if (!state.sub) return
  65. await state.sub?.unsubscribe()
  66. },
  67. )
  68. export function init() {
  69. if (!Flag.OPENCODE_EXPERIMENTAL_WATCHER) return
  70. state()
  71. }
  72. }