|
| 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 | +function run(target) { |
| 9 | + const result = childProcess.spawnSync(target, process.argv.slice(2), { |
| 10 | + stdio: "inherit", |
| 11 | + }) |
| 12 | + if (result.error) { |
| 13 | + console.error(result.error.message) |
| 14 | + process.exit(1) |
| 15 | + } |
| 16 | + const code = typeof result.status === "number" ? result.status : 0 |
| 17 | + process.exit(code) |
| 18 | +} |
| 19 | + |
| 20 | +const envPath = process.env.ALTIMATE_CODE_BIN_PATH |
| 21 | +if (envPath) { |
| 22 | + run(envPath) |
| 23 | +} |
| 24 | + |
| 25 | +const scriptPath = fs.realpathSync(__filename) |
| 26 | +const scriptDir = path.dirname(scriptPath) |
| 27 | + |
| 28 | +// |
| 29 | +const cached = path.join(scriptDir, ".altimate-code") |
| 30 | +if (fs.existsSync(cached)) { |
| 31 | + run(cached) |
| 32 | +} |
| 33 | + |
| 34 | +const platformMap = { |
| 35 | + darwin: "darwin", |
| 36 | + linux: "linux", |
| 37 | + win32: "windows", |
| 38 | +} |
| 39 | +const archMap = { |
| 40 | + x64: "x64", |
| 41 | + arm64: "arm64", |
| 42 | + arm: "arm", |
| 43 | +} |
| 44 | + |
| 45 | +let platform = platformMap[os.platform()] |
| 46 | +if (!platform) { |
| 47 | + platform = os.platform() |
| 48 | +} |
| 49 | +let arch = archMap[os.arch()] |
| 50 | +if (!arch) { |
| 51 | + arch = os.arch() |
| 52 | +} |
| 53 | +const scope = "@altimateai" |
| 54 | +const base = "altimate-code-" + platform + "-" + arch |
| 55 | +const binary = platform === "windows" ? "altimate-code.exe" : "altimate-code" |
| 56 | + |
| 57 | +function supportsAvx2() { |
| 58 | + if (arch !== "x64") return false |
| 59 | + |
| 60 | + if (platform === "linux") { |
| 61 | + try { |
| 62 | + return /(^|\s)avx2(\s|$)/i.test(fs.readFileSync("/proc/cpuinfo", "utf8")) |
| 63 | + } catch { |
| 64 | + return false |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + if (platform === "darwin") { |
| 69 | + try { |
| 70 | + const result = childProcess.spawnSync("sysctl", ["-n", "hw.optional.avx2_0"], { |
| 71 | + encoding: "utf8", |
| 72 | + timeout: 1500, |
| 73 | + }) |
| 74 | + if (result.status !== 0) return false |
| 75 | + return (result.stdout || "").trim() === "1" |
| 76 | + } catch { |
| 77 | + return false |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + if (platform === "windows") { |
| 82 | + const cmd = |
| 83 | + '(Add-Type -MemberDefinition "[DllImport(""kernel32.dll"")] public static extern bool IsProcessorFeaturePresent(int ProcessorFeature);" -Name Kernel32 -Namespace Win32 -PassThru)::IsProcessorFeaturePresent(40)' |
| 84 | + |
| 85 | + for (const exe of ["powershell.exe", "pwsh.exe", "pwsh", "powershell"]) { |
| 86 | + try { |
| 87 | + const result = childProcess.spawnSync(exe, ["-NoProfile", "-NonInteractive", "-Command", cmd], { |
| 88 | + encoding: "utf8", |
| 89 | + timeout: 3000, |
| 90 | + windowsHide: true, |
| 91 | + }) |
| 92 | + if (result.status !== 0) continue |
| 93 | + const out = (result.stdout || "").trim().toLowerCase() |
| 94 | + if (out === "true" || out === "1") return true |
| 95 | + if (out === "false" || out === "0") return false |
| 96 | + } catch { |
| 97 | + continue |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + return false |
| 102 | + } |
| 103 | + |
| 104 | + return false |
| 105 | +} |
| 106 | + |
| 107 | +const names = (() => { |
| 108 | + const avx2 = supportsAvx2() |
| 109 | + const baseline = arch === "x64" && !avx2 |
| 110 | + |
| 111 | + if (platform === "linux") { |
| 112 | + const musl = (() => { |
| 113 | + try { |
| 114 | + if (fs.existsSync("/etc/alpine-release")) return true |
| 115 | + } catch { |
| 116 | + // ignore |
| 117 | + } |
| 118 | + |
| 119 | + try { |
| 120 | + const result = childProcess.spawnSync("ldd", ["--version"], { encoding: "utf8" }) |
| 121 | + const text = ((result.stdout || "") + (result.stderr || "")).toLowerCase() |
| 122 | + if (text.includes("musl")) return true |
| 123 | + } catch { |
| 124 | + // ignore |
| 125 | + } |
| 126 | + |
| 127 | + return false |
| 128 | + })() |
| 129 | + |
| 130 | + if (musl) { |
| 131 | + if (arch === "x64") { |
| 132 | + if (baseline) return [`${base}-baseline-musl`, `${base}-musl`, `${base}-baseline`, base] |
| 133 | + return [`${base}-musl`, `${base}-baseline-musl`, base, `${base}-baseline`] |
| 134 | + } |
| 135 | + return [`${base}-musl`, base] |
| 136 | + } |
| 137 | + |
| 138 | + if (arch === "x64") { |
| 139 | + if (baseline) return [`${base}-baseline`, base, `${base}-baseline-musl`, `${base}-musl`] |
| 140 | + return [base, `${base}-baseline`, `${base}-musl`, `${base}-baseline-musl`] |
| 141 | + } |
| 142 | + return [base, `${base}-musl`] |
| 143 | + } |
| 144 | + |
| 145 | + if (arch === "x64") { |
| 146 | + if (baseline) return [`${base}-baseline`, base] |
| 147 | + return [base, `${base}-baseline`] |
| 148 | + } |
| 149 | + return [base] |
| 150 | +})() |
| 151 | + |
| 152 | +function findBinary(startDir) { |
| 153 | + let current = startDir |
| 154 | + for (;;) { |
| 155 | + const modules = path.join(current, "node_modules") |
| 156 | + if (fs.existsSync(modules)) { |
| 157 | + for (const name of names) { |
| 158 | + const candidate = path.join(modules, scope, name, "bin", binary) |
| 159 | + if (fs.existsSync(candidate)) return candidate |
| 160 | + } |
| 161 | + } |
| 162 | + const parent = path.dirname(current) |
| 163 | + if (parent === current) { |
| 164 | + return |
| 165 | + } |
| 166 | + current = parent |
| 167 | + } |
| 168 | +} |
| 169 | + |
| 170 | +const resolved = findBinary(scriptDir) |
| 171 | +if (!resolved) { |
| 172 | + console.error( |
| 173 | + "It seems that your package manager failed to install the right version of the altimate-code CLI for your platform. You can try manually installing " + |
| 174 | + names.map((n) => `\"${scope}/${n}\"`).join(" or ") + |
| 175 | + " package", |
| 176 | + ) |
| 177 | + process.exit(1) |
| 178 | +} |
| 179 | + |
| 180 | +run(resolved) |
0 commit comments