Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
#!/usr/bin/env node
const isScalar = (v) => {
return !(v === null || v === undefined || typeof v === 'object');
};
const mergeObject = (a, b) => {
const keys = new Set([...Object.keys(a), ...Object.keys(b)]);
const accum = {};
for (const k of keys) {
const va = a[k];
const vb = b[k];
if (va !== undefined && vb !== undefined) {
if (isScalar(va)) {
accum[k] = vb;
} else {
accum[k] = merge(va, vb);
}
} else {
accum[k] = [va, vb].find(e => e !== undefined);
}
}
return accum;
};
const mergeArray = (a, b) => {
return [...a, ...b];
};
const merge = (a, b) => {
if (Array.isArray(a)) {
return mergeArray(a, b);
} else {
return mergeObject(a, b);
}
};
const tsconfigPaths = process.argv.slice(2);
const tsconfigs = tsconfigPaths.map(path => require(`./${path}`));
const merged = tsconfigs.reduce((accum, config) => {
return merge(accum, config);
}, {});
console.log(JSON.stringify(merged));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment