- 在 scripts/install.sh 中默认安装至用户目录 ~/.local/bin,并自动清理历史遗留的 /opt/homebrew/bin/seal-use - 在 scripts/dev-override.ts 中优先命中 ~/.local/bin/seal-use,忽略 Homebrew 路径并自动清理残留 - 在 seal-use update 中支持 --system(提权迁移至 /usr/local/bin)与 --user(降权回归至 ~/.local/bin)选项,添加 seal-use migrate 别名 - 迁移与更新时自动清理另一端的非活动实体二进制(恪守 Single Binary Invariant) - 自更新权限预检:当目标位于系统目录且缺少写权限时,友好提示 sudo seal-use update 而非底层报错 - 在 seal-use doctor 中增加执行模式识别与多份二进制(PATH 阴影)检测告警
155 lines
5.3 KiB
TypeScript
Executable File
155 lines
5.3 KiB
TypeScript
Executable File
#!/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) && !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);
|
|
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
|
|
}
|
|
|
|
// 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);
|
|
});
|
|
}
|