upgrade.ts 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. import type { Argv } from "yargs"
  2. import { UI } from "../ui"
  3. import * as prompts from "@clack/prompts"
  4. import { Installation } from "../../installation"
  5. export const UpgradeCommand = {
  6. command: "upgrade [target]",
  7. describe: "upgrade opencode to the latest or a specific version",
  8. builder: (yargs: Argv) => {
  9. return yargs
  10. .positional("target", {
  11. describe: "version to upgrade to, for ex '0.1.48' or 'v0.1.48'",
  12. type: "string",
  13. })
  14. .option("method", {
  15. alias: "m",
  16. describe: "installation method to use",
  17. type: "string",
  18. choices: ["curl", "npm", "pnpm", "bun", "brew"],
  19. })
  20. },
  21. handler: async (args: { target?: string; method?: string }) => {
  22. UI.empty()
  23. UI.println(UI.logo(" "))
  24. UI.empty()
  25. prompts.intro("Upgrade")
  26. const detectedMethod = await Installation.method()
  27. const method = (args.method as Installation.Method) ?? detectedMethod
  28. if (method === "unknown") {
  29. prompts.log.error(
  30. `opencode is installed to ${process.execPath} and seems to be managed by a package manager`,
  31. )
  32. prompts.outro("Done")
  33. return
  34. }
  35. prompts.log.info("Using method: " + method)
  36. const target = args.target ?? (await Installation.latest())
  37. prompts.log.info(`From ${Installation.VERSION} → ${target}`)
  38. const spinner = prompts.spinner()
  39. spinner.start("Upgrading...")
  40. const err = await Installation.upgrade(method, target).catch((err) => err)
  41. if (err) {
  42. spinner.stop("Upgrade failed")
  43. if (err instanceof Installation.UpgradeFailedError)
  44. prompts.log.error(err.data.stderr)
  45. else if (err instanceof Error) prompts.log.error(err.message)
  46. prompts.outro("Done")
  47. return
  48. }
  49. spinner.stop("Upgrade complete")
  50. prompts.outro("Done")
  51. },
  52. }