Something went wrong. Try again.
I'm going to build my own OpenClaw, with blackjack... and bun!
Something went wrong. Try again.
TypeScript
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950/** * core/config-store.ts – Low-level JSON file read/write helpers. * * Provides the persistence primitives that core/config.ts uses to load and * save the application configuration file (typically `.piclaw/config.json` * inside the workspace). Also used by the keychain (secure/keychain.ts) and * any module that needs simple JSON-file persistence. */
import { mkdirSync, readFileSync, writeFileSync } from "fs";import { dirname } from "path";
import { createLogger, debugSuppressedError } from "../utils/logger.js";
const log = createLogger("core.config-store");
/** * Read and parse a JSON file, returning its contents as a plain object. * Returns an empty object if the file does not exist, is unreadable, or * contains invalid JSON — callers always get a safe default. * * @param filePath - Absolute path to the JSON file. */export function readJsonConfig(filePath: string): Record<string, unknown> { try { const raw = readFileSync(filePath, "utf-8"); const parsed = JSON.parse(raw); if (parsed && typeof parsed === "object") return parsed as Record<string, unknown>; } catch (err) { debugSuppressedError(log, "Failed to read JSON config; returning an empty object.", err, { operation: "config_store.read_json_config", filePath, }); } return {};}
/** * Atomically write a JSON object to disk with pretty-printing. * Creates parent directories if they do not already exist. * * @param filePath - Absolute path to the target JSON file. * @param config - The object to serialize and persist. */export function writeJsonConfig(filePath: string, config: Record<string, unknown>): void { mkdirSync(dirname(filePath), { recursive: true }); const next = JSON.stringify(config, null, 2); writeFileSync(filePath, `${next}\n`, "utf-8");}