@layerfig/config
Version:
Layer and runtime-validate type-safe configs for JavaScript apps.
55 lines (52 loc) • 1.55 kB
JavaScript
const require_set = require('./set-Bl59_6Sr.cjs');
const node_fs = require_set.__toESM(require("node:fs"));
const zod = require_set.__toESM(require("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 (require_set.isPlainObject(target) && require_set.isPlainObject(source)) for (const key in source) {
const sourceValue = source[key];
const targetValue = target[key];
if (require_set.isPlainObject(sourceValue)) {
if (!targetValue || !require_set.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 (node_fs.default.existsSync(filePath)) {
const fileContent = node_fs.default.readFileSync(filePath, "utf8");
const fileContentResult = zod.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
Object.defineProperty(exports, 'merge', {
enumerable: true,
get: function () {
return merge;
}
});
Object.defineProperty(exports, 'readIfExist', {
enumerable: true,
get: function () {
return readIfExist;
}
});
//# sourceMappingURL=utils-q9FK7xDA.cjs.map