-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
265 lines (231 loc) · 6.3 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
'use strict';
var path = require('path');
var glob = require('matched');
var set = require('set-value');
var typeOf = require('kind-of');
var extend = require('extend-shallow');
var Emitter = require('component-emitter');
var isValidGlob = require('is-valid-glob');
var isGlob = require('is-glob');
var utils = require('./utils');
/**
* Create an instance of `Loader` with the given `options`.
*
* ```js
* var Loader = require('load-helpers');
* var loader = new Loader();
* ```
* @param {Object} `options`
* @api public
*/
function Loader(options) {
if (!(this instanceof Loader)) {
return new Loader(options);
}
this.options = options || {};
this.cache = this.options.helpers || {};
}
/**
* Inherit `Emitter`
*/
Loader.prototype = Object.create(Emitter.prototype);
Loader.prototype.constructor = Loader;
/**
* Register a helper function by `name`.
*
* ```js
* loader.addHelper('foo', function() {
* // do stuff
* });
* ```
* @param {String} `name`
* @param {Function} `fn`
* @param {Object} `options`
* @api public
*/
Loader.prototype.addHelper = function(name, fn, options) {
if (typeof name !== 'string') {
throw new TypeError('expected helper name to be a string');
}
if (typeof fn !== 'function') {
throw new TypeError('expected helper to be a function');
}
if (typeof options === 'boolean') {
options = { async: options };
}
if (options && options.async === true) {
fn.async = true;
}
set(this.cache, name, fn);
this.emit('helper', name, fn);
return this;
};
/**
* Register an object of helper functions.
*
* ```js
* loader.addHelpers({
* foo: function() {},
* bar: function() {},
* baz: function() {}
* });
* ```
* @param {Object} `helpers`
* @param {Object} `options`
* @api public
*/
Loader.prototype.addHelpers = function(helpers, options) {
var keys = Object.keys(helpers);
for (var i = 0; i < keys.length; i++) {
var key = keys[i];
this.addHelper(key, helpers[key], options);
}
return this;
};
/**
* Load one or more helpers from a filepath, glob pattern, object, or
* an array of any of those things. This method detects the type of
* value to be handled then calls one of the other methods to do the
* actual loading.
*
* ```js
* var loader = new Loader();
* console.log(loader.load(['foo/*.hbs', 'bar/*.hbs']));
* console.log(loader.load({path: 'a/b/c.md'}));
* console.log(loader.load('index', {path: 'a/b/c.md'}));
* ```
* @param {Object} `helpers`
* @param {Object} `options`
* @return {Object} Returns the views from `loader.helpers`
* @api public
*/
Loader.prototype.load = function(val, options) {
if (typeof options === 'boolean') {
options = { isAsync: options };
}
switch (typeOf(val)) {
case 'string':
case 'function':
return this.loadHelper.apply(this, arguments);
case 'object':
return this.loadHelpers.apply(this, arguments);
case 'array':
if (isValidGlob(val)) {
var cache = this.loadGlob.apply(this, arguments);
return this.loadHelpers(cache, options);
}
return this.loadHelpers.apply(this, arguments);
default: {
throw new TypeError('expected name to be an object, array or string');
}
}
};
Loader.prototype.loadHelper = function(name, fn, options) {
if (utils.isObject(fn)) {
options = fn;
fn = undefined;
}
if (isGlob(name)) {
var cache = this.loadGlob.apply(this, arguments);
return this.loadHelpers(cache, options);
}
var opts = utils.options(this, options);
if (typeof fn === 'string') {
fn = utils.tryRequire(fn, opts);
}
if (typeof fn !== 'function') {
var val = this.loadFile(name, options);
if (utils.isObject(val)) {
return this.addHelpers(val, options);
}
}
this.addHelper(name, fn, options);
return this;
};
Loader.prototype.loadHelpers = function(helpers, options) {
if (Array.isArray(helpers) && helpers.length) {
for (var i = 0; i < helpers.length; i++) {
this.load(helpers[i], options);
}
} else if (utils.isObject(helpers)) {
var keys = Object.keys(helpers);
var len = keys.length;
var idx = -1;
while (++idx < len) {
var name = keys[idx];
var helper = helpers[name];
var type = typeOf(helper);
switch (type) {
case 'function':
this.loadHelper(name, helper, options);
break;
case 'object':
this.loadGroup(name, helper, options);
break;
case 'string':
this.loadHelper(name, utils.tryRequire(helper), options);
break;
case 'array':
if (isValidGlob(helper)) {
var cache = this.loadGlob(helper, options);
this.loadGroup(name, cache, options);
} else {
for (var j = 0; j < helper.length; j++) {
var val = helper[j];
if (isValidGlob(val)) {
val = this.loadGlob(val, options);
}
this.loadGroup(name, val, options);
}
}
break;
default: {
throw new TypeError('unsupported helper type: ' + type);
}
}
}
}
return this;
};
Loader.prototype.loadGroup = function(name, helpers, options) {
if (typeof helpers === 'function') {
this.addHelper(name, helpers);
}
var keys = Object.keys(helpers);
for (var j = 0; j < keys.length; j++) {
var key = keys[j];
this.addHelper(name + '.' + key, helpers[key], options);
}
return this;
};
Loader.prototype.loadFile = function(name, options) {
if (typeof name !== 'string') {
throw new TypeError('expected filepath or module name to be a string');
}
var opts = utils.options(this, options);
var val = utils.tryRequire(name, opts);
if (typeof val === 'function') {
var cache = {};
name = utils.renameHelper(name, opts);
cache[name] = val;
return cache;
}
return val;
};
Loader.prototype.loadGlob = function(patterns, options) {
var opts = utils.options(this, options);
var cache = {};
if (typeof patterns === 'string' && !isGlob(patterns)) {
return this.loadFile(patterns, options);
}
var files = glob.sync(patterns, opts);
for (var i = 0; i < files.length; i++) {
var name = path.resolve(opts.cwd, files[i]);
cache = extend({}, cache, this.loadFile(name, opts));
}
return cache;
};
/**
* Expose `Loader`
*/
module.exports = Loader;