2025-06-15 02:05:41 +00:00
|
|
|
import type { Argv } from "yargs"
|
|
|
|
|
import { UI } from "../ui"
|
|
|
|
|
import * as prompts from "@clack/prompts"
|
2026-04-11 03:26:16 +00:00
|
|
|
import { AppRuntime } from "@/effect/app-runtime"
|
2025-06-17 04:07:17 +00:00
|
|
|
import { Installation } from "../../installation"
|
2026-04-16 06:03:03 +00:00
|
|
|
import { InstallationVersion } from "../../installation/version"
|
2025-06-15 02:05:41 +00:00
|
|
|
|
|
|
|
|
export const UpgradeCommand = {
|
|
|
|
|
command: "upgrade [target]",
|
2025-06-23 17:00:21 +00:00
|
|
|
describe: "upgrade opencode to the latest or a specific version",
|
2025-06-15 02:05:41 +00:00
|
|
|
builder: (yargs: Argv) => {
|
2025-06-21 00:48:23 +00:00
|
|
|
return yargs
|
|
|
|
|
.positional("target", {
|
2025-06-23 17:00:21 +00:00
|
|
|
describe: "version to upgrade to, for ex '0.1.48' or 'v0.1.48'",
|
2025-06-21 00:48:23 +00:00
|
|
|
type: "string",
|
|
|
|
|
})
|
|
|
|
|
.option("method", {
|
|
|
|
|
alias: "m",
|
2025-06-23 17:00:21 +00:00
|
|
|
describe: "installation method to use",
|
2025-06-21 00:48:23 +00:00
|
|
|
type: "string",
|
2026-01-14 15:39:01 +00:00
|
|
|
choices: ["curl", "npm", "pnpm", "bun", "brew", "choco", "scoop"],
|
2025-06-21 00:48:23 +00:00
|
|
|
})
|
2025-06-15 02:05:41 +00:00
|
|
|
},
|
2025-06-21 00:48:23 +00:00
|
|
|
handler: async (args: { target?: string; method?: string }) => {
|
2025-06-15 02:05:41 +00:00
|
|
|
UI.empty()
|
|
|
|
|
UI.println(UI.logo(" "))
|
|
|
|
|
UI.empty()
|
2025-06-15 17:10:58 +00:00
|
|
|
prompts.intro("Upgrade")
|
2026-04-11 03:26:16 +00:00
|
|
|
const detectedMethod = await AppRuntime.runPromise(Installation.Service.use((svc) => svc.method()))
|
2025-06-21 00:48:23 +00:00
|
|
|
const method = (args.method as Installation.Method) ?? detectedMethod
|
2025-06-17 04:07:17 +00:00
|
|
|
if (method === "unknown") {
|
2025-10-14 18:18:40 +00:00
|
|
|
prompts.log.error(`opencode is installed to ${process.execPath} and may be managed by a package manager`)
|
|
|
|
|
const install = await prompts.select({
|
|
|
|
|
message: "Install anyways?",
|
|
|
|
|
options: [
|
|
|
|
|
{ label: "Yes", value: true },
|
|
|
|
|
{ label: "No", value: false },
|
|
|
|
|
],
|
|
|
|
|
initialValue: false,
|
|
|
|
|
})
|
|
|
|
|
if (!install) {
|
|
|
|
|
prompts.outro("Done")
|
|
|
|
|
return
|
|
|
|
|
}
|
2025-06-15 02:05:41 +00:00
|
|
|
}
|
2025-06-21 00:48:23 +00:00
|
|
|
prompts.log.info("Using method: " + method)
|
2026-04-11 03:26:16 +00:00
|
|
|
const target = args.target
|
|
|
|
|
? args.target.replace(/^v/, "")
|
|
|
|
|
: await AppRuntime.runPromise(Installation.Service.use((svc) => svc.latest()))
|
2025-07-07 03:36:59 +00:00
|
|
|
|
2026-04-16 06:03:03 +00:00
|
|
|
if (InstallationVersion === target) {
|
2025-07-07 19:53:43 +00:00
|
|
|
prompts.log.warn(`opencode upgrade skipped: ${target} is already installed`)
|
2025-07-07 03:36:59 +00:00
|
|
|
prompts.outro("Done")
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-16 06:03:03 +00:00
|
|
|
prompts.log.info(`From ${InstallationVersion} → ${target}`)
|
2025-06-17 04:07:17 +00:00
|
|
|
const spinner = prompts.spinner()
|
|
|
|
|
spinner.start("Upgrading...")
|
2026-04-11 03:26:16 +00:00
|
|
|
const err = await AppRuntime.runPromise(Installation.Service.use((svc) => svc.upgrade(method, target))).catch(
|
|
|
|
|
(err) => err,
|
|
|
|
|
)
|
2025-06-17 04:07:17 +00:00
|
|
|
if (err) {
|
2025-08-19 05:14:50 +00:00
|
|
|
spinner.stop("Upgrade failed", 1)
|
2026-01-14 15:39:01 +00:00
|
|
|
if (err instanceof Installation.UpgradeFailedError) {
|
|
|
|
|
// necessary because choco only allows install/upgrade in elevated terminals
|
2026-03-20 16:10:33 +00:00
|
|
|
if (method === "choco" && err.stderr.includes("not running from an elevated command shell")) {
|
2026-01-14 15:39:01 +00:00
|
|
|
prompts.log.error("Please run the terminal as Administrator and try again")
|
|
|
|
|
} else {
|
2026-03-20 16:10:33 +00:00
|
|
|
prompts.log.error(err.stderr)
|
2026-01-14 15:39:01 +00:00
|
|
|
}
|
|
|
|
|
} else if (err instanceof Error) prompts.log.error(err.message)
|
2025-06-17 04:07:17 +00:00
|
|
|
prompts.outro("Done")
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
spinner.stop("Upgrade complete")
|
|
|
|
|
prompts.outro("Done")
|
2025-06-15 02:05:41 +00:00
|
|
|
},
|
|
|
|
|
}
|