Files
flynn/src/config/loader.ts
T
William Valentin c2cc052694 feat(02-01): implement deepMerge and overlay-aware loadConfig with tests
- Add deepMerge utility for recursive object merging (arrays replace, not concat)
- Extend loadConfig with optional overlayPath parameter
- Merge happens before env var expansion and Zod validation
- Add 6 deepMerge unit tests and 4 overlay integration tests
- Re-export deepMerge from config/index.ts
- All 1087 existing tests still pass
2026-02-09 20:56:29 -08:00

86 lines
2.5 KiB
TypeScript

import { readFileSync } from 'fs';
import { parse } from 'yaml';
import { configSchema, type Config } from './schema.js';
function expandEnvVars(value: string): string {
return value.replace(/\$\{([^}]+)\}/g, (_, envVar) => {
const envValue = process.env[envVar];
if (envValue === undefined) {
throw new Error(`Environment variable ${envVar} is not set`);
}
return envValue;
});
}
function expandEnvVarsInObject(obj: unknown): unknown {
if (typeof obj === 'string') {
return expandEnvVars(obj);
}
if (Array.isArray(obj)) {
return obj.map(expandEnvVarsInObject);
}
if (obj !== null && typeof obj === 'object') {
const result: Record<string, unknown> = {};
for (const [key, value] of Object.entries(obj)) {
result[key] = expandEnvVarsInObject(value);
}
return result;
}
return obj;
}
/**
* Deep merge two plain objects. Overlay values override base values.
* Arrays in overlay replace base arrays (not concatenated).
* null in overlay explicitly sets key to null.
* Only plain objects recurse; arrays, primitives, and null are overwritten.
*/
export function deepMerge(
base: Record<string, unknown>,
overlay: Record<string, unknown>,
): Record<string, unknown> {
const result = { ...base };
for (const [key, value] of Object.entries(overlay)) {
if (
value !== null &&
typeof value === 'object' &&
!Array.isArray(value) &&
typeof result[key] === 'object' &&
result[key] !== null &&
!Array.isArray(result[key])
) {
result[key] = deepMerge(
result[key] as Record<string, unknown>,
value as Record<string, unknown>,
);
} else {
result[key] = value;
}
}
return result;
}
/**
* Load and validate a Flynn config from YAML file(s).
* If overlayPath is provided, the overlay YAML is deep-merged on top of
* the base config BEFORE env var expansion and Zod validation.
*/
export function loadConfig(configPath: string, overlayPath?: string): Config {
const rawContent = readFileSync(configPath, 'utf-8');
let rawConfig = parse(rawContent);
if (overlayPath) {
const overlayContent = readFileSync(overlayPath, 'utf-8');
const overlayConfig = parse(overlayContent);
if (overlayConfig && typeof overlayConfig === 'object') {
rawConfig = deepMerge(
rawConfig as Record<string, unknown>,
overlayConfig as Record<string, unknown>,
);
}
}
const expandedConfig = expandEnvVarsInObject(rawConfig);
return configSchema.parse(expandedConfig);
}