Skip to content

Commit

Permalink
Merge pull request #107 from adobe/config-merging
Browse files Browse the repository at this point in the history
allow merging of configs
  • Loading branch information
trieloff authored May 7, 2019
2 parents 48528e4 + 31d2a42 commit 01b2ac9
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
30 changes: 30 additions & 0 deletions src/HelixConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ const YAML = require('yaml');
const Strain = require('./Strain.js');
const Strains = require('./Strains.js');
const ConfigValidator = require('./ConfigValidator.js');
const { concat, uniq, each } = require('./sequence.js');
const { pipe } = require('./functional.js');


const HELIX_CONFIG = 'helix-config.yaml';

Expand Down Expand Up @@ -64,6 +67,33 @@ class HelixConfig {
return this;
}

/**
* @name ResolveFn
* @function
* @param {Strain} left the current candidate strain (can be undefined)
* @param {Strain} right the alternative candidate strain (can be undefined)
*/

/**
* Updates the current configuration with the strains of another configuration
* object and a user-defined resolver function.
* @param {HelixConfig} other another Helix Config to merge
* @param {ResolveFn} resolvefn a resolver function that returns either a strain or undefined
* @returns {HelixConfig} the merged Helix Config
*/
merge(other, resolvefn) {
const filtered = new Strains();
pipe(concat(other.strains.keys(), this.strains.keys()), uniq, each((name) => {
const strain = resolvefn(this.strains.get(name), other.strains.get(name));
if (strain) {
filtered.add(strain);
}
}));

this._strains = filtered;
return this;
}

get directory() {
return this._cwd;
}
Expand Down
23 changes: 23 additions & 0 deletions test/config.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,29 @@ describe('Helix Config Loading', () => {
});
});

describe('Helix Config Merging', () => {
it('can merge two configs', async () => {
const oldconf = await new HelixConfig()
.withConfigPath(path.resolve(SPEC_ROOT, 'minimal.yaml'))
.init();

assert.deepEqual(Array.from(oldconf.strains.keys()), ['default']);

const newconf = await new HelixConfig()
.withConfigPath(path.resolve(SPEC_ROOT, 'minimal-foo.yaml'))
.init();

oldconf.merge(newconf, (left, right) => {
if (left && left.name === 'default') {
return left;
}
return right;
});

assert.deepEqual(Array.from(oldconf.strains.keys()), ['default', 'foo']);
});
});

describe('Helix Config Serializing', () => {
it('can serialize strains as json', async () => {
const cfg = await new HelixConfig()
Expand Down

0 comments on commit 01b2ac9

Please sign in to comment.