From 83c03b69346b0afba21e864ae008c01c5ed1f9ad Mon Sep 17 00:00:00 2001 From: zhi Date: Tue, 3 Mar 2026 16:39:40 +0000 Subject: [PATCH] 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 --- scripts/install-dirigent-openclaw.mjs | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/scripts/install-dirigent-openclaw.mjs b/scripts/install-dirigent-openclaw.mjs index c08c755..99bfde0 100755 --- a/scripts/install-dirigent-openclaw.mjs +++ b/scripts/install-dirigent-openclaw.mjs @@ -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= 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);