#!/usr/bin/env bun import { chmodSync, copyFileSync, existsSync, mkdirSync, 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)), ".."); function resolveTargetBinaryPath(): string { // 1. Check which seal-use try { const whichRes = spawnSync("which", ["seal-use"], { encoding: "utf8" }); if (whichRes.status === 0 && whichRes.stdout) { const p = whichRes.stdout.trim(); if (p && existsSync(p)) { return p; } } } catch { // ignore } // 2. Fallback candidate paths in order of preference const candidates = [ "/opt/homebrew/bin/seal-use", "/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 } } copyFileSync(tmpTarget, targetPath); chmodSync(targetPath, 0o755); try { rmSync(tmpTarget, { force: true }); } catch { // ignore } // 4. Sync Agent Skill to ~/.agents/skills/seal-use const skillSource = join(root, "skills", "seal-use"); const skillTarget = join(homedir(), ".agents", "skills", "seal-use"); const legacySkill = join(homedir(), ".agents", "skills", "seal-home"); if (existsSync(legacySkill)) { try { rmSync(legacySkill, { recursive: true, force: true }); } catch {} } if (existsSync(skillSource)) { try { const { cpSync } = await import("node:fs"); mkdirSync(dirname(skillTarget), { recursive: true }); cpSync(skillSource, skillTarget, { recursive: true }); console.error(`✓ [dev:override] Synced agent skill to ${skillTarget}`); } catch (err) { console.error(`⚠️ [dev:override] Failed to sync agent skill: ${err}`); } } // 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 console.error("✨ [dev:override] Success! Active global seal-use binary is now updated:"); const testRes = spawnSync(targetPath, ["v"], { encoding: "utf8" }); if (testRes.status === 0 && testRes.stdout) { process.stdout.write(testRes.stdout); } else { console.error(testRes.stderr || "Failed to verify updated binary"); } } main().catch((err) => { console.error(err); process.exit(1); });