#!/usr/bin/env node // Unified entry point for the Hanzo Dev CLI. import path from "path"; import { fileURLToPath } from "url"; import { platform as nodePlatform, arch as nodeArch } from "os"; import { execSync } from "child_process"; import { get as httpsGet } from "https"; import { runPostinstall } from "../postinstall.js"; // __dirname equivalent in ESM const __filename = fileURLToPath(import.meta.url); const __dirname = path.dirname(__filename); const { platform, arch } = process; function isWSL() { if (platform !== "linux") return false; try { const txt = readFileSync("/proc/version", "utf8").toLowerCase(); return txt.includes("microsoft"); } catch { return false; } } let targetTriple = null; switch (platform) { case "linux": case "android": switch (arch) { case "x64": targetTriple = "x86_64-unknown-linux-musl"; break; case "arm64": targetTriple = "aarch64-unknown-linux-musl"; break; default: break; } break; case "darwin": switch (arch) { case "x64": targetTriple = "x86_64-apple-darwin"; break; case "arm64": targetTriple = "aarch64-apple-darwin"; break; default: break; } break; case "win32": switch (arch) { case "x64": targetTriple = "x86_64-pc-windows-msvc.exe"; break; case "arm64": // We do not build this today, fall through... default: break; } break; default: break; } if (!targetTriple) { throw new Error(`Unsupported platform: ${platform} (${arch})`); } // Binary names for Hanzo dev let binaryPath = path.join(__dirname, "..", "bin", `dev-${targetTriple}`); let legacyBinaryPath = path.join(__dirname, "..", "bin", `dev-${targetTriple}`); import { existsSync, chmodSync, statSync, openSync, readSync, closeSync, mkdirSync, copyFileSync, readFileSync, unlinkSync, createWriteStream } from "fs"; const validateBinary = (p) => { try { const st = statSync(p); if (!st.isFile() || st.size === 0) { return { ok: false, reason: "empty or not a regular file" }; } const fd = openSync(p, "r"); try { const buf = Buffer.alloc(4); const n = readSync(fd, buf, 0, 4, 0); if (n < 2) return { ok: false, reason: "too short" }; if (platform === "win32") { if (!(buf[0] === 0x4d && buf[1] === 0x5a)) return { ok: false, reason: "invalid PE header (missing MZ)" }; } else if (platform === "linux" || platform === "android") { if (!(buf[0] === 0x7f && buf[1] === 0x45 && buf[2] === 0x4c && buf[3] === 0x46)) return { ok: false, reason: "invalid ELF header" }; } else if (platform === "darwin") { const isMachO = (buf[0] === 0xcf && buf[1] === 0xfa && buf[2] === 0xed && buf[3] === 0xfe) || (buf[0] === 0xca && buf[1] === 0xfe && buf[2] === 0xba && buf[3] === 0xbe); if (!isMachO) return { ok: false, reason: "invalid Mach-O header" }; } } finally { closeSync(fd); } return { ok: true }; } catch (e) { return { ok: false, reason: e.message }; } }; const getCacheDir = (version) => { const plt = nodePlatform(); const home = process.env.HOME || process.env.USERPROFILE || ""; let base = ""; if (plt === "win32") { base = process.env.LOCALAPPDATA || path.join(home, "AppData", "Local"); } else if (plt === "darwin") { base = path.join(home, "Library", "Caches"); } else { base = process.env.XDG_CACHE_HOME || path.join(home, ".cache"); } const dir = path.join(base, "hanzo", "dev", version); if (!existsSync(dir)) mkdirSync(dir, { recursive: true }); return dir; }; const getCachedBinaryPath = (version) => { const cacheDir = getCacheDir(version); return path.join(cacheDir, `dev-${targetTriple}`); }; let lastBootstrapError = null; const httpsDownload = (url, dest) => new Promise((resolve, reject) => { const req = httpsGet(url, (res) => { const status = res.statusCode || 0; if (status >= 300 && status < 400 && res.headers.location) { return resolve(httpsDownload(res.headers.location, dest)); } if (status !== 200) { return reject(new Error(`HTTP ${status}`)); } const out = createWriteStream(dest); res.pipe(out); out.on("finish", () => out.close(resolve)); out.on("error", (e) => { try { unlinkSync(dest); } catch {} reject(e); }); }); req.on("error", (e) => { try { unlinkSync(dest); } catch {} reject(e); }); req.setTimeout(120000, () => { req.destroy(new Error("download timed out")); }); }); const tryBootstrapBinary = async () => { try { const pkg = JSON.parse(readFileSync(path.join(__dirname, "..", "package.json"), "utf8")); const version = pkg.version; const binDir = path.join(__dirname, "..", "bin"); if (!existsSync(binDir)) mkdirSync(binDir, { recursive: true }); // Fast path: user cache const cachePath = getCachedBinaryPath(version); if (existsSync(cachePath)) { const v = validateBinary(cachePath); if (v.ok) { if (platform !== "win32") { copyFileSync(cachePath, binaryPath); try { chmodSync(binaryPath, 0o755); } catch {} } return true; } } // Try platform package (if present) try { const req = (await import("module")).createRequire(import.meta.url); const name = (() => { if (platform === "win32") return "@hanzo/dev-win32-x64"; const plt = nodePlatform(); const cpu = nodeArch(); if (plt === "darwin" && cpu === "arm64") return "@hanzo/dev-darwin-arm64"; if (plt === "darwin" && cpu === "x64") return "@hanzo/dev-darwin-x64"; if (plt === "linux" && cpu === "x64") return "@hanzo/dev-linux-x64-musl"; if (plt === "linux" && cpu === "arm64") return "@hanzo/dev-linux-arm64-musl"; return null; })(); if (name) { try { const pkgJson = req.resolve(`${name}/package.json`); const pkgDir = path.dirname(pkgJson); const src = path.join(pkgDir, "bin", `dev-${targetTriple}`); if (existsSync(src)) { copyFileSync(src, cachePath); if (platform !== "win32") { copyFileSync(cachePath, binaryPath); try { chmodSync(binaryPath, 0o755); } catch {} } return true; } } catch { /* ignore and fall back */ } } } catch { /* ignore */ } // Download from GitHub release const isWin = platform === "win32"; // Use 'dev-*' binary names matching the release artifacts const binaryName = `dev-${targetTriple}`; const archiveName = isWin ? `${binaryName}.zip` : (() => { try { execSync("zstd --version", { stdio: "ignore", shell: true }); return `${binaryName}.zst`; } catch { return `${binaryName}.tar.gz`; } })(); const url = `https://github.com/hanzoai/dev/releases/download/v${version}/${archiveName}`; const tmp = path.join(binDir, `.${archiveName}.part`); return httpsDownload(url, tmp) .then(() => { if (isWin) { try { const sysRoot = process.env.SystemRoot || process.env.windir || 'C:\\Windows'; const psFull = path.join(sysRoot, 'System32', 'WindowsPowerShell', 'v1.0', 'powershell.exe'); const unzipDest = getCacheDir(version); const psCmd = `Expand-Archive -Path '${tmp}' -DestinationPath '${unzipDest}' -Force`; let ok = false; try { execSync(`"${psFull}" -NoProfile -NonInteractive -Command "${psCmd}"`, { stdio: 'ignore' }); ok = true; } catch {} if (!ok) { try { execSync(`powershell -NoProfile -NonInteractive -Command "${psCmd}"`, { stdio: 'ignore' }); ok = true; } catch {} } if (!ok) { try { execSync(`pwsh -NoProfile -NonInteractive -Command "${psCmd}"`, { stdio: 'ignore' }); ok = true; } catch {} } if (!ok) { execSync(`tar -xf "${tmp}" -C "${unzipDest}"`, { stdio: 'ignore', shell: true }); } } catch (e) { throw new Error(`failed to unzip: ${e.message}`); } finally { try { unlinkSync(tmp); } catch {} } } else { if (archiveName.endsWith(".zst")) { try { execSync(`zstd -d '${tmp}' -o '${binaryPath}'`, { stdio: 'ignore', shell: true }); } catch (e) { try { unlinkSync(tmp); } catch {}; throw new Error(`failed to decompress zst: ${e.message}`); } try { unlinkSync(tmp); } catch {} } else { try { execSync(`tar -xzf '${tmp}' -C '${binDir}'`, { stdio: 'ignore', shell: true }); } catch (e) { try { unlinkSync(tmp); } catch {}; throw new Error(`failed to extract tar.gz: ${e.message}`); } try { unlinkSync(tmp); } catch {} } } if (platform !== "win32") { try { copyFileSync(binaryPath, cachePath); } catch {} } const v = validateBinary(platform === "win32" ? cachePath : binaryPath); if (!v.ok) throw new Error(`invalid binary (${v.reason})`); if (platform !== "win32") try { chmodSync(binaryPath, 0o755); } catch {} return true; }) .catch((e) => { lastBootstrapError = e; return false; }); } catch { return false; } }; // If missing, attempt to bootstrap into place let binaryReady = existsSync(binaryPath) || existsSync(legacyBinaryPath); if (!binaryReady) { let runtimePostinstallError = null; try { await runPostinstall({ invokedByRuntime: true, skipGlobalAlias: true }); } catch (err) { runtimePostinstallError = err; } binaryReady = existsSync(binaryPath) || existsSync(legacyBinaryPath); if (!binaryReady) { const ok = await tryBootstrapBinary(); if (!ok) { if (runtimePostinstallError && !lastBootstrapError) { lastBootstrapError = runtimePostinstallError; } if (existsSync(legacyBinaryPath) && !existsSync(binaryPath)) { binaryPath = legacyBinaryPath; } } } } // Prefer cached binary when available try { const pkg = JSON.parse(readFileSync(path.join(__dirname, "..", "package.json"), "utf8")); const version = pkg.version; const cached = getCachedBinaryPath(version); const v = existsSync(cached) ? validateBinary(cached) : { ok: false }; if (v.ok) { binaryPath = cached; } else if (!existsSync(binaryPath) && existsSync(legacyBinaryPath)) { binaryPath = legacyBinaryPath; } } catch { // ignore } import { spawnSync } from "child_process"; if (existsSync(binaryPath)) { try { if (platform !== "win32") { chmodSync(binaryPath, 0o755); } } catch (e) { // Ignore permission errors } } else { console.error(`Binary not found: ${binaryPath}`); if (lastBootstrapError) { const msg = (lastBootstrapError && (lastBootstrapError.message || String(lastBootstrapError))) || 'unknown bootstrap error'; console.error(`Bootstrap error: ${msg}`); } console.error(`Please try reinstalling the package:`); console.error(` npm uninstall -g @hanzo/dev`); console.error(` npm install -g @hanzo/dev`); if (isWSL()) { console.error("Detected WSL. Install inside WSL (Ubuntu) separately:"); console.error(" npx -y @hanzo/dev@latest (run inside WSL)"); console.error("If installed globally on Windows, those binaries are not usable from WSL."); } process.exit(1); } const validation = validateBinary(binaryPath); if (!validation.ok) { console.error(`The native binary at ${binaryPath} appears invalid: ${validation.reason}`); console.error("This can happen if the download failed or was modified by antivirus/proxy."); console.error("Please try reinstalling:"); console.error(" npm uninstall -g @hanzo/dev"); console.error(" npm install -g @hanzo/dev"); if (platform === "win32") { console.error("If the issue persists, clear npm cache and disable antivirus temporarily:"); console.error(" npm cache clean --force"); } if (isWSL()) { console.error("Detected WSL. Ensure you install/run inside WSL, not Windows:"); console.error(" npx -y @hanzo/dev@latest (inside WSL)"); } process.exit(1); } // If running under npx/npm, emit a concise notice try { const ua = process.env.npm_config_user_agent || ""; const isNpx = ua.includes("npx"); if (isNpx && process.stderr && process.stderr.isTTY) { console.error(`@hanzo/dev: running bundled binary -> ${binaryPath}`); } } catch {} const { spawn } = await import("child_process"); process.env.DEV_BINARY_PATH = binaryPath; const child = spawn(binaryPath, process.argv.slice(2), { stdio: "inherit", env: { ...process.env, DEV_MANAGED_BY_NPM: "1", DEV_BINARY_PATH: binaryPath }, }); child.on("error", (err) => { const code = err && err.code; if (code === 'EACCES') { console.error(`Permission denied: ${binaryPath}`); console.error(`Try running: chmod +x "${binaryPath}"`); console.error(`Or reinstall the package with: npm install -g @hanzo/dev`); } else if (code === 'EFTYPE' || code === 'ENOEXEC') { console.error(`Failed to execute native binary: ${binaryPath}`); console.error("The file may be corrupt or of the wrong type. Reinstall usually fixes this:"); console.error(" npm uninstall -g @hanzo/dev && npm install -g @hanzo/dev"); if (platform === 'win32') { console.error("On Windows, ensure the .exe downloaded correctly (proxy/AV can interfere)."); console.error("Try clearing cache: npm cache clean --force"); } if (isWSL()) { console.error("Detected WSL. Windows binaries cannot be executed from WSL."); console.error("Install inside WSL and run there: npx -y @hanzo/dev@latest"); } } else { console.error(err); } process.exit(1); }); const forwardSignal = (signal) => { if (child.killed) { return; } try { child.kill(signal); } catch { /* ignore */ } }; ["SIGINT", "SIGTERM", "SIGHUP"].forEach((sig) => { process.on(sig, () => forwardSignal(sig)); }); const childResult = await new Promise((resolve) => { child.on("exit", (code, signal) => { if (signal) { resolve({ type: "signal", signal }); } else { resolve({ type: "code", exitCode: code ?? 1 }); } }); }); if (childResult.type === "signal") { process.kill(process.pid, childResult.signal); } else { process.exit(childResult.exitCode); }