Skip to content
This repository was archived by the owner on Dec 20, 2023. It is now read-only.

Commit 1cae07d

Browse files
authored
Merge pull request #40 from C2FO/fix-load-merging
fix: issue where configs were not properly merging multiple locations
2 parents 4d7763b + 64411d3 commit 1cae07d

24 files changed

+764
-1006
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ node_modules
22
*.iml
33
.idea
44
coverage
5+
test/__fixtures__/generated

lib/index.js

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,6 @@ class ConfigLoader extends PatternEventEmitter {
118118
);
119119
}
120120
}
121-
122121
/**
123122
* Factory method to create a new {@link ConfigLoader}.
124123
*
@@ -140,12 +139,14 @@ class ConfigLoader extends PatternEventEmitter {
140139
* @return {ConfigLoader} the Config to use to load configurations.
141140
*/
142141
function gofigure(options = {}) {
143-
const locations = (options.locations || []).map((file) => {
144-
if (_.isPlainObject(file)) {
145-
return file;
146-
}
147-
return { file };
148-
});
142+
const locations = (options.locations || [])
143+
.map((file) => {
144+
if (_.isPlainObject(file)) {
145+
return file;
146+
}
147+
return { file };
148+
})
149+
.reverse();
149150
if (!locations) {
150151
throw new Error('Please provide locations to load configurations from');
151152
}

lib/processor/index.js

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,3 @@
1-
const _ = require('lodash');
2-
const selector = require('./selector');
3-
const merger = require('./merger');
4-
const mixin = require('./mixin');
5-
const replacer = require('./replacer');
1+
const processor = require('./processor');
62

7-
module.exports = (configToMergeInto, sourceConfigs, options) => {
8-
const mergedSource = sourceConfigs.reduce((merged, source) => _.merge(merged, mixin(source, options)), {});
9-
const mergeEnvConfig = selector(mergedSource, options);
10-
const replaced = replacer(mergeEnvConfig, options);
11-
return merger(configToMergeInto, replaced, options);
12-
};
3+
module.exports = processor;

lib/processor/mixin.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,12 @@ const mergeMixin = (originalConfig, env, defaultEnv, nodeType) => {
2323
return config;
2424
};
2525

26-
const mixinMerger = (config, opts) => {
26+
const mixin = (config, opts) => {
2727
const options = opts || {};
2828
const defaultEnvironment = options.defaultEnvironment || null;
2929
const environment = options.environment || null;
3030
const nodeType = options.nodeType || null;
3131
return mergeMixin(config, environment, defaultEnvironment, nodeType);
3232
};
3333

34-
module.exports = mixinMerger;
34+
module.exports = mixin;

lib/processor/processor.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
const _ = require('lodash');
2+
const replacer = require('./replacer');
3+
const merger = require('./merger');
4+
const selector = require('./selector');
5+
const mixin = require('./mixin');
6+
7+
const processConfig = (config, options) => {
8+
const merged = mixin(config, options);
9+
const mergeEnvConfig = selector(merged, options);
10+
return replacer(mergeEnvConfig, options);
11+
};
12+
13+
const processor = (configToMergeInto, sourceConfigs, options) => {
14+
const mergedSource = sourceConfigs.reduce((merged, source) => _.merge(merged, processConfig(source, options)), {});
15+
return merger(configToMergeInto, mergedSource, options);
16+
};
17+
18+
module.exports = processor;

lib/processor/selector.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,12 @@ const selectConfig = (originalConfig, env, defaultEnv, nodeType) => {
2626
return config;
2727
};
2828

29-
const configSelector = (config, opts) => {
29+
const selector = (config, opts) => {
3030
const options = opts || {};
3131
const defaultEnvironment = options.defaultEnvironment || null;
3232
const environment = options.environment || null;
3333
const nodeType = options.nodeType || null;
3434
return selectConfig(config, environment, defaultEnvironment, nodeType);
3535
};
3636

37-
module.exports = configSelector;
37+
module.exports = selector;

test/__fixtures__/config-mixin/config-mixin.default.json

Lines changed: 0 additions & 7 deletions
This file was deleted.

test/__fixtures__/config-mixin/config-mixin.development-one.json

Lines changed: 0 additions & 8 deletions
This file was deleted.

test/__fixtures__/config-mixin/config-mixin.development-two.json

Lines changed: 0 additions & 8 deletions
This file was deleted.

test/__fixtures__/config-mixin/config-mixin.production-one.json

Lines changed: 0 additions & 8 deletions
This file was deleted.

0 commit comments

Comments
 (0)