UNPKG

@hanzo/dev

Version:

Lightweight coding agent that runs in your terminal - Hanzo AI developer tools

421 lines (389 loc) 15.6 kB
#!/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"; // __dirname equivalent in ESM const __filename = fileURLToPath(import.meta.url); const __dirname = path.dirname(__filename); const { platform, arch } = process; // Important: Never delegate to another system's `dev` binary. // When users run via `npx @hanzo/dev`, we must always execute our // packaged native binary by absolute path to avoid PATH collisions. const isWSL = () => { if (platform !== "linux") return false; try { const os = require("os"); const rel = os.release().toLowerCase(); if (rel.includes("microsoft")) return true; const fs = require("fs"); const txt = fs.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})`); } // Use 'dev-*' binary names let binaryPath = path.join(__dirname, "..", "bin", `dev-${targetTriple}`); let legacyBinaryPath = path.join(__dirname, "..", "bin", `dev-${targetTriple}`); // --- Bootstrap helper (runs if the binary is missing, e.g. Bun blocked postinstall) --- import { existsSync, chmodSync, statSync, openSync, readSync, closeSync, mkdirSync, copyFileSync, readFileSync, unlinkSync } 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 isWin = nodePlatform() === "win32"; const ext = isWin ? ".exe" : ""; const cacheDir = getCacheDir(version); return path.join(cacheDir, `dev-${targetTriple}${ext}`); }; 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) { // follow one redirect recursively return resolve(httpsDownload(res.headers.location, dest)); } if (status !== 200) { return reject(new Error(`HTTP ${status}`)); } const out = require("fs").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 { // 1) Read our published version 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 }); // 2) Fast path: user cache const cachePath = getCachedBinaryPath(version); if (existsSync(cachePath)) { const v = validateBinary(cachePath); if (v.ok) { copyFileSync(cachePath, binaryPath); if (platform !== "win32") chmodSync(binaryPath, 0o755); return existsSync(binaryPath); } } // 3) Try platform package (if present) try { const req = (await import("module")).createRequire(import.meta.url); const name = (() => { if (platform === "win32") return "@just-every/code-win32-x64"; // may be unpublished; falls through 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}${platform === "win32" ? ".exe" : ""}`); if (existsSync(src)) { copyFileSync(src, binaryPath); if (platform !== "win32") chmodSync(binaryPath, 0o755); // refresh cache try { copyFileSync(binaryPath, cachePath); } catch {} return existsSync(binaryPath); } } catch { /* ignore and fall back */ } } } catch { /* ignore */ } // 4) Download from GitHub release const isWin = platform === "win32"; const archiveName = isWin ? `dev-${targetTriple}.zip` : (() => { try { execSync("zstd --version", { stdio: "ignore", shell: true }); return `dev-${targetTriple}.zst`; } catch { return `dev-${targetTriple}.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 ps = `powershell -NoProfile -NonInteractive -Command "Expand-Archive -Path '${tmp}' -DestinationPath '${binDir}' -Force"`; execSync(ps, { stdio: "ignore" }); } 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 {} } } const v = validateBinary(binaryPath); if (!v.ok) throw new Error(`invalid binary (${v.reason})`); if (platform !== "win32") chmodSync(binaryPath, 0o755); try { copyFileSync(binaryPath, cachePath); } catch {} return true; }) .catch((_e) => false); } catch { return false; } }; // If missing, attempt to bootstrap into place (helps when Bun blocks postinstall) if (!existsSync(binaryPath) && !existsSync(legacyBinaryPath)) { const ok = await tryBootstrapBinary(); if (!ok) { // retry legacy name in case archive provided coder-* if (existsSync(legacyBinaryPath) && !existsSync(binaryPath)) { binaryPath = legacyBinaryPath; } } } // Fall back to legacy name if primary is still missing if (!existsSync(binaryPath) && existsSync(legacyBinaryPath)) { binaryPath = legacyBinaryPath; } // Check if binary exists and try to fix permissions if needed // fs imports are above; keep for readability if tree-shaken by bundlers import { spawnSync } from "child_process"; if (existsSync(binaryPath)) { try { // Ensure binary is executable on Unix-like systems if (platform !== "win32") { chmodSync(binaryPath, 0o755); } } catch (e) { // Ignore permission errors, will be caught below if it's a real problem } } else { console.error(`Binary not found: ${binaryPath}`); 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); } // Lightweight header validation to provide clearer errors before spawn // Reuse the validateBinary helper defined above in the bootstrap section. 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 about which binary path is used try { const ua = process.env.npm_config_user_agent || ""; const isNpx = ua.includes("npx"); if (isNpx && process.stderr && process.stderr.isTTY) { // Best-effort discovery of another 'code' on PATH for user clarity let otherCode = ""; try { const cmd = process.platform === "win32" ? "where code" : "command -v code || which code || true"; const out = spawnSync(process.platform === "win32" ? "cmd" : "bash", [ process.platform === "win32" ? "/c" : "-lc", cmd, ], { encoding: "utf8" }); const line = (out.stdout || "").split(/\r?\n/).map((s) => s.trim()).filter(Boolean)[0]; if (line && !line.includes("@hanzo/dev")) { otherCode = line; } } catch {} if (otherCode) { console.error(`@hanzo/dev: running bundled binary -> ${binaryPath}`); console.error(`Note: a different 'dev' exists at ${otherCode}; not delegating.`); } else { console.error(`@hanzo/dev: running bundled binary -> ${binaryPath}`); } } } catch {} // Use an asynchronous spawn instead of spawnSync so that Node is able to // respond to signals (e.g. Ctrl-C / SIGINT) while the native binary is // executing. This allows us to forward those signals to the child process // and guarantees that when either the child terminates or the parent // receives a fatal signal, both processes exit in a predictable manner. const { spawn } = await import("child_process"); const child = spawn(binaryPath, process.argv.slice(2), { stdio: "inherit", env: { ...process.env, CODER_MANAGED_BY_NPM: "1", CODEX_MANAGED_BY_NPM: "1" }, }); child.on("error", (err) => { // Typically triggered when the binary is missing or not executable. 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); }); // Forward common termination signals to the child so that it shuts down // gracefully. In the handler we temporarily disable the default behavior of // exiting immediately; once the child has been signaled we simply wait for // its exit event which will in turn terminate the parent (see below). const forwardSignal = (signal) => { if (child.killed) { return; } try { child.kill(signal); } catch { /* ignore */ } }; ["SIGINT", "SIGTERM", "SIGHUP"].forEach((sig) => { process.on(sig, () => forwardSignal(sig)); }); // When the child exits, mirror its termination reason in the parent so that // shell scripts and other tooling observe the correct exit status. // Wrap the lifetime of the child process in a Promise so that we can await // its termination in a structured way. The Promise resolves with an object // describing how the child exited: either via exit code or due to a signal. 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") { // Re-emit the same signal so that the parent terminates with the expected // semantics (this also sets the correct exit code of 128 + n). process.kill(process.pid, childResult.signal); } else { process.exit(childResult.exitCode); }