-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathindex.js
38 lines (30 loc) · 947 Bytes
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
'use strict';
const readFileSync = require('fs').readFileSync;
const jsYaml = require('js-yaml');
const glob = require('glob');
const _ = require('lodash');
function readAsJSON(fileName) {
const fileBuffer = readFileSync(fileName);
const fileString = fileBuffer.toString();
return jsYaml.load(fileString);
}
/**
* Merge the given YAML file names into a single YAML document
*
* @param {array} from the file paths to read from
* @return {string} the output YAML file
*/
function yamlMerge(...from) {
const files = from
.reduce((arr, el) => arr.concat(glob.sync(el)), [])
.map((path) => readAsJSON(path));
const outputJSON = _.mergeWith({}, ...files, (objValue, srcValue) => {
if (Array.isArray(objValue) && Array.isArray(srcValue)) {
return [...objValue, ...srcValue];
}
// handle it just like with _.merge
return undefined;
});
return jsYaml.dump(outputJSON);
}
module.exports = yamlMerge;