openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
107 lines (106 loc) • 4.96 kB
JavaScript
import { n as resolveEffectiveHomeDir, r as resolveOsHomeDir, t as normalizeHomeDirValue } from "./home-dir-zhqynwRJ.mjs";
import { a as resolveGlobalSingleton } from "./global-singleton-D7yf05nR.mjs";
import { n as tryProcessCwd } from "./safe-cwd-CZqaxroH.mjs";
import path from "node:path";
import os from "node:os";
//#region src/infra/home-dir.ts
function getUserPathBaseDirectory() {
return resolveGlobalSingleton(Symbol.for("openclaw.userPathBaseDirectory"), () => {
const { AsyncLocalStorage: e } = process.getBuiltinModule("async_hooks");
return new e();
});
}
/** Preserve invocation-relative paths while an updater parks its physical cwd. */
function withUserPathBaseDirectory(e, t) {
return e === void 0 ? t() : getUserPathBaseDirectory().run(path.resolve(e), t);
}
/** Resolves the effective home or falls back to cwd when no home source exists. */
function resolveRequiredHomeDir(e = process.env, t = os.homedir) {
const n = resolveEffectiveHomeDir(e, t) ?? tryProcessCwd();
if (n) return path.resolve(n);
throw new Error("Unable to resolve an OpenClaw home: set OPENCLAW_HOME, HOME, or USERPROFILE, or run from an existing directory.");
}
/** Resolves the OS home or falls back to cwd when no OS home source exists. */
function resolveRequiredOsHomeDir(e = process.env, t = os.homedir) {
const n = resolveOsHomeDir(e, t) ?? tryProcessCwd();
if (n) return path.resolve(n);
throw new Error("Unable to resolve an OS home: set HOME or USERPROFILE, or run from an existing directory.");
}
/** Expands leading `~`, `~/`, or `~\` with the effective home when one is known. */
function expandHomePrefix(e, t) {
if (!e.startsWith("~")) return e;
const n = normalizeHomeDirValue(t?.home) ?? resolveEffectiveHomeDir(t?.env ?? process.env, t?.homedir ?? os.homedir);
if (!n) return e;
return e.replace(/^~(?=$|[\\/])/, () => n);
}
function resolveAgainstUserBase(e) {
const t = getUserPathBaseDirectory().getStore();
return t ? path.resolve(t, e) : path.resolve(e);
}
function resolvePathWithHome(e, t, n, r = resolveAgainstUserBase) {
const i = e.trim();
if (!i) return i;
return r(i.startsWith("~") ? expandHomePrefix(i, {
home: n(t?.env ?? process.env, t?.homedir ?? os.homedir),
env: t?.env,
homedir: t?.homedir
}) : i);
}
function notADirectory(e) {
return Object.assign(/* @__PURE__ */ new Error(`ENOTDIR: not a directory, '${e}'`), {
code: "ENOTDIR",
path: e
});
}
/**
* Resolves a path about to be opened or written, like `path.resolve`, except that a symlink
* followed by `..` is replaced by its target first, as the OS does: with `link -> a/b`,
* `link/../x` names `a/x`, not the lexical `./x`. Every other segment keeps its spelling, so a
* final symlink (for example a `plugins install --link` source) is recorded as typed.
*
* For a path with `..`, throws the error the OS would give (`ENOENT`, `ENOTDIR`, `EACCES`)
* when a segment before `..` cannot be traversed, or when a trailing `/` or `/.` names an
* existing non-directory. Returning such a path would let later normalization reopen a
* lexical neighbor.
*/
function resolveFilesystemPath(e) {
if (process.platform === "win32" || !e.split("/").includes("..")) return resolveAgainstUserBase(e);
const t = path.isAbsolute(e) ? e : `${getUserPathBaseDirectory().getStore() ?? process.cwd()}/${e}`;
const n = process.getBuiltinModule("fs");
const r = t.split("/");
let i = "/";
for (const e of r) if (e === "..") {
const e = n.lstatSync(i).isSymbolicLink() ? n.realpathSync(i) : i;
if (!n.statSync(e).isDirectory()) throw notADirectory(e);
i = path.dirname(e);
} else if (e !== "" && e !== ".") i = path.join(i, e);
const a = r.at(-1);
if (a === "" || a === ".") {
const e = n.statSync(i, { throwIfNoEntry: false });
if (e && !e.isDirectory()) throw notADirectory(i);
}
return i;
}
/** Resolves a user-supplied path after trimming and expanding against the effective home. */
function resolveHomeRelativePath(e, t) {
return resolvePathWithHome(e, t, resolveRequiredHomeDir);
}
/** Resolves a user path against the effective home, preserving an empty input. */
function resolveUserPath(e, t = process.env, n = os.homedir) {
if (!e) return "";
return resolveHomeRelativePath(e, {
env: t,
homedir: n
});
}
/** Resolves a user path like `resolveUserPath`, following symlinks before `..` as the OS does. */
function resolveUserFilesystemPath(e) {
if (!e) return "";
return resolvePathWithHome(e, void 0, resolveRequiredHomeDir, resolveFilesystemPath);
}
/** Resolves a user-supplied path against the OS home, ignoring OPENCLAW_HOME. */
function resolveOsHomeRelativePath(e, t) {
return resolvePathWithHome(e, t, resolveRequiredOsHomeDir);
}
//#endregion
export { resolveRequiredHomeDir as a, resolveUserPath as c, resolveOsHomeRelativePath as i, withUserPathBaseDirectory as l, resolveFilesystemPath as n, resolveRequiredOsHomeDir as o, resolveHomeRelativePath as r, resolveUserFilesystemPath as s, expandHomePrefix as t };