feat(install): idempotent install/uninstall

- Repeat install = reinstall: auto-detect existing install, uninstall first, then install
- Repeat uninstall = no-op: if no install record found, exit 0 with message
- Uses spawnSync to re-exec uninstall phase during reinstall
This commit is contained in:
zhi
2026-03-03 16:39:40 +00:00
parent 6423dbcdfa
commit 83c03b6934

View File

@@ -6,7 +6,7 @@
import fs from "node:fs";
import path from "node:path";
import os from "node:os";
import { execFileSync } from "node:child_process";
import { execFileSync, spawnSync } from "node:child_process";
const modeArg = process.argv[2];
if (modeArg !== "--install" && modeArg !== "--uninstall") {
@@ -134,9 +134,26 @@ if (!fs.existsSync(OPENCLAW_CONFIG_PATH)) {
}
// ═══════════════════════════════════════════════════════════════════════════
// INSTALL
// INSTALL (with auto-reinstall if already installed)
// ═══════════════════════════════════════════════════════════════════════════
if (mode === "install") {
// Check if already installed - if so, uninstall first
const existingRecord = findLatestInstallRecord();
if (existingRecord) {
console.log("[dirigent] existing installation detected, uninstalling first...");
process.env.RECORD_FILE = existingRecord;
// Re-exec ourselves in uninstall mode
const result = spawnSync(process.execPath, [import.meta.filename, "--uninstall"], {
env: process.env,
stdio: ["inherit", "inherit", "inherit"],
});
if (result.status !== 0) {
console.error("[dirigent] reinstall failed during uninstall phase");
process.exit(1);
}
console.log("[dirigent] previous installation removed, proceeding with fresh install...");
}
fs.copyFileSync(OPENCLAW_CONFIG_PATH, BACKUP_PATH);
console.log(`[dirigent] safety backup: ${BACKUP_PATH}`);
@@ -248,8 +265,8 @@ if (mode === "install") {
else {
const recFile = env.RECORD_FILE || findLatestInstallRecord();
if (!recFile || !fs.existsSync(recFile)) {
console.error("[dirigent] no install record found. set RECORD_FILE=<path> to an install record.");
process.exit(1);
console.log("[dirigent] no install record found, nothing to uninstall.");
process.exit(0);
}
fs.copyFileSync(OPENCLAW_CONFIG_PATH, BACKUP_PATH);