From fc2cdf0eeeb49d69d7faaf59bb93f8026d414b7f Mon Sep 17 00:00:00 2001 From: zhi Date: Sat, 7 Mar 2026 18:39:55 +0000 Subject: [PATCH] refactor: simplify openclaw dir resolution priority Priority: --openclaw-profile-path arg > env > ~/.openclaw Remove openclaw CLI command attempts, simplify to 3 clear sources --- scripts/install-dirigent-openclaw.mjs | 76 +++++++++++++-------------- 1 file changed, 37 insertions(+), 39 deletions(-) diff --git a/scripts/install-dirigent-openclaw.mjs b/scripts/install-dirigent-openclaw.mjs index a8a70e5..3c56b27 100755 --- a/scripts/install-dirigent-openclaw.mjs +++ b/scripts/install-dirigent-openclaw.mjs @@ -7,9 +7,9 @@ * node install-dirigent-openclaw.mjs --uninstall Remove plugin config & files * node install-dirigent-openclaw.mjs --update Pull latest from git and reinstall * - * OpenClaw directory resolution (in order): - * 1. $OPENCLAW_DIR environment variable - * 2. `openclaw config get dataDir` command output + * OpenClaw directory resolution (priority order): + * 1. --openclaw-profile-path CLI argument + * 2. $OPENCLAW_DIR environment variable * 3. ~/.openclaw (fallback) */ import fs from "node:fs"; @@ -17,54 +17,51 @@ import path from "node:path"; import os from "node:os"; import { execFileSync, spawnSync } from "node:child_process"; -// ── Mode parsing ────────────────────────────────────────────────────────── +// ── Arg parsing ─────────────────────────────────────────────────────────── const VALID_MODES = ["--install", "--uninstall", "--update"]; -const modeArg = process.argv[2]; -if (!VALID_MODES.includes(modeArg)) { - console.error("Usage: install-dirigent-openclaw.mjs --install | --uninstall | --update"); +let modeArg = null; +let argOpenClawDir = null; + +for (let i = 2; i < process.argv.length; i++) { + const arg = process.argv[i]; + if (VALID_MODES.includes(arg)) { + modeArg = arg; + } else if (arg === "--openclaw-profile-path" && i + 1 < process.argv.length) { + argOpenClawDir = process.argv[++i]; + } else if (arg.startsWith("--openclaw-profile-path=")) { + argOpenClawDir = arg.split("=").slice(1).join("="); + } +} + +if (!modeArg) { + console.error("Usage: install-dirigent-openclaw.mjs --install | --uninstall | --update [--openclaw-profile-path ]"); process.exit(2); } const mode = modeArg.slice(2); // "install" | "uninstall" | "update" // ── OpenClaw directory resolution ───────────────────────────────────────── +// Priority: --openclaw-profile-path arg > $OPENCLAW_DIR env > ~/.openclaw function resolveOpenClawDir() { - // 1. Explicit env var + // 1. CLI argument + if (argOpenClawDir) { + const dir = argOpenClawDir.replace(/^~(?=$|\/)/, os.homedir()); + if (fs.existsSync(dir)) return dir; + console.error(`[dirigent] --openclaw-profile-path=${dir} does not exist`); + process.exit(1); + } + + // 2. Environment variable if (process.env.OPENCLAW_DIR) { const dir = process.env.OPENCLAW_DIR.replace(/^~(?=$|\/)/, os.homedir()); if (fs.existsSync(dir)) return dir; - console.warn(`[dirigent] OPENCLAW_DIR=${dir} does not exist, trying other methods...`); + console.warn(`[dirigent] OPENCLAW_DIR=${dir} does not exist, falling back...`); } - // 2. Ask openclaw CLI - try { - const out = execFileSync("openclaw", ["config", "get", "dataDir"], { - encoding: "utf8", - timeout: 5000, - }).trim(); - if (out && fs.existsSync(out)) return out; - } catch { - // openclaw not found or command failed - } - - // 3. Try openclaw config file location to infer dir - try { - const out = execFileSync("openclaw", ["config", "path"], { - encoding: "utf8", - timeout: 5000, - }).trim(); - if (out) { - const dir = path.dirname(out); - if (fs.existsSync(dir)) return dir; - } - } catch { - // ignore - } - - // 4. Fallback + // 3. Fallback const fallback = path.join(os.homedir(), ".openclaw"); if (fs.existsSync(fallback)) return fallback; - console.error("[dirigent] cannot resolve OpenClaw directory. Set OPENCLAW_DIR or ensure openclaw CLI is available."); + console.error("[dirigent] cannot resolve OpenClaw directory. Use --openclaw-profile-path or set OPENCLAW_DIR."); process.exit(1); } @@ -106,10 +103,11 @@ if (mode === "update") { process.exit(1); } - // Re-exec as install (the updated script may differ) + // Re-exec as install (the updated script may differ), pass through openclaw dir const updatedScript = path.join(REPO_ROOT, "scripts", "install-dirigent-openclaw.mjs"); - const result = spawnSync(process.execPath, [updatedScript, "--install"], { - env: { ...process.env, OPENCLAW_DIR }, + const installArgs = [updatedScript, "--install", "--openclaw-profile-path", OPENCLAW_DIR]; + const result = spawnSync(process.execPath, installArgs, { + env: process.env, stdio: "inherit", cwd: REPO_ROOT, });