Repository navigation
Expand file tree
/
Copy pathaltimate
More file actions
executable file
·238 lines (210 loc) · 7.05 KB
/
Copy pathaltimate
File metadata and controls
executable file
·238 lines (210 loc) · 7.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
#!/usr/bin/env node
const childProcess = require("child_process")
const fs = require("fs")
const path = require("path")
const os = require("os")
// Resolve script location early — needed by both run() and findBinary().
const scriptPath = fs.realpathSync(__filename)
const scriptDir = path.dirname(scriptPath)
// Collect ALL node_modules directories walking upward from startDir.
// Bun's single-file executable uses a virtual filesystem (/$bunfs/root/) without
// node_modules. External packages are resolved via NODE_PATH instead.
// We collect every node_modules in the hierarchy (not just the first) to handle
// pnpm strict layouts, hoisted monorepos, and npm flat installs alike.
function findAllNodeModules(startDir) {
const paths = []
let current = startDir
for (;;) {
const modules = path.join(current, "node_modules")
if (fs.existsSync(modules)) paths.push(modules)
const parent = path.dirname(current)
if (parent === current) break
current = parent
}
return paths
}
function run(target) {
// Resolve NODE_PATH so the compiled Bun binary can find external packages
// installed alongside the wrapper (e.g. @altimateai/altimate-core NAPI module).
// Search from BOTH the binary's location AND the wrapper script's location
// to cover npm flat installs, pnpm isolated stores, and hoisted monorepos.
const env = { ...process.env }
// Export bin directory so the compiled binary can add it to PATH when
// spawning bash commands. This makes bundled tools (e.g. altimate-dbt)
// available to agents without manual PATH configuration.
env.ALTIMATE_BIN_DIR = scriptDir
try {
const resolvedTarget = fs.realpathSync(target)
const targetDir = path.dirname(path.dirname(resolvedTarget))
const targetModules = findAllNodeModules(targetDir)
const scriptModules = findAllNodeModules(scriptDir)
const allPaths = [...new Set([...scriptModules, ...targetModules])]
if (allPaths.length > 0) {
const sep = process.platform === "win32" ? ";" : ":"
const joined = allPaths.join(sep)
env.NODE_PATH = env.NODE_PATH ? joined + sep + env.NODE_PATH : joined
}
} catch {
// realpathSync failed (e.g. target doesn't exist) — continue without
// NODE_PATH; spawnSync will report the missing binary via result.error.
}
const result = childProcess.spawnSync(target, process.argv.slice(2), {
stdio: "inherit",
env,
})
if (result.error) {
console.error(result.error.message)
process.exit(1)
}
const code = typeof result.status === "number" ? result.status : 0
process.exit(code)
}
const envPath = process.env.ALTIMATE_CODE_BIN_PATH
if (envPath) {
run(envPath)
}
//
const cached = path.join(scriptDir, ".altimate-code")
if (fs.existsSync(cached)) {
run(cached)
}
const platformMap = {
darwin: "darwin",
linux: "linux",
win32: "windows",
}
const archMap = {
x64: "x64",
arm64: "arm64",
arm: "arm",
}
let platform = platformMap[os.platform()]
if (!platform) {
platform = os.platform()
}
let arch = archMap[os.arch()]
if (!arch) {
arch = os.arch()
}
const scope = "@altimateai"
const base = "altimate-code-" + platform + "-" + arch
const binary = platform === "windows" ? "altimate-code.exe" : "altimate-code"
function supportsAvx2() {
if (arch !== "x64") return false
if (platform === "linux") {
try {
return /(^|\s)avx2(\s|$)/i.test(fs.readFileSync("/proc/cpuinfo", "utf8"))
} catch {
return false
}
}
if (platform === "darwin") {
try {
const result = childProcess.spawnSync("sysctl", ["-n", "hw.optional.avx2_0"], {
encoding: "utf8",
timeout: 1500,
})
if (result.status !== 0) return false
return (result.stdout || "").trim() === "1"
} catch {
return false
}
}
if (platform === "windows") {
const cmd =
'(Add-Type -MemberDefinition "[DllImport(""kernel32.dll"")] public static extern bool IsProcessorFeaturePresent(int ProcessorFeature);" -Name Kernel32 -Namespace Win32 -PassThru)::IsProcessorFeaturePresent(40)'
for (const exe of ["powershell.exe", "pwsh.exe", "pwsh", "powershell"]) {
try {
const result = childProcess.spawnSync(exe, ["-NoProfile", "-NonInteractive", "-Command", cmd], {
encoding: "utf8",
timeout: 3000,
windowsHide: true,
})
if (result.status !== 0) continue
const out = (result.stdout || "").trim().toLowerCase()
if (out === "true" || out === "1") return true
if (out === "false" || out === "0") return false
} catch {
continue
}
}
return false
}
return false
}
function isMusl() {
if (platform !== "linux") return false
try {
if (fs.existsSync("/etc/alpine-release")) return true
} catch {
// ignore
}
try {
const result = childProcess.spawnSync("ldd", ["--version"], { encoding: "utf8" })
const text = ((result.stdout || "") + (result.stderr || "")).toLowerCase()
if (text.includes("musl")) return true
} catch {
// ignore
}
return false
}
// @altimateai/altimate-core has no NAPI prebuild for musl or win32-arm64,
// and the altimate binary embeds altimate-core's .node file at build time.
// Hard-error early instead of letting findBinary() walk the whole tree and
// emit a misleading "package manager failed to install the right version"
// message — the right diagnosis is that these platforms aren't built.
if (isMusl()) {
console.error("altimate-code is not currently supported on Alpine Linux (musl).")
console.error("Workarounds:")
console.error(" • apk add gcompat # run glibc binaries on Alpine")
console.error(" • Use a glibc-based container (debian/ubuntu/alpine+gcompat)")
process.exit(1)
}
if (platform === "windows" && arch === "arm64") {
console.error("altimate-code is not currently built for Windows on ARM64.")
console.error("Run the x64 build under Windows ARM's x64 emulation, or use WSL.")
process.exit(1)
}
const names = (() => {
const avx2 = supportsAvx2()
const baseline = arch === "x64" && !avx2
if (platform === "linux") {
if (arch === "x64") {
if (baseline) return [`${base}-baseline`, base]
return [base, `${base}-baseline`]
}
return [base]
}
if (arch === "x64") {
if (baseline) return [`${base}-baseline`, base]
return [base, `${base}-baseline`]
}
return [base]
})()
function findBinary(startDir) {
let current = startDir
for (;;) {
const modules = path.join(current, "node_modules")
if (fs.existsSync(modules)) {
for (const name of names) {
const candidate = path.join(modules, scope, name, "bin", binary)
if (fs.existsSync(candidate)) return candidate
}
}
const parent = path.dirname(current)
if (parent === current) {
return
}
current = parent
}
}
const resolved = findBinary(scriptDir)
if (!resolved) {
console.error(
"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 " +
names.map((n) => `\"${scope}/${n}\"`).join(" or ") +
" package",
)
process.exit(1)
}
run(resolved)