Something went wrong. Try again.
opencode v2 mirror
Something went wrong. Try again.
TypeScript
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120import { $ } from "bun"import { chmod, copyFile, mkdtemp, rm } from "node:fs/promises"import { tmpdir } from "node:os"import { dirname, join } from "node:path"
const CLI_VERSION = "dev"
export type Channel = "dev" | "beta" | "prod"
export function resolveChannel(): Channel { const raw = Bun.env.OPENCODE_CHANNEL if (raw === "dev" || raw === "beta" || raw === "prod") return raw if (raw === "latest") return "prod" return "dev"}
export const CLI_BINARIES: Array<{ target: string; package: string; os: string; cpu: string }> = [ { target: "aarch64-apple-darwin", package: "@opencode/cli-darwin-arm64", os: "darwin", cpu: "arm64", }, { target: "x86_64-apple-darwin", package: "@opencode/cli-darwin-x64-baseline", os: "darwin", cpu: "x64", }, { target: "aarch64-pc-windows-msvc", package: "@opencode/cli-windows-arm64", os: "win32", cpu: "arm64", }, { target: "x86_64-pc-windows-msvc", package: "@opencode/cli-windows-x64-baseline", os: "win32", cpu: "x64", }, { target: "x86_64-unknown-linux-gnu", package: "@opencode/cli-linux-x64-baseline", os: "linux", cpu: "x64", }, { target: "aarch64-unknown-linux-gnu", package: "@opencode/cli-linux-arm64", os: "linux", cpu: "arm64", },]
export const CLI_TARGET = Bun.env.OPENCODE_CLI_TARGET
function nativeTarget() { const { platform, arch } = process if (platform === "darwin") return arch === "arm64" ? "aarch64-apple-darwin" : "x86_64-apple-darwin" if (platform === "win32") return arch === "arm64" ? "aarch64-pc-windows-msvc" : "x86_64-pc-windows-msvc" if (platform === "linux") return arch === "arm64" ? "aarch64-unknown-linux-gnu" : "x86_64-unknown-linux-gnu" throw new Error(`Unsupported platform: ${platform}/${arch}`)}
export function getCurrentCli(target = CLI_TARGET ?? nativeTarget()) { const binaryConfig = CLI_BINARIES.find((item) => item.target === target) if (!binaryConfig) throw new Error(`CLI configuration not available for target '${target}'`)
return binaryConfig}
export async function downloadCliToResources(version = CLI_VERSION, dest = windowsify("resources/opencode-cli")) { const cli = getCurrentCli() const directory = await mkdtemp(join(tmpdir(), "opencode-cli-")) try { await $`bun install --no-save --cwd ${directory} ${`${cli.package}@${version}`} ${`--os=${cli.os}`} ${`--cpu=${cli.cpu}`}` await copyCliToResources(join(directory, "node_modules", cli.package), dest) } finally { await rm(directory, { recursive: true, force: true }) }
console.log(`Copied ${cli.package}@${version} to ${dest}`)}
export async function copyBuiltCliToResources(root: string, dest = windowsify("resources/opencode-cli")) { const cli = getCurrentCli() const directory = cli.package.replace("@opencode/", "") await copyCliToResources(join(root, directory), dest)}
// The package directory is an npm package: its package.json version is the string the executable// prints for --version. Writing it next to the executable spares the desktop a ~400 ms spawn of the// 200 MB binary on first launch.async function copyCliToResources(pkg: string, dest: string) { const cli = getCurrentCli() await copyFile(join(pkg, "bin", cli.os === "win32" ? "opencode.exe" : "opencode"), dest) await prepareCli(dest) const manifest = (await Bun.file(join(pkg, "package.json")).json()) as { version?: string } if (!manifest.version) throw new Error(`Bundled CLI package has no version: ${pkg}`) await Bun.write(versionFile(dest), manifest.version)}
export function versionFile(cli: string) { return join(dirname(cli), "opencode-cli.version")}
async function prepareCli(dest: string) { if (process.platform !== "win32") await chmod(dest, 0o755) if (process.platform === "win32" && process.env.GITHUB_ACTIONS === "true") { await $`pwsh -NoLogo -NoProfile -ExecutionPolicy Bypass -File ../../script/sign-windows.ps1 ${dest}` } if (process.platform === "darwin") await $`codesign --force --sign - ${dest}`}
export function windowsify(path: string) { if (path.endsWith(".exe")) return path return `${path}${process.platform === "win32" ? ".exe" : ""}`}