1 | const requireAll = require('require-all');
|
2 | const path = require('path');
|
3 | const _forEach = require('lodash.foreach');
|
4 | const _isFunction = require('lodash.isfunction');
|
5 | const _isObject = require('lodash.isobject');
|
6 |
|
7 | const rfileFilter = /(.+)\.(js|json)$/;
|
8 |
|
9 | const hasOwnProp = function (source, propertyName) {
|
10 | return Object.prototype.hasOwnProperty.call(source, propertyName);
|
11 | };
|
12 |
|
13 | module.exports = {
|
14 | registerFolder(rootDir, dir) {
|
15 | const dirname = path.resolve(rootDir, dir);
|
16 | const libs = requireAll({
|
17 | dirname,
|
18 | filter: rfileFilter
|
19 | });
|
20 | this.registerLibMap(libs);
|
21 | return this;
|
22 | },
|
23 |
|
24 | registerLibMap(libs) {
|
25 | _forEach(libs, (value, name) => {
|
26 | if (hasOwnProp(libs, name)) {
|
27 | const lib = libs[name];
|
28 | if (_isFunction(lib)) {
|
29 | this.addResolvableDependency(name, lib);
|
30 | } else if (_isObject(lib)) {
|
31 | this.registerLibMap(lib);
|
32 | }
|
33 | }
|
34 | });
|
35 | return this;
|
36 | },
|
37 |
|
38 | registerFolders(rootDir, dirs) {
|
39 | _forEach(dirs, (dir) => this.registerFolder(rootDir, dir));
|
40 | return this;
|
41 | },
|
42 |
|
43 | registerDependencies(dependencies) {
|
44 | _forEach(dependencies, (value, name) => {
|
45 | if (hasOwnProp(dependencies, name)) {
|
46 | const lib = dependencies[name];
|
47 | this.addDependency(name, lib);
|
48 | }
|
49 | });
|
50 | return this;
|
51 | }
|
52 | };
|