refactor: simplify openclaw dir resolution priority

Priority: --openclaw-profile-path arg >  env > ~/.openclaw
Remove openclaw CLI command attempts, simplify to 3 clear sources
This commit is contained in:
zhi
2026-03-07 18:39:55 +00:00
parent 211ad9246f
commit fc2cdf0eee

View File

@@ -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 <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 <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,
});