fix(status): show OPENCLAW_HOME in shortened paths (#12091) (thanks @sebslight)

This commit is contained in:
Sebastian
2026-02-08 16:13:01 -05:00
parent ac90fad24b
commit 5ad0c70f8e
2 changed files with 49 additions and 8 deletions

View File

@@ -13,6 +13,8 @@ import {
resolveHomeDir,
resolveJidToE164,
resolveUserPath,
shortenHomeInString,
shortenHomePath,
sleep,
toWhatsappJid,
withWhatsAppPrefix,
@@ -146,6 +148,32 @@ describe("resolveHomeDir", () => {
});
});
describe("shortenHomePath", () => {
it("uses $OPENCLAW_HOME prefix when OPENCLAW_HOME is set", () => {
vi.stubEnv("OPENCLAW_HOME", "/srv/openclaw-home");
vi.stubEnv("HOME", "/home/other");
expect(shortenHomePath("/srv/openclaw-home/.openclaw/openclaw.json")).toBe(
"$OPENCLAW_HOME/.openclaw/openclaw.json",
);
vi.unstubAllEnvs();
});
});
describe("shortenHomeInString", () => {
it("uses $OPENCLAW_HOME replacement when OPENCLAW_HOME is set", () => {
vi.stubEnv("OPENCLAW_HOME", "/srv/openclaw-home");
vi.stubEnv("HOME", "/home/other");
expect(shortenHomeInString("config: /srv/openclaw-home/.openclaw/openclaw.json")).toBe(
"config: $OPENCLAW_HOME/.openclaw/openclaw.json",
);
vi.unstubAllEnvs();
});
});
describe("resolveJidToE164", () => {
it("resolves @lid via lidLookup when mapping file is missing", async () => {
const lidLookup = {

View File

@@ -278,19 +278,32 @@ export function resolveHomeDir(): string | undefined {
return resolveEffectiveHomeDir(process.env, os.homedir);
}
function resolveHomeDisplayPrefix(): { home: string; prefix: string } | undefined {
const home = resolveHomeDir();
if (!home) {
return undefined;
}
const explicitHome = process.env.OPENCLAW_HOME?.trim();
if (explicitHome) {
return { home, prefix: "$OPENCLAW_HOME" };
}
return { home, prefix: "~" };
}
export function shortenHomePath(input: string): string {
if (!input) {
return input;
}
const home = resolveHomeDir();
if (!home) {
const display = resolveHomeDisplayPrefix();
if (!display) {
return input;
}
const { home, prefix } = display;
if (input === home) {
return "~";
return prefix;
}
if (input.startsWith(`${home}/`)) {
return `~${input.slice(home.length)}`;
if (input.startsWith(`${home}/`) || input.startsWith(`${home}\\`)) {
return `${prefix}${input.slice(home.length)}`;
}
return input;
}
@@ -299,11 +312,11 @@ export function shortenHomeInString(input: string): string {
if (!input) {
return input;
}
const home = resolveHomeDir();
if (!home) {
const display = resolveHomeDisplayPrefix();
if (!display) {
return input;
}
return input.split(home).join("~");
return input.split(display.home).join(display.prefix);
}
export function displayPath(input: string): string {