core: refactor Installation service to use a single consolidated result object
Reorganizes the Installation service implementation by grouping info, method, latest, and upgrade methods into a single result object. This improves code locality and makes the service interface more maintainable. Also adds a clarifying comment explaining why the package manager's resolver is used for version lookups (to ensure registries, mirrors, auth, proxies, and dist-tags match upgrade behavior).
This commit is contained in:
parent
216dd363e8
commit
60ebd074ac
1 changed files with 137 additions and 140 deletions
|
|
@ -132,6 +132,7 @@ export const layer: Layer.Layer<Service, never, HttpClient.HttpClient | ChildPro
|
||||||
Effect.catch(() => Effect.succeed({ code: ChildProcessSpawner.ExitCode(1), stdout: "", stderr: "" })),
|
Effect.catch(() => Effect.succeed({ code: ChildProcessSpawner.ExitCode(1), stdout: "", stderr: "" })),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Use the package manager's resolver so registries, mirrors, auth, proxies, and dist-tags match upgrade behavior.
|
||||||
const viewVersion = Effect.fnUntraced(function* (method: "npm" | "pnpm" | "bun", spec: string) {
|
const viewVersion = Effect.fnUntraced(function* (method: "npm" | "pnpm" | "bun", spec: string) {
|
||||||
const args = method === "bun" ? ["pm", "view", spec, "version", "--json"] : ["view", spec, "version", "--json"]
|
const args = method === "bun" ? ["pm", "view", spec, "version", "--json"] : ["view", spec, "version", "--json"]
|
||||||
const result = yield* run([method, ...args])
|
const result = yield* run([method, ...args])
|
||||||
|
|
@ -173,163 +174,159 @@ export const layer: Layer.Layer<Service, never, HttpClient.HttpClient | ChildPro
|
||||||
Effect.orDie,
|
Effect.orDie,
|
||||||
)
|
)
|
||||||
|
|
||||||
const methodImpl = Effect.fn("Installation.method")(function* () {
|
const result: Interface = {
|
||||||
if (process.execPath.includes(path.join(".opencode", "bin"))) return "curl" as Method
|
|
||||||
if (process.execPath.includes(path.join(".local", "bin"))) return "curl" as Method
|
|
||||||
const exec = process.execPath.toLowerCase()
|
|
||||||
|
|
||||||
const checks: Array<{ name: Method; command: () => Effect.Effect<string> }> = [
|
|
||||||
{ name: "npm", command: () => text(["npm", "list", "-g", "--depth=0"]) },
|
|
||||||
{ name: "yarn", command: () => text(["yarn", "global", "list"]) },
|
|
||||||
{ name: "pnpm", command: () => text(["pnpm", "list", "-g", "--depth=0"]) },
|
|
||||||
{ name: "bun", command: () => text(["bun", "pm", "ls", "-g"]) },
|
|
||||||
{ name: "brew", command: () => text(["brew", "list", "--formula", "opencode"]) },
|
|
||||||
{ name: "scoop", command: () => text(["scoop", "list", "opencode"]) },
|
|
||||||
{ name: "choco", command: () => text(["choco", "list", "--limit-output", "opencode"]) },
|
|
||||||
]
|
|
||||||
|
|
||||||
checks.sort((a, b) => {
|
|
||||||
const aMatches = exec.includes(a.name)
|
|
||||||
const bMatches = exec.includes(b.name)
|
|
||||||
if (aMatches && !bMatches) return -1
|
|
||||||
if (!aMatches && bMatches) return 1
|
|
||||||
return 0
|
|
||||||
})
|
|
||||||
|
|
||||||
for (const check of checks) {
|
|
||||||
const output = yield* check.command()
|
|
||||||
const installedName =
|
|
||||||
check.name === "brew" || check.name === "choco" || check.name === "scoop" ? "opencode" : "opencode-ai"
|
|
||||||
if (output.includes(installedName)) {
|
|
||||||
return check.name
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return "unknown" as Method
|
|
||||||
})
|
|
||||||
|
|
||||||
const latestImpl = Effect.fn("Installation.latest")(function* (installMethod?: Method) {
|
|
||||||
const detectedMethod = installMethod || (yield* methodImpl())
|
|
||||||
|
|
||||||
if (detectedMethod === "brew") {
|
|
||||||
const formula = yield* getBrewFormula()
|
|
||||||
if (formula.includes("/")) {
|
|
||||||
const infoJson = yield* text(["brew", "info", "--json=v2", formula])
|
|
||||||
const info = yield* Schema.decodeUnknownEffect(Schema.fromJsonString(BrewInfoV2))(infoJson)
|
|
||||||
return info.formulae[0].versions.stable
|
|
||||||
}
|
|
||||||
const response = yield* httpOk.execute(
|
|
||||||
HttpClientRequest.get("https://formulae.brew.sh/api/formula/opencode.json").pipe(
|
|
||||||
HttpClientRequest.acceptJson,
|
|
||||||
),
|
|
||||||
)
|
|
||||||
const data = yield* HttpClientResponse.schemaBodyJson(BrewFormula)(response)
|
|
||||||
return data.versions.stable
|
|
||||||
}
|
|
||||||
|
|
||||||
if (detectedMethod === "npm" || detectedMethod === "bun" || detectedMethod === "pnpm") {
|
|
||||||
return yield* viewVersion(detectedMethod, `opencode-ai@${InstallationChannel}`)
|
|
||||||
}
|
|
||||||
|
|
||||||
if (detectedMethod === "choco") {
|
|
||||||
const response = yield* httpOk.execute(
|
|
||||||
HttpClientRequest.get(
|
|
||||||
"https://community.chocolatey.org/api/v2/Packages?$filter=Id%20eq%20%27opencode%27%20and%20IsLatestVersion&$select=Version",
|
|
||||||
).pipe(HttpClientRequest.setHeaders({ Accept: "application/json;odata=verbose" })),
|
|
||||||
)
|
|
||||||
const data = yield* HttpClientResponse.schemaBodyJson(ChocoPackage)(response)
|
|
||||||
return data.d.results[0].Version
|
|
||||||
}
|
|
||||||
|
|
||||||
if (detectedMethod === "scoop") {
|
|
||||||
const response = yield* httpOk.execute(
|
|
||||||
HttpClientRequest.get(
|
|
||||||
"https://raw.githubusercontent.com/ScoopInstaller/Main/master/bucket/opencode.json",
|
|
||||||
).pipe(HttpClientRequest.setHeaders({ Accept: "application/json" })),
|
|
||||||
)
|
|
||||||
const data = yield* HttpClientResponse.schemaBodyJson(ScoopManifest)(response)
|
|
||||||
return data.version
|
|
||||||
}
|
|
||||||
|
|
||||||
const response = yield* httpOk.execute(
|
|
||||||
HttpClientRequest.get("https://api.github.com/repos/anomalyco/opencode/releases/latest").pipe(
|
|
||||||
HttpClientRequest.acceptJson,
|
|
||||||
),
|
|
||||||
)
|
|
||||||
const data = yield* HttpClientResponse.schemaBodyJson(GitHubRelease)(response)
|
|
||||||
return data.tag_name.replace(/^v/, "")
|
|
||||||
}, Effect.orDie)
|
|
||||||
|
|
||||||
const upgradeImpl = Effect.fn("Installation.upgrade")(function* (m: Method, target: string) {
|
|
||||||
let result: { code: ChildProcessSpawner.ExitCode; stdout: string; stderr: string } | undefined
|
|
||||||
switch (m) {
|
|
||||||
case "curl":
|
|
||||||
result = yield* upgradeCurl(target)
|
|
||||||
break
|
|
||||||
case "npm":
|
|
||||||
result = yield* run(["npm", "install", "-g", `opencode-ai@${target}`])
|
|
||||||
break
|
|
||||||
case "pnpm":
|
|
||||||
result = yield* run(["pnpm", "install", "-g", `opencode-ai@${target}`])
|
|
||||||
break
|
|
||||||
case "bun":
|
|
||||||
result = yield* run(["bun", "install", "-g", `opencode-ai@${target}`])
|
|
||||||
break
|
|
||||||
case "brew": {
|
|
||||||
const formula = yield* getBrewFormula()
|
|
||||||
const env = { HOMEBREW_NO_AUTO_UPDATE: "1" }
|
|
||||||
if (formula.includes("/")) {
|
|
||||||
const tap = yield* run(["brew", "tap", "anomalyco/tap"], { env })
|
|
||||||
if (tap.code !== 0) {
|
|
||||||
result = tap
|
|
||||||
break
|
|
||||||
}
|
|
||||||
const repo = yield* text(["brew", "--repo", "anomalyco/tap"])
|
|
||||||
const dir = repo.trim()
|
|
||||||
if (dir) {
|
|
||||||
const pull = yield* run(["git", "pull", "--ff-only"], { cwd: dir, env })
|
|
||||||
if (pull.code !== 0) {
|
|
||||||
result = pull
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
result = yield* run(["brew", "upgrade", formula], { env })
|
|
||||||
break
|
|
||||||
}
|
|
||||||
case "choco":
|
|
||||||
result = yield* run(["choco", "upgrade", "opencode", `--version=${target}`, "-y"])
|
|
||||||
break
|
|
||||||
case "scoop":
|
|
||||||
result = yield* run(["scoop", "install", `opencode@${target}`])
|
|
||||||
break
|
|
||||||
default:
|
|
||||||
return yield* new UpgradeFailedError({ stderr: `Unknown method: ${m}` })
|
|
||||||
}
|
|
||||||
if (!result || result.code !== 0) {
|
|
||||||
const stderr = m === "choco" ? "not running from an elevated command shell" : result?.stderr || ""
|
|
||||||
return yield* new UpgradeFailedError({ stderr })
|
|
||||||
}
|
|
||||||
log.info("upgraded", {
|
|
||||||
method: m,
|
|
||||||
target,
|
|
||||||
stdout: result.stdout,
|
|
||||||
stderr: result.stderr,
|
|
||||||
})
|
|
||||||
yield* text([process.execPath, "--version"])
|
|
||||||
})
|
|
||||||
|
|
||||||
return Service.of({
|
|
||||||
info: Effect.fn("Installation.info")(function* () {
|
info: Effect.fn("Installation.info")(function* () {
|
||||||
return {
|
return {
|
||||||
version: InstallationVersion,
|
version: InstallationVersion,
|
||||||
latest: yield* latestImpl(),
|
latest: yield* result.latest(),
|
||||||
}
|
}
|
||||||
}),
|
}),
|
||||||
method: methodImpl,
|
method: Effect.fn("Installation.method")(function* () {
|
||||||
latest: latestImpl,
|
if (process.execPath.includes(path.join(".opencode", "bin"))) return "curl" as Method
|
||||||
upgrade: upgradeImpl,
|
if (process.execPath.includes(path.join(".local", "bin"))) return "curl" as Method
|
||||||
})
|
const exec = process.execPath.toLowerCase()
|
||||||
|
|
||||||
|
const checks: Array<{ name: Method; command: () => Effect.Effect<string> }> = [
|
||||||
|
{ name: "npm", command: () => text(["npm", "list", "-g", "--depth=0"]) },
|
||||||
|
{ name: "yarn", command: () => text(["yarn", "global", "list"]) },
|
||||||
|
{ name: "pnpm", command: () => text(["pnpm", "list", "-g", "--depth=0"]) },
|
||||||
|
{ name: "bun", command: () => text(["bun", "pm", "ls", "-g"]) },
|
||||||
|
{ name: "brew", command: () => text(["brew", "list", "--formula", "opencode"]) },
|
||||||
|
{ name: "scoop", command: () => text(["scoop", "list", "opencode"]) },
|
||||||
|
{ name: "choco", command: () => text(["choco", "list", "--limit-output", "opencode"]) },
|
||||||
|
]
|
||||||
|
|
||||||
|
checks.sort((a, b) => {
|
||||||
|
const aMatches = exec.includes(a.name)
|
||||||
|
const bMatches = exec.includes(b.name)
|
||||||
|
if (aMatches && !bMatches) return -1
|
||||||
|
if (!aMatches && bMatches) return 1
|
||||||
|
return 0
|
||||||
|
})
|
||||||
|
|
||||||
|
for (const check of checks) {
|
||||||
|
const output = yield* check.command()
|
||||||
|
const installedName =
|
||||||
|
check.name === "brew" || check.name === "choco" || check.name === "scoop" ? "opencode" : "opencode-ai"
|
||||||
|
if (output.includes(installedName)) {
|
||||||
|
return check.name
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return "unknown" as Method
|
||||||
|
}),
|
||||||
|
latest: Effect.fn("Installation.latest")(function* (installMethod?: Method) {
|
||||||
|
const detectedMethod = installMethod || (yield* result.method())
|
||||||
|
|
||||||
|
if (detectedMethod === "brew") {
|
||||||
|
const formula = yield* getBrewFormula()
|
||||||
|
if (formula.includes("/")) {
|
||||||
|
const infoJson = yield* text(["brew", "info", "--json=v2", formula])
|
||||||
|
const info = yield* Schema.decodeUnknownEffect(Schema.fromJsonString(BrewInfoV2))(infoJson)
|
||||||
|
return info.formulae[0].versions.stable
|
||||||
|
}
|
||||||
|
const response = yield* httpOk.execute(
|
||||||
|
HttpClientRequest.get("https://formulae.brew.sh/api/formula/opencode.json").pipe(
|
||||||
|
HttpClientRequest.acceptJson,
|
||||||
|
),
|
||||||
|
)
|
||||||
|
const data = yield* HttpClientResponse.schemaBodyJson(BrewFormula)(response)
|
||||||
|
return data.versions.stable
|
||||||
|
}
|
||||||
|
|
||||||
|
if (detectedMethod === "npm" || detectedMethod === "bun" || detectedMethod === "pnpm") {
|
||||||
|
return yield* viewVersion(detectedMethod, `opencode-ai@${InstallationChannel}`)
|
||||||
|
}
|
||||||
|
|
||||||
|
if (detectedMethod === "choco") {
|
||||||
|
const response = yield* httpOk.execute(
|
||||||
|
HttpClientRequest.get(
|
||||||
|
"https://community.chocolatey.org/api/v2/Packages?$filter=Id%20eq%20%27opencode%27%20and%20IsLatestVersion&$select=Version",
|
||||||
|
).pipe(HttpClientRequest.setHeaders({ Accept: "application/json;odata=verbose" })),
|
||||||
|
)
|
||||||
|
const data = yield* HttpClientResponse.schemaBodyJson(ChocoPackage)(response)
|
||||||
|
return data.d.results[0].Version
|
||||||
|
}
|
||||||
|
|
||||||
|
if (detectedMethod === "scoop") {
|
||||||
|
const response = yield* httpOk.execute(
|
||||||
|
HttpClientRequest.get(
|
||||||
|
"https://raw.githubusercontent.com/ScoopInstaller/Main/master/bucket/opencode.json",
|
||||||
|
).pipe(HttpClientRequest.setHeaders({ Accept: "application/json" })),
|
||||||
|
)
|
||||||
|
const data = yield* HttpClientResponse.schemaBodyJson(ScoopManifest)(response)
|
||||||
|
return data.version
|
||||||
|
}
|
||||||
|
|
||||||
|
const response = yield* httpOk.execute(
|
||||||
|
HttpClientRequest.get("https://api.github.com/repos/anomalyco/opencode/releases/latest").pipe(
|
||||||
|
HttpClientRequest.acceptJson,
|
||||||
|
),
|
||||||
|
)
|
||||||
|
const data = yield* HttpClientResponse.schemaBodyJson(GitHubRelease)(response)
|
||||||
|
return data.tag_name.replace(/^v/, "")
|
||||||
|
}, Effect.orDie),
|
||||||
|
upgrade: Effect.fn("Installation.upgrade")(function* (m: Method, target: string) {
|
||||||
|
let upgradeResult: { code: ChildProcessSpawner.ExitCode; stdout: string; stderr: string } | undefined
|
||||||
|
switch (m) {
|
||||||
|
case "curl":
|
||||||
|
upgradeResult = yield* upgradeCurl(target)
|
||||||
|
break
|
||||||
|
case "npm":
|
||||||
|
upgradeResult = yield* run(["npm", "install", "-g", `opencode-ai@${target}`])
|
||||||
|
break
|
||||||
|
case "pnpm":
|
||||||
|
upgradeResult = yield* run(["pnpm", "install", "-g", `opencode-ai@${target}`])
|
||||||
|
break
|
||||||
|
case "bun":
|
||||||
|
upgradeResult = yield* run(["bun", "install", "-g", `opencode-ai@${target}`])
|
||||||
|
break
|
||||||
|
case "brew": {
|
||||||
|
const formula = yield* getBrewFormula()
|
||||||
|
const env = { HOMEBREW_NO_AUTO_UPDATE: "1" }
|
||||||
|
if (formula.includes("/")) {
|
||||||
|
const tap = yield* run(["brew", "tap", "anomalyco/tap"], { env })
|
||||||
|
if (tap.code !== 0) {
|
||||||
|
upgradeResult = tap
|
||||||
|
break
|
||||||
|
}
|
||||||
|
const repo = yield* text(["brew", "--repo", "anomalyco/tap"])
|
||||||
|
const dir = repo.trim()
|
||||||
|
if (dir) {
|
||||||
|
const pull = yield* run(["git", "pull", "--ff-only"], { cwd: dir, env })
|
||||||
|
if (pull.code !== 0) {
|
||||||
|
upgradeResult = pull
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
upgradeResult = yield* run(["brew", "upgrade", formula], { env })
|
||||||
|
break
|
||||||
|
}
|
||||||
|
case "choco":
|
||||||
|
upgradeResult = yield* run(["choco", "upgrade", "opencode", `--version=${target}`, "-y"])
|
||||||
|
break
|
||||||
|
case "scoop":
|
||||||
|
upgradeResult = yield* run(["scoop", "install", `opencode@${target}`])
|
||||||
|
break
|
||||||
|
default:
|
||||||
|
return yield* new UpgradeFailedError({ stderr: `Unknown method: ${m}` })
|
||||||
|
}
|
||||||
|
if (!upgradeResult || upgradeResult.code !== 0) {
|
||||||
|
const stderr = m === "choco" ? "not running from an elevated command shell" : upgradeResult?.stderr || ""
|
||||||
|
return yield* new UpgradeFailedError({ stderr })
|
||||||
|
}
|
||||||
|
log.info("upgraded", {
|
||||||
|
method: m,
|
||||||
|
target,
|
||||||
|
stdout: upgradeResult.stdout,
|
||||||
|
stderr: upgradeResult.stderr,
|
||||||
|
})
|
||||||
|
yield* text([process.execPath, "--version"])
|
||||||
|
}),
|
||||||
|
}
|
||||||
|
|
||||||
|
return Service.of(result)
|
||||||
}),
|
}),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue