|
1 | 1 | import { Global } from "@opencode-ai/util/global" |
2 | 2 | import { AppProcess } from "@opencode-ai/util/process" |
3 | 3 | import { OPENCODE_CHANNEL, OPENCODE_LOCAL, OPENCODE_VERSION } from "../version" |
4 | | -import { Context, Duration, Effect, FileSystem, Layer } from "effect" |
| 4 | +import { Context, Duration, Effect, FileSystem, Layer, Ref } from "effect" |
5 | 5 | import { ChildProcess } from "effect/unstable/process" |
6 | 6 | import { parse, type ParseError } from "jsonc-parser" |
7 | 7 | import path from "node:path" |
@@ -38,6 +38,7 @@ export const layer = Layer.effect( |
38 | 38 | const fs = yield* FileSystem.FileSystem |
39 | 39 | const global = yield* Global.Service |
40 | 40 | const appProcess = yield* AppProcess.Service |
| 41 | + const installed = yield* Ref.make(OPENCODE_VERSION) |
41 | 42 | const channel = OPENCODE_CHANNEL.replace(/[^a-zA-Z0-9._-]/g, "-") |
42 | 43 |
|
43 | 44 | const readPolicy = Effect.fnUntraced(function* () { |
@@ -112,18 +113,20 @@ export const layer = Layer.effect( |
112 | 113 |
|
113 | 114 | const upgrade = Effect.fnUntraced(function* (method: Method, version: string) { |
114 | 115 | const target = `${packageName}@${version}` |
115 | | - const commands: Record<Exclude<Method, "bun" | "curl">, string[]> = { |
116 | | - npm: ["npm", "install", "--global", target], |
| 116 | + const commands: Record<Exclude<Method, "bun" | "curl" | "npm">, string[]> = { |
117 | 117 | pnpm: ["pnpm", "add", "--global", `--allow-build=${packageName}`, target], |
118 | 118 | yarn: ["yarn", "global", "add", target], |
119 | 119 | } |
120 | 120 | const result = yield* Effect.scoped( |
121 | 121 | Effect.gen(function* () { |
122 | | - if (method === "bun") { |
123 | | - // Bun does not prune old versions from its shared package cache. |
| 122 | + if (method === "bun" || method === "npm") { |
124 | 123 | yield* fs.makeDirectory(global.cache, { recursive: true }) |
125 | 124 | const cache = yield* fs.makeTempDirectoryScoped({ directory: global.cache, prefix: "update-" }) |
126 | | - return yield* run(["bun", "install", "--global", "--trust", "--cache-dir", cache, target], "5 minutes") |
| 125 | + const command = |
| 126 | + method === "bun" |
| 127 | + ? ["bun", "install", "--global", "--trust", "--cache-dir", cache, target] |
| 128 | + : ["npm", "install", "--global", "--cache", cache, target] |
| 129 | + return yield* run(command, "5 minutes") |
127 | 130 | } |
128 | 131 | if (method === "curl") { |
129 | 132 | yield* fs.makeDirectory(global.cache, { recursive: true }) |
@@ -156,18 +159,19 @@ export const layer = Layer.effect( |
156 | 159 |
|
157 | 160 | return yield* Effect.gen(function* () { |
158 | 161 | const version = yield* latest() |
| 162 | + const current = yield* Ref.get(installed) |
159 | 163 | yield* Effect.logInfo("update check", { |
160 | | - current: OPENCODE_VERSION, |
| 164 | + current, |
161 | 165 | latest: version, |
162 | 166 | }) |
163 | | - const next = action(OPENCODE_VERSION, version, policy) |
| 167 | + const next = action(OPENCODE_VERSION, version, policy, current) |
164 | 168 | if (next === "none") return yield* Effect.logInfo("update check done", { action: "up-to-date" }) |
165 | | - if (next === "notify") |
166 | | - return yield* Effect.logInfo("OpenCode update available", { current: OPENCODE_VERSION, latest: version }) |
| 169 | + if (next === "notify") return yield* Effect.logInfo("OpenCode update available", { current, latest: version }) |
167 | 170 | const detected = yield* method() |
168 | 171 | if (!detected) return yield* Effect.logWarning("automatic update skipped: installation method not found") |
169 | 172 | yield* upgrade(detected, version) |
170 | | - yield* Effect.logInfo("updated OpenCode", { from: OPENCODE_VERSION, to: version, method: detected }) |
| 173 | + yield* Ref.set(installed, version) |
| 174 | + yield* Effect.logInfo("updated OpenCode", { from: current, to: version, method: detected }) |
171 | 175 | }) |
172 | 176 | }, |
173 | 177 | Effect.catchCause((cause) => Effect.logWarning("automatic update failed", { cause })), |
|
0 commit comments