Browse Source

autoupgrade

Dax Raad 8 tháng trước cách đây
mục cha
commit
3fe163416d
2 tập tin đã thay đổi với 35 bổ sung1 xóa
  1. 24 1
      packages/opencode/src/global/config.ts
  2. 11 0
      packages/opencode/src/index.ts

+ 24 - 1
packages/opencode/src/global/config.ts

@@ -1 +1,24 @@
-export namespace GlobalConfig {}
+import { z } from "zod"
+import { Global } from "."
+import { lazy } from "../util/lazy"
+import path from "path"
+
+export namespace GlobalConfig {
+  export const Info = z.object({
+    autoupdate: z.boolean().optional(),
+    provider: z.string().optional(),
+    model: z.string().optional(),
+  })
+  export type Info = z.infer<typeof Info>
+
+  export const get = lazy(async () => {
+    const toml = await import(path.join(Global.Path.config, "config"), {
+      with: {
+        type: "toml",
+      },
+    })
+      .then((mod) => mod.default)
+      .catch(() => ({}))
+    return Info.parse(toml)
+  })
+}

+ 11 - 0
packages/opencode/src/index.ts

@@ -16,6 +16,17 @@ import { AuthCommand, AuthLoginCommand } from "./cli/cmd/auth"
 import { UpgradeCommand } from "./cli/cmd/upgrade"
 import { Provider } from "./provider/provider"
 import { UI } from "./cli/ui"
+import { GlobalConfig } from "./global/config"
+import { Installation } from "./installation"
+;(async () => {
+  if (Installation.VERSION === "dev") return
+  const config = await GlobalConfig.get()
+  if (config.autoupdate === false) return
+  const latest = await Installation.latest()
+  if (Installation.VERSION === latest) return
+  const method = await Installation.method()
+  await Installation.upgrade(method, latest).catch(() => {})
+})()
 
 const cli = yargs(hideBin(process.argv))
   .scriptName("opencode")