Răsfoiți Sursa

flag for disabling file watcher

Dax Raad 7 luni în urmă
părinte
comite
204801052a
2 a modificat fișierele cu 34 adăugiri și 30 ștergeri
  1. 33 30
      packages/opencode/src/file/watch.ts
  2. 1 0
      packages/opencode/src/flag/flag.ts

+ 33 - 30
packages/opencode/src/file/watch.ts

@@ -3,6 +3,7 @@ import { Bus } from "../bus"
 import fs from "fs"
 import fs from "fs"
 import { App } from "../app/app"
 import { App } from "../app/app"
 import { Log } from "../util/log"
 import { Log } from "../util/log"
+import { Flag } from "../flag/flag"
 
 
 export namespace FileWatcher {
 export namespace FileWatcher {
   const log = Log.create({ service: "file.watcher" })
   const log = Log.create({ service: "file.watcher" })
@@ -16,37 +17,39 @@ export namespace FileWatcher {
       }),
       }),
     ),
     ),
   }
   }
+  const state = App.state(
+    "file.watcher",
+    () => {
+      const app = App.use()
+      try {
+        const watcher = fs.watch(
+          app.info.path.cwd,
+          { recursive: true },
+          (event, file) => {
+            log.info("change", { file, event })
+            if (!file) return
+            // for some reason async local storage is lost here
+            // https://github.com/oven-sh/bun/issues/20754
+            App.provideExisting(app, async () => {
+              Bus.publish(Event.Updated, {
+                file,
+                event,
+              })
+            })
+          },
+        )
+        return { watcher }
+      } catch {
+        return {}
+      }
+    },
+    async (state) => {
+      state.watcher?.close()
+    },
+  )
 
 
   export function init() {
   export function init() {
-    App.state(
-      "file.watcher",
-      () => {
-        const app = App.use()
-        try {
-          const watcher = fs.watch(
-            app.info.path.cwd,
-            { recursive: true },
-            (event, file) => {
-              log.info("change", { file, event })
-              if (!file) return
-              // for some reason async local storage is lost here
-              // https://github.com/oven-sh/bun/issues/20754
-              App.provideExisting(app, async () => {
-                Bus.publish(Event.Updated, {
-                  file,
-                  event,
-                })
-              })
-            },
-          )
-          return { watcher }
-        } catch {
-          return {}
-        }
-      },
-      async (state) => {
-        state.watcher?.close()
-      },
-    )()
+    if (Flag.OPENCODE_DISABLE_WATCHER) return
+    state()
   }
   }
 }
 }

+ 1 - 0
packages/opencode/src/flag/flag.ts

@@ -1,5 +1,6 @@
 export namespace Flag {
 export namespace Flag {
   export const OPENCODE_AUTO_SHARE = truthy("OPENCODE_AUTO_SHARE")
   export const OPENCODE_AUTO_SHARE = truthy("OPENCODE_AUTO_SHARE")
+  export const OPENCODE_DISABLE_WATCHER = truthy("OPENCODE_DISABLE_WATCHER")
 
 
   function truthy(key: string) {
   function truthy(key: string) {
     const value = process.env[key]?.toLowerCase()
     const value = process.env[key]?.toLowerCase()