mirror of
https://github.com/openclaw/openclaw.git
synced 2026-02-19 18:39:20 -05:00
fix(status): show OPENCLAW_HOME in shortened paths (#12091) (thanks @sebslight)
This commit is contained in:
@@ -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 = {
|
||||
|
||||
29
src/utils.ts
29
src/utils.ts
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user