utils.ts 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. import { $ } from "bun"
  2. export type Channel = "dev" | "beta" | "prod"
  3. export function resolveChannel(): Channel {
  4. const raw = Bun.env.OPENCODE_CHANNEL
  5. if (raw === "dev" || raw === "beta" || raw === "prod") return raw
  6. return "dev"
  7. }
  8. export const SIDECAR_BINARIES: Array<{ rustTarget: string; ocBinary: string; assetExt: string }> = [
  9. {
  10. rustTarget: "aarch64-apple-darwin",
  11. ocBinary: "opencode-darwin-arm64",
  12. assetExt: "zip",
  13. },
  14. {
  15. rustTarget: "x86_64-apple-darwin",
  16. ocBinary: "opencode-darwin-x64-baseline",
  17. assetExt: "zip",
  18. },
  19. {
  20. rustTarget: "x86_64-pc-windows-msvc",
  21. ocBinary: "opencode-windows-x64-baseline",
  22. assetExt: "zip",
  23. },
  24. {
  25. rustTarget: "x86_64-unknown-linux-gnu",
  26. ocBinary: "opencode-linux-x64-baseline",
  27. assetExt: "tar.gz",
  28. },
  29. {
  30. rustTarget: "aarch64-unknown-linux-gnu",
  31. ocBinary: "opencode-linux-arm64",
  32. assetExt: "tar.gz",
  33. },
  34. ]
  35. export const RUST_TARGET = Bun.env.RUST_TARGET
  36. function nativeTarget() {
  37. const { platform, arch } = process
  38. if (platform === "darwin") return arch === "arm64" ? "aarch64-apple-darwin" : "x86_64-apple-darwin"
  39. if (platform === "win32") return "x86_64-pc-windows-msvc"
  40. if (platform === "linux") return arch === "arm64" ? "aarch64-unknown-linux-gnu" : "x86_64-unknown-linux-gnu"
  41. throw new Error(`Unsupported platform: ${platform}/${arch}`)
  42. }
  43. export function getCurrentSidecar(target = RUST_TARGET ?? nativeTarget()) {
  44. const binaryConfig = SIDECAR_BINARIES.find((b) => b.rustTarget === target)
  45. if (!binaryConfig) throw new Error(`Sidecar configuration not available for Rust target '${target}'`)
  46. return binaryConfig
  47. }
  48. export async function copyBinaryToSidecarFolder(source: string) {
  49. const dir = `resources`
  50. await $`mkdir -p ${dir}`
  51. const dest = windowsify(`${dir}/opencode-cli`)
  52. await $`cp ${source} ${dest}`
  53. if (process.platform === "darwin") await $`codesign --force --sign - ${dest}`
  54. console.log(`Copied ${source} to ${dest}`)
  55. }
  56. export function windowsify(path: string) {
  57. if (path.endsWith(".exe")) return path
  58. return `${path}${process.platform === "win32" ? ".exe" : ""}`
  59. }