|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +const childProcess = require("child_process") |
| 4 | +const fs = require("fs") |
| 5 | +const path = require("path") |
| 6 | +const os = require("os") |
| 7 | + |
| 8 | +const forwardedSignals = ["SIGINT", "SIGTERM", "SIGHUP"] |
| 9 | + |
| 10 | +function run(target) { |
| 11 | + const child = childProcess.spawn(target, process.argv.slice(2), { stdio: "inherit" }) |
| 12 | + child.on("error", (error) => { |
| 13 | + console.error(error.message) |
| 14 | + process.exit(1) |
| 15 | + }) |
| 16 | + const forwarders = {} |
| 17 | + for (const signal of forwardedSignals) { |
| 18 | + forwarders[signal] = () => { |
| 19 | + try { |
| 20 | + child.kill(signal) |
| 21 | + } catch {} |
| 22 | + } |
| 23 | + process.on(signal, forwarders[signal]) |
| 24 | + } |
| 25 | + child.on("exit", (code, signal) => { |
| 26 | + for (const forwardedSignal of forwardedSignals) process.removeListener(forwardedSignal, forwarders[forwardedSignal]) |
| 27 | + if (signal) return process.kill(process.pid, signal) |
| 28 | + process.exit(typeof code === "number" ? code : 0) |
| 29 | + }) |
| 30 | +} |
| 31 | + |
| 32 | +const envPath = process.env.OPENCODE_BIN_PATH |
| 33 | +const scriptDir = path.dirname(fs.realpathSync(__filename)) |
| 34 | +const cached = path.join(scriptDir, ".lildax") |
| 35 | +const platform = { darwin: "darwin", linux: "linux", win32: "windows" }[os.platform()] || os.platform() |
| 36 | +const arch = { x64: "x64", arm64: "arm64", arm: "arm" }[os.arch()] || os.arch() |
| 37 | +const base = "@opencode-ai/lildax-" + platform + "-" + arch |
| 38 | +const binary = platform === "windows" ? "lildax.exe" : "lildax" |
| 39 | + |
| 40 | +function supportsAvx2() { |
| 41 | + if (arch !== "x64") return false |
| 42 | + if (platform === "linux") { |
| 43 | + try { |
| 44 | + return /(^|\s)avx2(\s|$)/i.test(fs.readFileSync("/proc/cpuinfo", "utf8")) |
| 45 | + } catch { |
| 46 | + return false |
| 47 | + } |
| 48 | + } |
| 49 | + if (platform === "darwin") { |
| 50 | + try { |
| 51 | + const result = childProcess.spawnSync("sysctl", ["-n", "hw.optional.avx2_0"], { encoding: "utf8", timeout: 1500 }) |
| 52 | + return result.status === 0 && (result.stdout || "").trim() === "1" |
| 53 | + } catch { |
| 54 | + return false |
| 55 | + } |
| 56 | + } |
| 57 | + if (platform === "windows") { |
| 58 | + const command = |
| 59 | + '(Add-Type -MemberDefinition "[DllImport(""kernel32.dll"")] public static extern bool IsProcessorFeaturePresent(int ProcessorFeature);" -Name Kernel32 -Namespace Win32 -PassThru)::IsProcessorFeaturePresent(40)' |
| 60 | + for (const executable of ["powershell.exe", "pwsh.exe", "pwsh", "powershell"]) { |
| 61 | + try { |
| 62 | + const result = childProcess.spawnSync(executable, ["-NoProfile", "-NonInteractive", "-Command", command], { |
| 63 | + encoding: "utf8", |
| 64 | + timeout: 3000, |
| 65 | + windowsHide: true, |
| 66 | + }) |
| 67 | + if (result.status !== 0) continue |
| 68 | + const output = (result.stdout || "").trim().toLowerCase() |
| 69 | + if (output === "true" || output === "1") return true |
| 70 | + if (output === "false" || output === "0") return false |
| 71 | + } catch { |
| 72 | + continue |
| 73 | + } |
| 74 | + } |
| 75 | + } |
| 76 | + return false |
| 77 | +} |
| 78 | + |
| 79 | +const names = (() => { |
| 80 | + const baseline = arch === "x64" && !supportsAvx2() |
| 81 | + if (platform === "linux") { |
| 82 | + const musl = (() => { |
| 83 | + try { |
| 84 | + if (fs.existsSync("/etc/alpine-release")) return true |
| 85 | + const result = childProcess.spawnSync("ldd", ["--version"], { encoding: "utf8" }) |
| 86 | + return ((result.stdout || "") + (result.stderr || "")).toLowerCase().includes("musl") |
| 87 | + } catch { |
| 88 | + return false |
| 89 | + } |
| 90 | + })() |
| 91 | + if (musl) return arch === "x64" ? (baseline ? [`${base}-baseline-musl`, `${base}-musl`, `${base}-baseline`, base] : [`${base}-musl`, `${base}-baseline-musl`, base, `${base}-baseline`]) : [`${base}-musl`, base] |
| 92 | + return arch === "x64" ? (baseline ? [`${base}-baseline`, base, `${base}-baseline-musl`, `${base}-musl`] : [base, `${base}-baseline`, `${base}-musl`, `${base}-baseline-musl`]) : [base, `${base}-musl`] |
| 93 | + } |
| 94 | + return arch === "x64" ? (baseline ? [`${base}-baseline`, base] : [base, `${base}-baseline`]) : [base] |
| 95 | +})() |
| 96 | + |
| 97 | +function findBinary(startDir) { |
| 98 | + let current = startDir |
| 99 | + for (;;) { |
| 100 | + const modules = path.join(current, "node_modules") |
| 101 | + if (fs.existsSync(modules)) for (const name of names) { |
| 102 | + const candidate = path.join(modules, name, "bin", binary) |
| 103 | + if (fs.existsSync(candidate)) return candidate |
| 104 | + } |
| 105 | + const parent = path.dirname(current) |
| 106 | + if (parent === current) return |
| 107 | + current = parent |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +const resolved = envPath || (fs.existsSync(cached) ? cached : findBinary(scriptDir)) |
| 112 | +if (!resolved) { |
| 113 | + console.error("It seems that your package manager failed to install the right lildax CLI package. Try manually installing " + names.map((name) => `"${name}"`).join(" or ") + " package") |
| 114 | + process.exit(1) |
| 115 | +} |
| 116 | +run(resolved) |
0 commit comments