#!/usr/bin/env bun import { chmodSync, copyFileSync, existsSync, mkdirSync, realpathSync, renameSync, rmSync } from "node:fs"; import { spawnSync } from "node:child_process"; import { homedir } from "node:os"; import { dirname, join, resolve } from "node:path"; import { fileURLToPath } from "node:url"; const root = resolve(dirname(fileURLToPath(import.meta.url)), ".."); export function resolveTargetBinaryPath(): string { // 1. Check which seal-use try { const whichRes = spawnSync("which", ["seal-use"], { encoding: "utf8", env: { ...process.env } }); if (whichRes.status === 0 && whichRes.stdout) { const p = whichRes.stdout.trim(); if (p && existsSync(p)) { // Preserve native binary symlinks, but never overwrite an npm-linked source entry. const target = realpathSync(p); return /\.(?:[cm]?[jt]sx?|sh)$/i.test(target) ? p : target; } } } catch { // ignore } // 2. Fallback candidate paths in order of preference const candidates = [ "/usr/local/bin/seal-use", join(homedir(), ".local", "bin", "seal-use"), join(homedir(), "bin", "seal-use") ]; for (const candidate of candidates) { const dir = dirname(candidate); if (existsSync(dir)) { return candidate; } } const defaultDir = join(homedir(), ".local", "bin"); mkdirSync(defaultDir, { recursive: true }); return join(defaultDir, "seal-use"); } async function main() { console.error("==> [dev:override] Building latest host binary from local worktree..."); // 1. Compile host binary with dev metadata const buildRes = spawnSync("node", ["scripts/build-binary.mjs"], { cwd: root, stdio: ["ignore", "inherit", "inherit"] }); if (buildRes.status !== 0) { console.error("❌ [dev:override] Build failed."); process.exit(buildRes.status ?? 1); } const localBinary = join(root, "dist", "bin", "seal-use"); if (!existsSync(localBinary)) { console.error(`❌ [dev:override] Expected binary at ${localBinary} not found.`); process.exit(1); } const targetPath = resolveTargetBinaryPath(); const targetDir = dirname(targetPath); mkdirSync(targetDir, { recursive: true }); console.error(`==> [dev:override] Installing to global path: ${targetPath}`); // 2. Check and stop background service if running let serviceWasRunning = false; try { const statusRes = spawnSync(targetPath, ["service", "status"], { encoding: "utf8" }); if (statusRes.status === 0 && statusRes.stdout) { const parsed = JSON.parse(statusRes.stdout); if (parsed.running) { console.error("==> [dev:override] Stopping running background service daemon..."); spawnSync(targetPath, ["service", "stop"], { stdio: "ignore" }); serviceWasRunning = true; } } } catch { // ignore } // 3. Atomically overwrite target binary const tmpTarget = `${targetPath}.tmp.${Date.now()}`; copyFileSync(localBinary, tmpTarget); chmodSync(tmpTarget, 0o755); if (process.platform === "darwin") { try { spawnSync("/usr/bin/codesign", ["--remove-signature", tmpTarget], { stdio: "ignore" }); spawnSync("/usr/bin/codesign", ["--force", "--sign", "-", tmpTarget], { stdio: "ignore" }); } catch { // ignore } } renameSync(tmpTarget, targetPath); try { rmSync(tmpTarget, { force: true }); } catch { // ignore } // 4. Sync Agent Skill to ~/.agents/skills/seal-use const skillTarget = join(homedir(), ".agents", "skills", "seal-use"); // Install the binary's manifest, including references sourced outside skills/seal-use. // A custom destination avoids changing unrelated agent host settings during development. const skillRes = spawnSync(targetPath, ["skill", "install", "--dir", skillTarget], { encoding: "utf8" }); if (skillRes.status !== 0) { throw new Error(`[dev:override] Failed to sync agent skill: ${skillRes.stderr || skillRes.error || "unknown error"}`); } console.error(`✓ [dev:override] Synced agent skill to ${skillTarget}`); // 5. Restart service daemon if it was previously running if (serviceWasRunning) { console.error("==> [dev:override] Restarting background service daemon with new binary..."); spawnSync(targetPath, ["service", "start"], { stdio: "ignore" }); } // 6. Verify and output const testRes = spawnSync(targetPath, ["v"], { encoding: "utf8" }); if (testRes.status !== 0 || !testRes.stdout) { throw new Error(testRes.stderr || "Failed to verify updated binary"); } const installed = JSON.parse(testRes.stdout); if (installed.name !== "seal-use" || typeof installed.version !== "string") { throw new Error("Updated binary returned an invalid version payload"); } console.error("✨ [dev:override] Success! Active global seal-use binary is now updated:"); process.stdout.write(testRes.stdout); } if (import.meta.main) { main().catch((err) => { console.error(err); process.exit(1); }); }