UNPKG

everything-dev

Version:

A consolidated product package for building Module Federation apps with oRPC APIs.

102 lines (100 loc) 3.8 kB
import { createDefu } from "defu"; //#region src/merge.ts const BOS_CONFIG_ORDER = [ "extends", "account", "domain", "title", "description", "testnet", "staging", "repository", "app", "plugins", "shared" ]; const ARRAY_UNION_KEYS = new Set(["secrets"]); function isPlainObject(value) { return Boolean(value) && typeof value === "object" && !Array.isArray(value); } function unionArrays(a, b) { const aArr = Array.isArray(a) ? a : []; const bArr = Array.isArray(b) ? b : []; if (aArr.length === 0 && bArr.length === 0) return void 0; const seen = /* @__PURE__ */ new Set(); const result = []; for (const item of [...aArr, ...bArr]) { if (typeof item === "string") { if (seen.has(item)) continue; seen.add(item); } result.push(item); } return result; } function cleanNullSentinels(obj) { const out = {}; for (const [key, value] of Object.entries(obj)) { if (value === null || value === void 0) continue; if (isPlainObject(value)) { const cleaned = cleanNullSentinels(value); if (Object.keys(cleaned).length > 0) out[key] = cleaned; } else out[key] = value; } return out; } const bosConfigMerger = createDefu((obj, key, value) => { if (obj[key] === null) return true; if (value === null) { obj[key] = null; return true; } if (Array.isArray(obj[key]) && Array.isArray(value)) { if (ARRAY_UNION_KEYS.has(key)) obj[key] = unionArrays(obj[key], value); else obj[key] = value; return true; } return false; }); function resolveExtendsRef(extendsField, env) { if (!extendsField) return void 0; if (typeof extendsField === "string") return extendsField; return extendsField[env] ?? extendsField.production ?? Object.values(extendsField).find(Boolean); } function mergeBosConfigWithExtends(parent, child) { const { plugins: _ignoredParentPlugins, ...parentWithoutPlugins } = parent; const merged = bosConfigMerger(child, parentWithoutPlugins); if (child.plugins !== void 0 && isPlainObject(child.plugins)) merged.plugins = cleanNullSentinels(child.plugins); else delete merged.plugins; const mergedRecord = merged; if (isPlainObject(mergedRecord.app)) for (const entryVal of Object.values(mergedRecord.app)) { if (!isPlainObject(entryVal)) continue; for (const secretKey of ARRAY_UNION_KEYS) if (Array.isArray(entryVal[secretKey])) entryVal[secretKey] = unionArrays(entryVal[secretKey], [])?.filter(Boolean) ?? entryVal[secretKey]; } if (isPlainObject(mergedRecord.plugins)) for (const pluginVal of Object.values(mergedRecord.plugins)) { if (!isPlainObject(pluginVal)) continue; for (const secretKey of ARRAY_UNION_KEYS) if (Array.isArray(pluginVal[secretKey])) pluginVal[secretKey] = unionArrays(pluginVal[secretKey], [])?.filter(Boolean) ?? pluginVal[secretKey]; } return rebuildOrderedConfig(mergedRecord); } function mergeBosConfigWithTemplate(local, template) { return rebuildOrderedConfig(mergeJsonValuesPreservingLocalOrder(local, template)); } function mergeJsonValuesPreservingLocalOrder(local, template) { if (isPlainObject(local) && isPlainObject(template)) { const merged = {}; for (const key of Object.keys(local)) merged[key] = mergeJsonValuesPreservingLocalOrder(local[key], template[key]); for (const key of Object.keys(template)) if (!(key in merged)) merged[key] = template[key]; return merged; } return local ?? template; } function rebuildOrderedConfig(config) { const ordered = {}; for (const key of BOS_CONFIG_ORDER) if (key in config) ordered[key] = config[key]; for (const key of Object.keys(config)) if (!BOS_CONFIG_ORDER.includes(key)) ordered[key] = config[key]; return ordered; } //#endregion export { BOS_CONFIG_ORDER, bosConfigMerger, isPlainObject, mergeBosConfigWithExtends, mergeBosConfigWithTemplate, rebuildOrderedConfig, resolveExtendsRef }; //# sourceMappingURL=merge.mjs.map