openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
29 lines (28 loc) • 940 B
JavaScript
import fs from "node:fs";
import path from "node:path";
import { resolvePathPrefixSync } from "@openclaw/fs-safe/advanced";
import { hasNodeErrorCode } from "@openclaw/fs-safe/path";
import { tryReadJsonSync, writeJsonSync } from "@openclaw/fs-safe/json";
//#region src/infra/json-file.ts
function resolveJsonSaveTarget(e) {
if (!fs.lstatSync(e, { throwIfNoEntry: false })?.isSymbolicLink()) return e;
const t = resolvePathPrefixSync(e);
const n = [t.existingPath, ...t.unresolvedSegments].join(path.sep);
fs.statSync(path.dirname(n));
return n;
}
function writeJsonTarget(e, t) {
writeJsonSync(resolveJsonSaveTarget(e), t);
}
function loadJsonFileThroughSymlink(e) {
let t;
try {
t = fs.realpathSync(e);
} catch (e) {
if (hasNodeErrorCode(e, "ENOENT") || hasNodeErrorCode(e, "ELOOP")) return;
throw e;
}
return tryReadJsonSync(t) ?? void 0;
}
//#endregion
export { writeJsonTarget as n, loadJsonFileThroughSymlink as t };