-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathhelpers.js
32 lines (26 loc) · 884 Bytes
/
helpers.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
'use strict';
var path = require('path');
/**
* Helper function to resolve assets on the pagelet.
*
* @param {Function} constructor The Pagelet constructor
* @param {String|Array} keys Name(s) of the property, e.g. [css, js].
* @param {String} dir Optional absolute directory to resolve from.
* @returns {Pagelet}
* @api private
*/
exports.resolve = function resolve(constructor, keys, dir) {
var prototype = constructor.prototype;
keys = Array.isArray(keys) ? keys : [keys];
keys.forEach(function each(key) {
if (!prototype[key]) return;
var stack = Array.isArray(prototype[key])
? prototype[key]
: [prototype[key]];
prototype[key] = stack.filter(Boolean).map(function map(file) {
if (/^(http:|https:)?\/\//.test(file)) return file;
return path.resolve(dir || prototype.directory, file);
});
});
return constructor;
};