Safely reads/parses JSON file, error-handled, part of fs-safe utilities.
import * as fs from "node:fs";
import * as path from "node:path";
import * as crypto from "node:crypto";
function isPlainObject(v: unknown): v is Record<string, unknown> {
return (
typeof v === "object" &&
v !== null &&
!Array.isArray(v) &&
Object.getPrototypeOf(v) === Object.prototype
);
}
/**
* Recursively fills missing keys in `loaded` from `defaults`.
* Loaded values always win; defaults only fill gaps. Arrays and scalars
* are replaced wholesale (not merged).
*/
function deepMergeDefaults<T>(defaults: T, loaded: T): T {
if (!isPlainObject(defaults) || !isPlainObject(loaded)) return loaded;
const result: Record<string, unknown> = { ...(defaults as Record<string, unknown>) };
for (const key of Object.keys(loaded as Record<string, unknown>)) {
const lv
... (truncated -- full source via MCP)
See the full source, get the GitHub permalink, and search 40K more like it.
Get a free API key