mirror of
https://github.com/openclaw/openclaw.git
synced 2026-03-26 17:32:16 +00:00
144 lines
3.6 KiB
TypeScript
144 lines
3.6 KiB
TypeScript
import os from "node:os";
|
|
import path from "node:path";
|
|
|
|
function normalize(value: string | undefined): string | undefined {
|
|
const trimmed = value?.trim();
|
|
if (!trimmed) {
|
|
return undefined;
|
|
}
|
|
if (trimmed === "undefined" || trimmed === "null") {
|
|
return undefined;
|
|
}
|
|
return trimmed;
|
|
}
|
|
|
|
export function resolveEffectiveHomeDir(
|
|
env: NodeJS.ProcessEnv = process.env,
|
|
homedir: () => string = os.homedir,
|
|
): string | undefined {
|
|
const raw = resolveRawHomeDir(env, homedir);
|
|
return raw ? path.resolve(raw) : undefined;
|
|
}
|
|
|
|
export function resolveOsHomeDir(
|
|
env: NodeJS.ProcessEnv = process.env,
|
|
homedir: () => string = os.homedir,
|
|
): string | undefined {
|
|
const raw = resolveRawOsHomeDir(env, homedir);
|
|
return raw ? path.resolve(raw) : undefined;
|
|
}
|
|
|
|
function resolveRawHomeDir(env: NodeJS.ProcessEnv, homedir: () => string): string | undefined {
|
|
const explicitHome = normalize(env.OPENCLAW_HOME);
|
|
if (explicitHome) {
|
|
if (explicitHome === "~" || explicitHome.startsWith("~/") || explicitHome.startsWith("~\\")) {
|
|
const fallbackHome = resolveRawOsHomeDir(env, homedir);
|
|
if (fallbackHome) {
|
|
return explicitHome.replace(/^~(?=$|[\\/])/, fallbackHome);
|
|
}
|
|
return undefined;
|
|
}
|
|
return explicitHome;
|
|
}
|
|
|
|
return resolveRawOsHomeDir(env, homedir);
|
|
}
|
|
|
|
function resolveRawOsHomeDir(env: NodeJS.ProcessEnv, homedir: () => string): string | undefined {
|
|
const envHome = normalize(env.HOME);
|
|
if (envHome) {
|
|
return envHome;
|
|
}
|
|
const userProfile = normalize(env.USERPROFILE);
|
|
if (userProfile) {
|
|
return userProfile;
|
|
}
|
|
return normalizeSafe(homedir);
|
|
}
|
|
|
|
function normalizeSafe(homedir: () => string): string | undefined {
|
|
try {
|
|
return normalize(homedir());
|
|
} catch {
|
|
return undefined;
|
|
}
|
|
}
|
|
|
|
export function resolveRequiredHomeDir(
|
|
env: NodeJS.ProcessEnv = process.env,
|
|
homedir: () => string = os.homedir,
|
|
): string {
|
|
return resolveEffectiveHomeDir(env, homedir) ?? path.resolve(process.cwd());
|
|
}
|
|
|
|
export function resolveRequiredOsHomeDir(
|
|
env: NodeJS.ProcessEnv = process.env,
|
|
homedir: () => string = os.homedir,
|
|
): string {
|
|
return resolveOsHomeDir(env, homedir) ?? path.resolve(process.cwd());
|
|
}
|
|
|
|
export function expandHomePrefix(
|
|
input: string,
|
|
opts?: {
|
|
home?: string;
|
|
env?: NodeJS.ProcessEnv;
|
|
homedir?: () => string;
|
|
},
|
|
): string {
|
|
if (!input.startsWith("~")) {
|
|
return input;
|
|
}
|
|
const home =
|
|
normalize(opts?.home) ??
|
|
resolveEffectiveHomeDir(opts?.env ?? process.env, opts?.homedir ?? os.homedir);
|
|
if (!home) {
|
|
return input;
|
|
}
|
|
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);
|
|
}
|
|
|
|
export function resolveOsHomeRelativePath(
|
|
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: resolveRequiredOsHomeDir(opts?.env ?? process.env, opts?.homedir ?? os.homedir),
|
|
env: opts?.env,
|
|
homedir: opts?.homedir,
|
|
});
|
|
return path.resolve(expanded);
|
|
}
|
|
return path.resolve(trimmed);
|
|
}
|