@layerfig/config
Version:
Layer and runtime-validate type-safe configs for JavaScript apps.
44 lines (41 loc) • 1.22 kB
JavaScript
import { isPlainObject } from "./set-LPILcL1N.js";
import fs from "node:fs";
import { z } from "zod";
//#region src/utils/merge.ts
function merge(target, ...sources) {
if (!sources.length) return target;
const source = sources.shift();
if (!source) return merge(target, ...sources);
if (isPlainObject(target) && isPlainObject(source)) for (const key in source) {
const sourceValue = source[key];
const targetValue = target[key];
if (isPlainObject(sourceValue)) {
if (!targetValue || !isPlainObject(targetValue)) target[key] = {};
merge(target[key], sourceValue);
} else target[key] = sourceValue;
}
return merge(target, ...sources);
}
//#endregion
//#region src/utils/read-if-exist.ts
function readIfExist(filePath) {
if (fs.existsSync(filePath)) {
const fileContent = fs.readFileSync(filePath, "utf8");
const fileContentResult = z.string().safeParse(fileContent);
if (fileContentResult.success) return {
ok: true,
data: fileContentResult.data
};
return {
ok: false,
error: "File content is not a string."
};
}
return {
ok: false,
error: `File "${filePath}" does not exist`
};
}
//#endregion
export { merge, readIfExist };
//# sourceMappingURL=utils-B21a12rH.js.map