Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

config: improves #340

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 16 additions & 16 deletions packages/config/index.js → packages/config/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@ var fs = require('fs'),
merge = require('./lib/merge'),
resolveSets = require('./lib/resolve-sets'),

basePlugins = [require('./plugins/resolve-level')];
basePlugins = [require('./plugins/resolve-level')],

accessAsync = (cwd, mode) =>
new Promise((resolve, reject) =>
fs.access(cwd, mode, (err) => !err ? resolve() : reject(err)));

/**
* Constructor
Expand Down Expand Up @@ -141,20 +145,16 @@ BemConfig.prototype.library = function(libName) {
lib = libs && libs[libName];

if (lib !== undefined && typeof lib !== 'object') {
return Promise.reject('Invalid `libs` format');
throw new Error('Invalid `libs` format');
}

var cwd = lib && lib.path || path.resolve('node_modules', libName);

return new Promise(function(resolve, reject) {
fs.exists(cwd, function(doesExist) {
if (!doesExist) {
return reject('Library ' + libName + ' was not found at ' + cwd);
}
const cwd = lib && lib.path || path.resolve('node_modules', libName);

resolve(cwd);
})
});
return accessAsync(cwd)
.then(() => cwd)
.catch(err => {
throw new Error('Library ' + libName + ' was not found at ' + cwd + ('\n' + err));
});
})
.then(cwd => new BemConfig({ cwd: path.resolve(cwd) }));
};
Expand All @@ -180,7 +180,7 @@ BemConfig.prototype.levelMap = function() {
var allLevels = [].concat.apply([], libLevels.filter(Boolean)).concat(projectLevels);

return allLevels.reduce((res, lvl) => {
res[lvl.path] = merge(res[lvl.path] || {}, lvl);
res[lvl.path] = merge([res[lvl.path] || {}, lvl]);
return res;
}, {});
});
Expand Down Expand Up @@ -395,21 +395,21 @@ function getLevelByConfigs(pathToLevel, options, allConfigs, root) {
var conf = allConfigs[i],
levels = conf.levels || [];

commonOpts = merge({}, conf, commonOpts);
commonOpts = merge([{}, conf, commonOpts]);

for (var j = 0; j < levels.length; j++) {
var level = levels[j];

if (level === undefined || level.path !== absLevelPath) { continue; }

// works like deep extend but overrides arrays
levelOpts = merge({}, level, levelOpts);
levelOpts = merge([{}, level, levelOpts]);
}

if (conf.root) { break; }
}

levelOpts = merge(commonOpts, levelOpts);
levelOpts = merge([commonOpts, levelOpts]);

delete levelOpts.__source;
delete levelOpts.path;
Expand Down
20 changes: 13 additions & 7 deletions packages/config/lib/merge.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,20 @@ var mergeWith = require('lodash.mergewith');

/**
* Merge all arguments to firt one.
*
* Consider arrays as simple value and not deep merge them.
* @param {Array|Object} configs - array of configs or positional arguments
* @return {Object}
*
* @example
* result: {levels: Array<{path: string, layer: string}>, sets: Object<string,string|Array>}
*
* @param {Array<Object>} configs - array of configs
* @returns {Object}
*/
module.exports = function merge(configs) {
var args = Array.isArray(configs) ? configs : Array.from(arguments);
args.push(function(objValue, srcValue) {
if (Array.isArray(objValue)) { return srcValue; }
});
return mergeWith.apply(null, args);
return mergeWith.apply(null, [].concat(
Array.isArray(configs) ? configs : Array.from(arguments),
function(objValue, srcValue) {
if (Array.isArray(objValue)) { return srcValue; }
}
));
};
2 changes: 1 addition & 1 deletion packages/config/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"publishConfig": {
"access": "public"
},
"main": "index.js",
"main": "config.js",
"scripts": {
"specs": "mocha",
"cover": "nyc mocha",
Expand Down
8 changes: 4 additions & 4 deletions packages/config/test/async.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -290,13 +290,13 @@ describe('async', () => {
}
}]);

return bemConfig().library('lib1').catch(err => expect(err).to.equal('Invalid `libs` format'));
return bemConfig().library('lib1').catch(err => expect(err).to.match(/Invalid `libs` format/));
});

it('should throw if lib was not found', () => {
const bemConfig = config();

return bemConfig().library('lib1').catch(err => expect(err.includes('Library lib1 was not found at')).to.equal(true));
return bemConfig().library('lib1').catch(err => expect(err).to.match(/Library lib1 was not found at /));
});

it('should throw if lib was not found', () => {
Expand All @@ -310,8 +310,8 @@ describe('async', () => {
}]);

return Promise.all([
bemConfig().library('lib1').catch(err => expect(err.includes('Library lib1 was not found at')).to.equal(true)),
bemConfig().library('lib2').catch(err => expect(err.includes('Library lib2 was not found at')).to.equal(true))
bemConfig().library('lib1').catch(err => expect(err).to.match(/Library lib1 was not found at/)),
bemConfig().library('lib2').catch(err => expect(err).to.match(/Library lib2 was not found at/))
]);
});

Expand Down