From 7bcaa4681e8665ffb6ad81bcd0c8186522a59e2a Mon Sep 17 00:00:00 2001 From: Alexey Yaroshevich Date: Tue, 30 Jan 2018 10:54:36 +0300 Subject: [PATCH] wip --- packages/config/{index.js => config.js} | 32 ++++++++++++------------- packages/config/lib/merge.js | 20 ++++++++++------ packages/config/package.json | 2 +- packages/config/test/async.test.js | 8 +++---- 4 files changed, 34 insertions(+), 28 deletions(-) rename packages/config/{index.js => config.js} (94%) diff --git a/packages/config/index.js b/packages/config/config.js similarity index 94% rename from packages/config/index.js rename to packages/config/config.js index 981414d8..9da8fbb4 100644 --- a/packages/config/index.js +++ b/packages/config/config.js @@ -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 @@ -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) })); }; @@ -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; }, {}); }); @@ -395,7 +395,7 @@ 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]; @@ -403,13 +403,13 @@ function getLevelByConfigs(pathToLevel, options, allConfigs, root) { 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; diff --git a/packages/config/lib/merge.js b/packages/config/lib/merge.js index 6fed789f..bfe9ac74 100644 --- a/packages/config/lib/merge.js +++ b/packages/config/lib/merge.js @@ -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} + * + * @param {Array} 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; } + } + )); }; diff --git a/packages/config/package.json b/packages/config/package.json index d22d98ae..bccaf00f 100644 --- a/packages/config/package.json +++ b/packages/config/package.json @@ -5,7 +5,7 @@ "publishConfig": { "access": "public" }, - "main": "index.js", + "main": "config.js", "scripts": { "specs": "mocha", "cover": "nyc mocha", diff --git a/packages/config/test/async.test.js b/packages/config/test/async.test.js index 95a6016f..baf790a5 100644 --- a/packages/config/test/async.test.js +++ b/packages/config/test/async.test.js @@ -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', () => { @@ -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/)) ]); });