-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
306 lines (250 loc) · 7.75 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
'use strict';
var path = require('path'),
rc = require('betterc'),
Promise = require('pinkie-promise'),
merge = require('./lib/merge');
/**
* Constructor
* @param {Object} [options] object
* @param {String} [options.name='bem'] - config filename.
* @param {String} [options.cwd=process.cwd()] project root directory.
* @param {Object} [options.defaults={}] use this object as fallback for found configs
* @param {String} [options.pathToConfig] custom path to config on FS via command line argument `--config`
* @constructor
*/
function BemConfig(options) {
this._options = options || {};
// TODO: use cwd for resolver
this._options.cwd || (this._options.cwd = process.cwd());
// TODO: use cache
// this._cache = {};
}
/**
* Returns all found configs
* @param {Boolean} isSync - flag to resolve configs synchronously
* @returns {Promise|Array}
*/
BemConfig.prototype.configs = function(isSync) {
var options = this._options,
cwd = options.cwd,
rcOpts = {
defaults: options.defaults && JSON.parse(JSON.stringify(options.defaults)),
cwd: cwd,
fsRoot: options.fsRoot,
fsHome: options.fsHome,
name: options.name || 'bem',
extendBy: options.extendBy
};
if (options.pathToConfig) {
rcOpts.argv = { config: options.pathToConfig };
}
var plugins = [require('./plugins/resolve-level')].concat(options.plugins || []);
if (isSync) {
var configs = this._configs || (this._configs = rc.sync(rcOpts));
this._root = getConfigsRootDir(configs);
return plugins.reduce(function(configs, plugin) {
return configs.map(function(config) {
return plugin(config, configs, options);
});
}, configs);
}
var _this = this;
return (this._configs ? Promise.resolve(this._configs) : rc(rcOpts)).then(function(cfgs) {
_this._configs || (_this._configs = cfgs);
_this._root = getConfigsRootDir(cfgs);
return plugins.reduce(
function(cfgsPromise, plugin) {
return cfgsPromise.then(function(configs) {
return Promise.all(configs.map(function(config) {
return new Promise(function(resolve) {
plugin(config, configs, options, resolve);
});
}));
});
},
Promise.resolve(cfgs));
});
};
/**
* Returns project root
* @returns {Promise}
*/
BemConfig.prototype.root = function() {
if (this._root) {
return Promise.resolve(this._root);
}
var _this = this;
return this.configs().then(function() {
return _this._root;
});
};
/**
* Returns merged config
* @returns {Promise}
*/
BemConfig.prototype.get = function() {
return this.configs().then(function(configs) {
return merge(configs);
});
};
/**
* Resolves config for given level
* @param {String} pathToLevel - level path
* @returns {Promise}
*/
BemConfig.prototype.level = function(pathToLevel) {
var _this = this;
return this.configs()
.then(function(configs) {
return getLevelByConfigs(
pathToLevel,
_this._options,
configs,
_this._root);
});
};
/**
* Returns config for given library
* @param {String} libName - library name
* @returns {Promise}
*/
BemConfig.prototype.library = function(libName) {
return this.get().then(function(config) {
var libs = config.libs;
if (!libs) { return; }
var lib = libs[libName];
if (!lib) { return; }
return new BemConfig({ projectRoot: lib.path });
});
};
/**
* Returns map of settings for each of level
* @returns {Promise}
*/
BemConfig.prototype.levelMap = function() {
var _this = this;
return this.get().then(function(config) {
var projectLevels = config.levels,
libNames = config.libs ? Object.keys(config.libs) : [];
return Promise.all(libNames.map(function(libName) {
return _this.library(libName).then(function(bemLibConf) {
return bemLibConf.get().then(function(libConfig) {
return libConfig.levels;
});
});
})).then(function(libLevels) {
var allLevels = libLevels.concat(projectLevels);
return allLevels.reduce(merge, {});
});
});
};
/**
* Returns config for given module name
* @param {String} moduleName - name of module
* @returns {Promise}
*/
BemConfig.prototype.module = function(moduleName) {
return this.get().then(function(config) {
var modules = config.modules;
return modules && modules[moduleName];
});
};
/**
* Returns project root
* @returns {String}
*/
BemConfig.prototype.rootSync = function() {
if (this._root) {
return this._root;
}
this.configs(true);
return this._root;
};
/**
* Returns merged config synchronously
* @returns {Object}
*/
BemConfig.prototype.getSync = function() {
return merge(this.configs(true));
}
/**
* Resolves config for given level synchronously
* @param {String} pathToLevel - level path
* @returns {Object}
*/
BemConfig.prototype.levelSync = function(pathToLevel) {
// TODO: cache
return getLevelByConfigs(
pathToLevel,
this._options,
this.configs(true),
this._root);
};
/**
* Returns config for given library synchronously
* @param {String} libName - library name
* @returns {Object}
*/
BemConfig.prototype.librarySync = function(libName) {
var config = this.getSync(),
libs = config.libs;
if (!libs) { return; }
var lib = libs[libName];
if (!lib) { return; }
return new BemConfig({ projectRoot: lib.path });
};
/**
* Returns map of settings for each of level synchronously
* @returns {Object}
*/
BemConfig.prototype.levelMapSync = function() {
var config = this.getSync(),
projectLevels = config.levels,
libNames = config.libs ? Object.keys(config.libs) : [];
var libLevels = libNames.map(function(libName) {
var bemLibConf = this.librarySync(libName),
libConfig = bemLibConf.getSync();
return libConfig.levels;
}, this);
var allLevels = libLevels.concat(projectLevels);
return allLevels.reduce(function(acc, level) {
return merge(acc, level);
}, {});
};
/**
* Returns config for given module name synchronously
* @param {String} moduleName - name of module
* @returns {Object}
*/
BemConfig.prototype.moduleSync = function(moduleName) {
var modules = this.getSync().modules;
return modules && modules[moduleName];
};
function getConfigsRootDir(configs) {
var rootCfg = [].concat(configs).reverse().find(function(cfg) { return cfg.root && cfg.__source; });
if (rootCfg) { return path.dirname(rootCfg.__source); }
}
function getLevelByConfigs(pathToLevel, options, allConfigs, root) {
var absLevelPath = path.resolve(root || options.cwd, pathToLevel),
levelOpts = {},
commonOpts = {};
for (var i = allConfigs.length - 1; i >= 0; i--) {
var conf = allConfigs[i],
levels = conf.levels || {};
commonOpts = merge({}, conf, commonOpts);
for (var level in levels) {
if (level !== absLevelPath) { continue; }
// works like deep extend but overrides arrays
levelOpts = merge({}, levels[level], levelOpts);
}
if (conf.root) { break; }
}
levelOpts = merge(commonOpts, levelOpts);
delete levelOpts.__source;
delete levelOpts.levels;
delete levelOpts.root;
return Object.keys(levelOpts).length ? levelOpts : undefined;
}
module.exports = function(opts) {
return new BemConfig(opts);
};