Skip to content

Commit

Permalink
feat: add glob matching
Browse files Browse the repository at this point in the history
  • Loading branch information
Pedro Cerejo authored and alexlafroscia committed Jun 25, 2021
1 parent 229c2a0 commit ded7017
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 3 deletions.
2 changes: 0 additions & 2 deletions bin/yaml-merge
Expand Up @@ -2,12 +2,10 @@

'use strict';

const resolve = require('path').resolve;
const yamlMerge = require('../index.js');

const files = process.argv
.slice(2)
.map((path) => resolve(path));
const outputFile = yamlMerge(...files);

process.stdout.write(outputFile);
6 changes: 5 additions & 1 deletion index.js
Expand Up @@ -2,6 +2,7 @@

const readFileSync = require('fs').readFileSync;
const jsYaml = require('js-yaml');
const glob = require('glob');
const _ = require('lodash');

function readAsJSON(fileName) {
Expand All @@ -18,7 +19,10 @@ function readAsJSON(fileName) {
* @return {string} the output YAML file
*/
function yamlMerge(...from) {
const files = from.map((path) => readAsJSON(path));
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];
Expand Down
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -18,6 +18,7 @@
"index.js"
],
"dependencies": {
"glob": "^7.1.7",
"js-yaml": "^4.0.0",
"lodash": "^4.17.20"
},
Expand Down
35 changes: 35 additions & 0 deletions test/lib-test.js
Expand Up @@ -3,6 +3,7 @@
'use strict';

const resolve = require('path').resolve;
const join = require('path').join;
const expect = require('chai').expect;
const stripIndent = require('common-tags').stripIndent;
const merge = require('../index.js');
Expand All @@ -11,6 +12,10 @@ function fixtureFiles(...names) {
return names.map((fileName) => resolve(__dirname, './fixtures', fileName));
}

function globFiles(...globs) {
return globs.map((glob) => join(__dirname, './fixtures', glob));
}

describe('merge logic', function () {
it('merges multiple YAML files', function () {
const output = merge(...fixtureFiles('basic/a.yml', 'basic/b.yml'));
Expand Down Expand Up @@ -60,3 +65,33 @@ describe('merge logic', function () {
);
});
});

describe('glob logic', function () {
it('merges multiple YAML files', function () {
const output = merge(...globFiles('basic/*.yml'));

expect(output).to.equal(
stripIndent`
a:
foo: bar
b:
foo: bar
` + '\n'
);
});

it('merges multiple YAML files (to glob or not to glob)', function () {
const output = merge(...globFiles('basic/*.yml', 'merge/a.yml'));

expect(output).to.equal(
stripIndent`
a:
foo: bar
b:
foo: bar
key:
first_value: a
` + '\n'
);
});
});
12 changes: 12 additions & 0 deletions yarn.lock
Expand Up @@ -1483,6 +1483,18 @@ glob@7.1.6, glob@^7.0.0, glob@^7.1.3:
once "^1.3.0"
path-is-absolute "^1.0.0"

glob@^7.1.7:
version "7.1.7"
resolved "https://registry.yarnpkg.com/glob/-/glob-7.1.7.tgz#3b193e9233f01d42d0b3f78294bbeeb418f94a90"
integrity sha512-OvD9ENzPLbegENnYP5UUfJIirTg4+XwMWGaQfQTY0JenxNvvIKP3U3/tAQSPIu/lHxXYSZmpXlUHeqAIdKzBLQ==
dependencies:
fs.realpath "^1.0.0"
inflight "^1.0.4"
inherits "2"
minimatch "^3.0.4"
once "^1.3.0"
path-is-absolute "^1.0.0"

globals@^11.1.0:
version "11.12.0"
resolved "https://registry.yarnpkg.com/globals/-/globals-11.12.0.tgz#ab8795338868a0babd8525758018c2a7eb95c42e"
Expand Down

0 comments on commit ded7017

Please sign in to comment.