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:
@@ -7,9 +7,9 @@
|
|||||||
* node install-dirigent-openclaw.mjs --uninstall Remove plugin config & files
|
* node install-dirigent-openclaw.mjs --uninstall Remove plugin config & files
|
||||||
* node install-dirigent-openclaw.mjs --update Pull latest from git and reinstall
|
* node install-dirigent-openclaw.mjs --update Pull latest from git and reinstall
|
||||||
*
|
*
|
||||||
* OpenClaw directory resolution (in order):
|
* OpenClaw directory resolution (priority order):
|
||||||
* 1. $OPENCLAW_DIR environment variable
|
* 1. --openclaw-profile-path <path> CLI argument
|
||||||
* 2. `openclaw config get dataDir` command output
|
* 2. $OPENCLAW_DIR environment variable
|
||||||
* 3. ~/.openclaw (fallback)
|
* 3. ~/.openclaw (fallback)
|
||||||
*/
|
*/
|
||||||
import fs from "node:fs";
|
import fs from "node:fs";
|
||||||
@@ -17,54 +17,51 @@ import path from "node:path";
|
|||||||
import os from "node:os";
|
import os from "node:os";
|
||||||
import { execFileSync, spawnSync } from "node:child_process";
|
import { execFileSync, spawnSync } from "node:child_process";
|
||||||
|
|
||||||
// ── Mode parsing ──────────────────────────────────────────────────────────
|
// ── Arg parsing ───────────────────────────────────────────────────────────
|
||||||
const VALID_MODES = ["--install", "--uninstall", "--update"];
|
const VALID_MODES = ["--install", "--uninstall", "--update"];
|
||||||
const modeArg = process.argv[2];
|
let modeArg = null;
|
||||||
if (!VALID_MODES.includes(modeArg)) {
|
let argOpenClawDir = null;
|
||||||
console.error("Usage: install-dirigent-openclaw.mjs --install | --uninstall | --update");
|
|
||||||
|
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);
|
process.exit(2);
|
||||||
}
|
}
|
||||||
const mode = modeArg.slice(2); // "install" | "uninstall" | "update"
|
const mode = modeArg.slice(2); // "install" | "uninstall" | "update"
|
||||||
|
|
||||||
// ── OpenClaw directory resolution ─────────────────────────────────────────
|
// ── OpenClaw directory resolution ─────────────────────────────────────────
|
||||||
|
// Priority: --openclaw-profile-path arg > $OPENCLAW_DIR env > ~/.openclaw
|
||||||
function resolveOpenClawDir() {
|
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) {
|
if (process.env.OPENCLAW_DIR) {
|
||||||
const dir = process.env.OPENCLAW_DIR.replace(/^~(?=$|\/)/, os.homedir());
|
const dir = process.env.OPENCLAW_DIR.replace(/^~(?=$|\/)/, os.homedir());
|
||||||
if (fs.existsSync(dir)) return dir;
|
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
|
// 3. Fallback
|
||||||
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
|
|
||||||
const fallback = path.join(os.homedir(), ".openclaw");
|
const fallback = path.join(os.homedir(), ".openclaw");
|
||||||
if (fs.existsSync(fallback)) return fallback;
|
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);
|
process.exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -106,10 +103,11 @@ if (mode === "update") {
|
|||||||
process.exit(1);
|
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 updatedScript = path.join(REPO_ROOT, "scripts", "install-dirigent-openclaw.mjs");
|
||||||
const result = spawnSync(process.execPath, [updatedScript, "--install"], {
|
const installArgs = [updatedScript, "--install", "--openclaw-profile-path", OPENCLAW_DIR];
|
||||||
env: { ...process.env, OPENCLAW_DIR },
|
const result = spawnSync(process.execPath, installArgs, {
|
||||||
|
env: process.env,
|
||||||
stdio: "inherit",
|
stdio: "inherit",
|
||||||
cwd: REPO_ROOT,
|
cwd: REPO_ROOT,
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user