utils.ts 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. import { $ } from "bun"
  2. import { chmod, copyFile, mkdtemp, rm } from "node:fs/promises"
  3. import { tmpdir } from "node:os"
  4. import { join } from "node:path"
  5. const CLI_VERSION = "0.0.0-next-16365"
  6. export type Channel = "dev" | "beta" | "prod"
  7. export function resolveChannel(): Channel {
  8. const raw = Bun.env.OPENCODE_CHANNEL
  9. if (raw === "dev" || raw === "beta" || raw === "prod") return raw
  10. return "dev"
  11. }
  12. export const CLI_BINARIES: Array<{ rustTarget: string; package: string; os: string; cpu: string }> = [
  13. {
  14. rustTarget: "aarch64-apple-darwin",
  15. package: "@opencode-ai/cli-darwin-arm64",
  16. os: "darwin",
  17. cpu: "arm64",
  18. },
  19. {
  20. rustTarget: "x86_64-apple-darwin",
  21. package: "@opencode-ai/cli-darwin-x64-baseline",
  22. os: "darwin",
  23. cpu: "x64",
  24. },
  25. {
  26. rustTarget: "aarch64-pc-windows-msvc",
  27. package: "@opencode-ai/cli-windows-arm64",
  28. os: "win32",
  29. cpu: "arm64",
  30. },
  31. {
  32. rustTarget: "x86_64-pc-windows-msvc",
  33. package: "@opencode-ai/cli-windows-x64-baseline",
  34. os: "win32",
  35. cpu: "x64",
  36. },
  37. {
  38. rustTarget: "x86_64-unknown-linux-gnu",
  39. package: "@opencode-ai/cli-linux-x64-baseline",
  40. os: "linux",
  41. cpu: "x64",
  42. },
  43. {
  44. rustTarget: "aarch64-unknown-linux-gnu",
  45. package: "@opencode-ai/cli-linux-arm64",
  46. os: "linux",
  47. cpu: "arm64",
  48. },
  49. ]
  50. export const RUST_TARGET = Bun.env.RUST_TARGET
  51. function nativeTarget() {
  52. const { platform, arch } = process
  53. if (platform === "darwin") return arch === "arm64" ? "aarch64-apple-darwin" : "x86_64-apple-darwin"
  54. if (platform === "win32") return arch === "arm64" ? "aarch64-pc-windows-msvc" : "x86_64-pc-windows-msvc"
  55. if (platform === "linux") return arch === "arm64" ? "aarch64-unknown-linux-gnu" : "x86_64-unknown-linux-gnu"
  56. throw new Error(`Unsupported platform: ${platform}/${arch}`)
  57. }
  58. export function getCurrentCli(target = RUST_TARGET ?? nativeTarget()) {
  59. const binaryConfig = CLI_BINARIES.find((item) => item.rustTarget === target)
  60. if (!binaryConfig) throw new Error(`CLI configuration not available for target '${target}'`)
  61. return binaryConfig
  62. }
  63. export async function downloadCliToResources() {
  64. const cli = getCurrentCli()
  65. const directory = await mkdtemp(join(tmpdir(), "opencode-cli-"))
  66. const dest = windowsify("resources/opencode-cli")
  67. try {
  68. await $`bun install --no-save --cwd ${directory} ${`${cli.package}@${CLI_VERSION}`} ${`--os=${cli.os}`} ${`--cpu=${cli.cpu}`}`
  69. await copyFile(
  70. join(directory, "node_modules", cli.package, "bin", cli.os === "win32" ? "opencode2.exe" : "opencode2"),
  71. dest,
  72. )
  73. } finally {
  74. await rm(directory, { recursive: true, force: true })
  75. }
  76. if (process.platform !== "win32") await chmod(dest, 0o755)
  77. if (process.platform === "win32" && process.env.GITHUB_ACTIONS === "true") {
  78. await $`pwsh -NoLogo -NoProfile -ExecutionPolicy Bypass -File ../../script/sign-windows.ps1 ${dest}`
  79. }
  80. if (process.platform === "darwin") await $`codesign --force --sign - ${dest}`
  81. console.log(`Copied ${cli.package} to ${dest}`)
  82. }
  83. export function windowsify(path: string) {
  84. if (path.endsWith(".exe")) return path
  85. return `${path}${process.platform === "win32" ? ".exe" : ""}`
  86. }