mirror of
https://github.com/moltbot/moltbot.git
synced 2026-03-21 16:41:56 +00:00
refactor: dedupe home relative path resolution
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
import fs from "node:fs";
|
||||
import os from "node:os";
|
||||
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";
|
||||
|
||||
/**
|
||||
@@ -93,19 +93,7 @@ function resolveUserPath(
|
||||
env: NodeJS.ProcessEnv = process.env,
|
||||
homedir: () => string = envHomedir(env),
|
||||
): string {
|
||||
const trimmed = input.trim();
|
||||
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);
|
||||
return resolveHomeRelativePath(input, { env, homedir });
|
||||
}
|
||||
|
||||
export const STATE_DIR = resolveStateDir();
|
||||
|
||||
@@ -75,3 +75,25 @@ export function expandHomePrefix(
|
||||
}
|
||||
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 { logVerbose, shouldLogVerbose } from "./globals.js";
|
||||
import {
|
||||
expandHomePrefix,
|
||||
resolveEffectiveHomeDir,
|
||||
resolveHomeRelativePath,
|
||||
resolveRequiredHomeDir,
|
||||
} from "./infra/home-dir.js";
|
||||
import { isPlainObject } from "./infra/plain-object.js";
|
||||
@@ -279,19 +279,7 @@ export function resolveUserPath(
|
||||
if (!input) {
|
||||
return "";
|
||||
}
|
||||
const trimmed = input.trim();
|
||||
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);
|
||||
return resolveHomeRelativePath(input, { env, homedir });
|
||||
}
|
||||
|
||||
export function resolveConfigDir(
|
||||
|
||||
Reference in New Issue
Block a user