watcher.ts 2.7 KB

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