mirror of
https://github.com/openclaw/openclaw.git
synced 2026-04-19 11:58:38 +00:00
refactor: dedupe home relative path resolution
This commit is contained in:
@@ -1,7 +1,7 @@
|
|||||||
import fs from "node:fs";
|
import fs from "node:fs";
|
||||||
import os from "node:os";
|
import os from "node:os";
|
||||||
import path from "node:path";
|
import path from "node:path";
|
||||||
import { expandHomePrefix, resolveRequiredHomeDir } from "../infra/home-dir.js";
|
import { resolveHomeRelativePath, resolveRequiredHomeDir } from "../infra/home-dir.js";
|
||||||
import type { OpenClawConfig } from "./types.js";
|
import type { OpenClawConfig } from "./types.js";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -93,19 +93,7 @@ function resolveUserPath(
|
|||||||
env: NodeJS.ProcessEnv = process.env,
|
env: NodeJS.ProcessEnv = process.env,
|
||||||
homedir: () => string = envHomedir(env),
|
homedir: () => string = envHomedir(env),
|
||||||
): string {
|
): string {
|
||||||
const trimmed = input.trim();
|
return resolveHomeRelativePath(input, { env, homedir });
|
||||||
if (!trimmed) {
|
|
||||||
return trimmed;
|
|
||||||
}
|
|
||||||
if (trimmed.startsWith("~")) {
|
|
||||||
const expanded = expandHomePrefix(trimmed, {
|
|
||||||
home: resolveRequiredHomeDir(env, homedir),
|
|
||||||
env,
|
|
||||||
homedir,
|
|
||||||
});
|
|
||||||
return path.resolve(expanded);
|
|
||||||
}
|
|
||||||
return path.resolve(trimmed);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export const STATE_DIR = resolveStateDir();
|
export const STATE_DIR = resolveStateDir();
|
||||||
|
|||||||
@@ -75,3 +75,25 @@ export function expandHomePrefix(
|
|||||||
}
|
}
|
||||||
return input.replace(/^~(?=$|[\\/])/, home);
|
return input.replace(/^~(?=$|[\\/])/, home);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function resolveHomeRelativePath(
|
||||||
|
input: string,
|
||||||
|
opts?: {
|
||||||
|
env?: NodeJS.ProcessEnv;
|
||||||
|
homedir?: () => string;
|
||||||
|
},
|
||||||
|
): string {
|
||||||
|
const trimmed = input.trim();
|
||||||
|
if (!trimmed) {
|
||||||
|
return trimmed;
|
||||||
|
}
|
||||||
|
if (trimmed.startsWith("~")) {
|
||||||
|
const expanded = expandHomePrefix(trimmed, {
|
||||||
|
home: resolveRequiredHomeDir(opts?.env ?? process.env, opts?.homedir ?? os.homedir),
|
||||||
|
env: opts?.env,
|
||||||
|
homedir: opts?.homedir,
|
||||||
|
});
|
||||||
|
return path.resolve(expanded);
|
||||||
|
}
|
||||||
|
return path.resolve(trimmed);
|
||||||
|
}
|
||||||
|
|||||||
16
src/utils.ts
16
src/utils.ts
@@ -4,8 +4,8 @@ import path from "node:path";
|
|||||||
import { resolveOAuthDir } from "./config/paths.js";
|
import { resolveOAuthDir } from "./config/paths.js";
|
||||||
import { logVerbose, shouldLogVerbose } from "./globals.js";
|
import { logVerbose, shouldLogVerbose } from "./globals.js";
|
||||||
import {
|
import {
|
||||||
expandHomePrefix,
|
|
||||||
resolveEffectiveHomeDir,
|
resolveEffectiveHomeDir,
|
||||||
|
resolveHomeRelativePath,
|
||||||
resolveRequiredHomeDir,
|
resolveRequiredHomeDir,
|
||||||
} from "./infra/home-dir.js";
|
} from "./infra/home-dir.js";
|
||||||
import { isPlainObject } from "./infra/plain-object.js";
|
import { isPlainObject } from "./infra/plain-object.js";
|
||||||
@@ -279,19 +279,7 @@ export function resolveUserPath(
|
|||||||
if (!input) {
|
if (!input) {
|
||||||
return "";
|
return "";
|
||||||
}
|
}
|
||||||
const trimmed = input.trim();
|
return resolveHomeRelativePath(input, { env, homedir });
|
||||||
if (!trimmed) {
|
|
||||||
return trimmed;
|
|
||||||
}
|
|
||||||
if (trimmed.startsWith("~")) {
|
|
||||||
const expanded = expandHomePrefix(trimmed, {
|
|
||||||
home: resolveRequiredHomeDir(env, homedir),
|
|
||||||
env,
|
|
||||||
homedir,
|
|
||||||
});
|
|
||||||
return path.resolve(expanded);
|
|
||||||
}
|
|
||||||
return path.resolve(trimmed);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export function resolveConfigDir(
|
export function resolveConfigDir(
|
||||||
|
|||||||
Reference in New Issue
Block a user