Skip to content

Commit 7bcaa46

Browse files
committed
wip
1 parent e3070d3 commit 7bcaa46

File tree

4 files changed

+34
-28
lines changed

4 files changed

+34
-28
lines changed

packages/config/index.js renamed to packages/config/config.js

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,11 @@ var fs = require('fs'),
99
merge = require('./lib/merge'),
1010
resolveSets = require('./lib/resolve-sets'),
1111

12-
basePlugins = [require('./plugins/resolve-level')];
12+
basePlugins = [require('./plugins/resolve-level')],
13+
14+
accessAsync = (cwd, mode) =>
15+
new Promise((resolve, reject) =>
16+
fs.access(cwd, mode, (err) => !err ? resolve() : reject(err)));
1317

1418
/**
1519
* Constructor
@@ -141,20 +145,16 @@ BemConfig.prototype.library = function(libName) {
141145
lib = libs && libs[libName];
142146

143147
if (lib !== undefined && typeof lib !== 'object') {
144-
return Promise.reject('Invalid `libs` format');
148+
throw new Error('Invalid `libs` format');
145149
}
146150

147-
var cwd = lib && lib.path || path.resolve('node_modules', libName);
148-
149-
return new Promise(function(resolve, reject) {
150-
fs.exists(cwd, function(doesExist) {
151-
if (!doesExist) {
152-
return reject('Library ' + libName + ' was not found at ' + cwd);
153-
}
151+
const cwd = lib && lib.path || path.resolve('node_modules', libName);
154152

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

182182
return allLevels.reduce((res, lvl) => {
183-
res[lvl.path] = merge(res[lvl.path] || {}, lvl);
183+
res[lvl.path] = merge([res[lvl.path] || {}, lvl]);
184184
return res;
185185
}, {});
186186
});
@@ -395,21 +395,21 @@ function getLevelByConfigs(pathToLevel, options, allConfigs, root) {
395395
var conf = allConfigs[i],
396396
levels = conf.levels || [];
397397

398-
commonOpts = merge({}, conf, commonOpts);
398+
commonOpts = merge([{}, conf, commonOpts]);
399399

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

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

405405
// works like deep extend but overrides arrays
406-
levelOpts = merge({}, level, levelOpts);
406+
levelOpts = merge([{}, level, levelOpts]);
407407
}
408408

409409
if (conf.root) { break; }
410410
}
411411

412-
levelOpts = merge(commonOpts, levelOpts);
412+
levelOpts = merge([commonOpts, levelOpts]);
413413

414414
delete levelOpts.__source;
415415
delete levelOpts.path;

packages/config/lib/merge.js

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,20 @@ var mergeWith = require('lodash.mergewith');
44

55
/**
66
* Merge all arguments to firt one.
7+
*
78
* Consider arrays as simple value and not deep merge them.
8-
* @param {Array|Object} configs - array of configs or positional arguments
9-
* @return {Object}
9+
*
10+
* @example
11+
* result: {levels: Array<{path: string, layer: string}>, sets: Object<string,string|Array>}
12+
*
13+
* @param {Array<Object>} configs - array of configs
14+
* @returns {Object}
1015
*/
1116
module.exports = function merge(configs) {
12-
var args = Array.isArray(configs) ? configs : Array.from(arguments);
13-
args.push(function(objValue, srcValue) {
14-
if (Array.isArray(objValue)) { return srcValue; }
15-
});
16-
return mergeWith.apply(null, args);
17+
return mergeWith.apply(null, [].concat(
18+
Array.isArray(configs) ? configs : Array.from(arguments),
19+
function(objValue, srcValue) {
20+
if (Array.isArray(objValue)) { return srcValue; }
21+
}
22+
));
1723
};

packages/config/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"publishConfig": {
66
"access": "public"
77
},
8-
"main": "index.js",
8+
"main": "config.js",
99
"scripts": {
1010
"specs": "mocha",
1111
"cover": "nyc mocha",

packages/config/test/async.test.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -290,13 +290,13 @@ describe('async', () => {
290290
}
291291
}]);
292292

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

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

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

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

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

0 commit comments

Comments
 (0)