|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +import childProcess from "node:child_process" |
| 4 | +import fs from "node:fs" |
| 5 | +import os from "node:os" |
| 6 | +import path from "node:path" |
| 7 | +import { createRequire } from "node:module" |
| 8 | +import { fileURLToPath } from "node:url" |
| 9 | + |
| 10 | +const directory = path.dirname(fileURLToPath(import.meta.url)) |
| 11 | +const require = createRequire(import.meta.url) |
| 12 | +const packageJson = JSON.parse(fs.readFileSync(path.join(directory, "package.json"), "utf8")) |
| 13 | +const command = Object.keys(packageJson.bin ?? {})[0] |
| 14 | +if (!command) throw new Error("OpenCode package does not declare a binary") |
| 15 | + |
| 16 | +const platform = { darwin: "darwin", linux: "linux", win32: "windows" }[os.platform()] ?? os.platform() |
| 17 | +const arch = { x64: "x64", arm64: "arm64", arm: "arm" }[os.arch()] ?? os.arch() |
| 18 | +const sourceBinary = platform === "windows" ? `${command}.exe` : command |
| 19 | +const targetBinary = path.resolve(directory, packageJson.bin[command]) |
| 20 | +const dependencies = packageJson.optionalDependencies ?? {} |
| 21 | +const base = Object.keys(dependencies).find((name) => name.endsWith(`-${platform}-${arch}`)) |
| 22 | +if (!base) throw new Error(`OpenCode does not provide a binary for ${platform}-${arch}`) |
| 23 | + |
| 24 | +function supportsAvx2() { |
| 25 | + if (arch !== "x64") return false |
| 26 | + if (platform === "linux") { |
| 27 | + try { |
| 28 | + return /(^|\s)avx2(\s|$)/i.test(fs.readFileSync("/proc/cpuinfo", "utf8")) |
| 29 | + } catch { |
| 30 | + return false |
| 31 | + } |
| 32 | + } |
| 33 | + if (platform === "darwin") { |
| 34 | + try { |
| 35 | + const result = childProcess.spawnSync("sysctl", ["-n", "hw.optional.avx2_0"], { |
| 36 | + encoding: "utf8", |
| 37 | + timeout: 1500, |
| 38 | + }) |
| 39 | + return result.status === 0 && (result.stdout || "").trim() === "1" |
| 40 | + } catch { |
| 41 | + return false |
| 42 | + } |
| 43 | + } |
| 44 | + if (platform === "windows") { |
| 45 | + const script = |
| 46 | + '(Add-Type -MemberDefinition "[DllImport(""kernel32.dll"")] public static extern bool IsProcessorFeaturePresent(int ProcessorFeature);" -Name Kernel32 -Namespace Win32 -PassThru)::IsProcessorFeaturePresent(40)' |
| 47 | + for (const executable of ["powershell.exe", "pwsh.exe", "pwsh", "powershell"]) { |
| 48 | + try { |
| 49 | + const result = childProcess.spawnSync(executable, ["-NoProfile", "-NonInteractive", "-Command", script], { |
| 50 | + encoding: "utf8", |
| 51 | + timeout: 3000, |
| 52 | + windowsHide: true, |
| 53 | + }) |
| 54 | + if (result.status !== 0) continue |
| 55 | + const output = (result.stdout || "").trim().toLowerCase() |
| 56 | + if (output === "true" || output === "1") return true |
| 57 | + if (output === "false" || output === "0") return false |
| 58 | + } catch { |
| 59 | + continue |
| 60 | + } |
| 61 | + } |
| 62 | + } |
| 63 | + return false |
| 64 | +} |
| 65 | + |
| 66 | +function isMusl() { |
| 67 | + if (platform !== "linux") return false |
| 68 | + try { |
| 69 | + if (fs.existsSync("/etc/alpine-release")) return true |
| 70 | + const result = childProcess.spawnSync("ldd", ["--version"], { encoding: "utf8" }) |
| 71 | + return `${result.stdout || ""}${result.stderr || ""}`.toLowerCase().includes("musl") |
| 72 | + } catch { |
| 73 | + return false |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +function packageNames() { |
| 78 | + const baseline = arch === "x64" && !supportsAvx2() |
| 79 | + const names = |
| 80 | + platform === "linux" |
| 81 | + ? isMusl() |
| 82 | + ? arch === "x64" |
| 83 | + ? baseline |
| 84 | + ? [`${base}-baseline-musl`, `${base}-musl`, `${base}-baseline`, base] |
| 85 | + : [`${base}-musl`, `${base}-baseline-musl`, base, `${base}-baseline`] |
| 86 | + : [`${base}-musl`, base] |
| 87 | + : arch === "x64" |
| 88 | + ? baseline |
| 89 | + ? [`${base}-baseline`, base, `${base}-baseline-musl`, `${base}-musl`] |
| 90 | + : [base, `${base}-baseline`, `${base}-musl`, `${base}-baseline-musl`] |
| 91 | + : [base, `${base}-musl`] |
| 92 | + : arch === "x64" |
| 93 | + ? baseline |
| 94 | + ? [`${base}-baseline`, base] |
| 95 | + : [base, `${base}-baseline`] |
| 96 | + : [base] |
| 97 | + return names.filter((name) => dependencies[name]) |
| 98 | +} |
| 99 | + |
| 100 | +function copyBinary(source) { |
| 101 | + if (!fs.existsSync(source)) throw new Error(`Binary not found at ${source}`) |
| 102 | + fs.mkdirSync(path.dirname(targetBinary), { recursive: true }) |
| 103 | + if (fs.existsSync(targetBinary)) fs.unlinkSync(targetBinary) |
| 104 | + try { |
| 105 | + fs.linkSync(source, targetBinary) |
| 106 | + } catch { |
| 107 | + fs.copyFileSync(source, targetBinary) |
| 108 | + } |
| 109 | + fs.chmodSync(targetBinary, 0o755) |
| 110 | +} |
| 111 | + |
| 112 | +function resolveBinary(name) { |
| 113 | + const packagePath = require.resolve(`${name}/package.json`) |
| 114 | + return path.join(path.dirname(packagePath), "bin", sourceBinary) |
| 115 | +} |
| 116 | + |
| 117 | +function installPackage(name) { |
| 118 | + const temp = fs.mkdtempSync(path.join(os.tmpdir(), "opencode-install-")) |
| 119 | + try { |
| 120 | + const result = childProcess.spawnSync( |
| 121 | + "npm", |
| 122 | + ["install", "--ignore-scripts", "--no-save", "--loglevel=error", "--prefix", temp, `${name}@${dependencies[name]}`], |
| 123 | + { stdio: "inherit", windowsHide: true }, |
| 124 | + ) |
| 125 | + if (result.status !== 0) return false |
| 126 | + copyBinary(path.join(temp, "node_modules", name, "bin", sourceBinary)) |
| 127 | + return true |
| 128 | + } finally { |
| 129 | + fs.rmSync(temp, { recursive: true, force: true }) |
| 130 | + } |
| 131 | +} |
| 132 | + |
| 133 | +function verifyBinary() { |
| 134 | + return ( |
| 135 | + childProcess.spawnSync(targetBinary, ["--version"], { |
| 136 | + stdio: "ignore", |
| 137 | + windowsHide: true, |
| 138 | + }).status === 0 |
| 139 | + ) |
| 140 | +} |
| 141 | + |
| 142 | +function main() { |
| 143 | + const names = packageNames() |
| 144 | + for (const name of names) { |
| 145 | + try { |
| 146 | + copyBinary(resolveBinary(name)) |
| 147 | + if (verifyBinary()) return |
| 148 | + } catch { |
| 149 | + if (installPackage(name) && verifyBinary()) return |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | + throw new Error(`Failed to install OpenCode. Try manually installing ${names.map((name) => JSON.stringify(name)).join(" or ")}.`) |
| 154 | +} |
| 155 | + |
| 156 | +try { |
| 157 | + main() |
| 158 | +} catch (error) { |
| 159 | + console.error(error instanceof Error ? error.message : String(error)) |
| 160 | + process.exit(1) |
| 161 | +} |
0 commit comments