Skip to content

Commit

Permalink
feat(config): deeply merge configs
Browse files Browse the repository at this point in the history
use deep-assign to merge default, user and environment configs

BREAKING CHANGE: configs are now being deeply merged (instead of shallowly)
  • Loading branch information
dmbch committed Jan 25, 2018
1 parent 3b604df commit b9519cd
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions packages/config/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

var path = require('path');

var assign = require('deep-assign');
var root = require('pkg-dir').sync();

var explorer = require('cosmiconfig')('hops', {
Expand Down Expand Up @@ -30,18 +31,18 @@ function getDefaultConfig() {

function applyUserConfig(config) {
var result = explorer.load(process.cwd());
return Object.assign(config, result ? result.config : {});
return assign(config, result ? result.config : {});
}

function applyExtensionConfig(config) {
var result = Object.assign({}, config);
if (config.extends) {
try {
require.resolve(config.extends);
Object.assign(result, require(config.extends));
assign(result, require(config.extends));
} catch (_) {
try {
Object.assign(result, require(path.join(root, config.extends)));
assign(result, require(path.join(root, config.extends)));
} catch (_) {
console.error('Failed to extend config using', config.extends);
}
Expand All @@ -52,7 +53,7 @@ function applyExtensionConfig(config) {

function applyEnvironmentConfig(config) {
var env = process.env.HOPS_ENV || process.env.NODE_ENV;
var result = Object.assign({}, config, config.env && config.env[env]);
var result = assign({}, config, config.env && config.env[env]);
delete result.env;
return result;
}
Expand Down

0 comments on commit b9519cd

Please sign in to comment.