require-glob.js
3.87 KB
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
'use strict';
var assign = require('object-assign');
var globby = require('globby');
var path = require('path');
var CAMELIZE_PATTERN = /[\.\-]+(.)/g;
var SEPARATOR_PATTERN = /[\\\/]/;
var SEPARATOR = path.sep;
// Utilities
function toCamelCase(value) {
return value.replace(CAMELIZE_PATTERN, function (match, character) {
return character.toUpperCase();
});
}
function toCombinedValues(a, b) {
return a.concat(b);
}
function toCommonFirstValue(a, b) {
return a && b && a.length && b.length && a[0] === b[0] ? a : false;
}
function toJoinedPath(filePath) {
return filePath.join(SEPARATOR);
}
function toNestedObject(obj, key) {
return obj[key] || (obj[key] = {});
}
function toResolvedPath(cwd, filePath) {
return require.resolve(path.resolve(cwd, filePath));
}
function toShiftedPath(filePath) {
return filePath.slice(1);
}
function toSplitPath(filePath) {
return filePath.split(SEPARATOR_PATTERN);
}
// Mapper
function resolvePaths(cwd, paths) {
return paths.map(toResolvedPath.bind(null, cwd));
}
function trimPaths(paths) {
paths = paths.map(toSplitPath);
if (paths.length === 1) {
return paths[0].slice(-1);
}
while (paths.reduce(toCommonFirstValue)) {
paths = paths.map(toShiftedPath);
}
return paths.map(toJoinedPath);
}
function mapper(filePath, i, filePaths) {
var cwd = this.cwd;
var resolvedPaths = this.resolvedPaths ||
(this.resolvedPaths = resolvePaths(cwd, filePaths));
var trimmedPaths = this.trimmedPaths ||
(this.trimmedPaths = trimPaths(resolvedPaths));
var resolvedPath = resolvedPaths[i];
var trimmedPath = trimmedPaths[i];
if (this.bustCache) {
delete require.cache[resolvedPath];
}
return {
cwd: cwd,
path: resolvedPath,
shortPath: trimmedPath,
relativePath: filePath,
exports: require(resolvedPath)
};
}
// Reducer
function keygen(file) {
var parsedPath = path.parse(file.shortPath);
return [parsedPath.dir, parsedPath.name]
.map(toSplitPath)
.reduce(toCombinedValues)
.filter(Boolean)
.map(toCamelCase);
}
function reducer(tree, file) {
var keys = [].concat(this.keygen(file));
var lastKey = keys.pop();
var obj = keys.reduce(toNestedObject, tree);
obj[lastKey] = file.exports;
return tree;
}
// Map Reduce
function mapReduce(options, filePaths) {
return filePaths
.map(options.mapper)
.reduce(options.reducer, {});
}
// API
function normalizeOptions(options) {
var defaults = {
cwd: path.dirname(module.parent.filename),
bustCache: false,
keygen: keygen,
mapper: mapper,
reducer: reducer
};
options = assign(defaults, options);
options.keygen = options.keygen.bind(options);
options.mapper = options.mapper.bind(options);
options.reducer = options.reducer.bind(options);
return options;
}
/**
* Requires multiple modules using glob patterns and returns the results as a
* nested object. Supports exclusions.
*
* @type {Function}
* @param {String|Array.<String>} pattern One or more glob patterns.
* @param {Object=} options
* @param {Boolean=} options.bustCache Whether to force the reload of modules.
* @param {Boolean=} options.cwd The current working directory in which to search.
* @param {Function=} options.mapper Optional custom map function.
* @param {Function=} options.reducer Optional custom reduce function.
* @param {Function=} options.keygen Optional custom key generator function.
* @return {Promise.<Object>}
*/
function requireGlob(pattern, options) {
options = normalizeOptions(options);
return globby(pattern, options)
.then(mapReduce.bind(null, options));
}
/**
* Synchronous version of the above.
*
* @method sync
* @param {String|Array.<String>} patterns Same as async method.
* @param {Object=} options Same as async method.
* @return {Object}
*/
function requireGlobSync(pattern, options) {
options = normalizeOptions(options);
return mapReduce(options, globby.sync(pattern, options));
}
module.exports = requireGlob;
module.exports.sync = requireGlobSync;