Files
seal-use/scripts/dev-override.ts
T

167 lines
5.8 KiB
TypeScript
Executable File

#!/usr/bin/env bun
import { accessSync, chmodSync, constants, 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) && !p.startsWith("/opt/homebrew")) {
// Preserve native binary symlinks, but never overwrite an npm-linked source entry or stale Homebrew.
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 = [
join(homedir(), ".local", "bin", "seal-use"),
"/usr/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);
try {
mkdirSync(targetDir, { recursive: true });
accessSync(targetDir, constants.W_OK);
if (existsSync(targetPath)) {
accessSync(targetPath, constants.W_OK);
}
} catch {
console.error(`❌ [dev:override] Permission denied: Target path '${targetPath}' requires root privileges.`);
console.error(`👉 Next action: run with sudo: 'sudo bun run dev:override'`);
console.error(`💡 Tip: To develop without sudo, migrate to user directory first: 'seal-use update --user'`);
process.exit(1);
}
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
}
// Clean up stale /opt/homebrew/bin/seal-use if present
for (const stale of ["/opt/homebrew/bin/seal-use", "/opt/homebrew/bin/seal-home"]) {
if (existsSync(stale) && stale !== targetPath) {
try {
rmSync(stale, { force: true });
console.error(`✓ [dev:override] Cleaned stale Homebrew binary at ${stale}`);
} 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);
});
}