-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
89 lines (72 loc) · 2.45 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
/*!
* load-middlewares
* Copyright(c) 2015 Fangdun Cai
* MIT Licensed
*/
'use strict';
var multimatch = require('multimatch');
var findup = require('findup-sync');
var path = require('path');
function arrayify(el) {
return Array.isArray(el) ? el : [el];
}
function camelize(str) {
return str.replace(/-(\w)/g, function(m, p1) {
return p1.toUpperCase();
});
}
module.exports = function(options) {
var finalObject = {};
var configObject;
var requireFn;
options = options || {};
var pattern = arrayify(options.pattern || ['koa-*', 'koa.*']);
var config = options.config || findup('package.json', {cwd: parentDir});
var scope = arrayify(options.scope || ['dependencies', 'devDependencies', 'peerDependencies']);
var replaceString = options.replaceString || /^koa(-|\.)/;
var camelizePluginName = options.camelize === false ? false : true;
var lazy = 'lazy' in options ? !!options.lazy : true;
var renameObj = options.rename || {};
if(typeof options.requireFn === 'function') {
requireFn = options.requireFn;
} else if(typeof config === 'string') {
requireFn = function (name) {
// This searches up from the specified package.json file, making sure
// the config option behaves as expected.
var searchFor = path.join('node_modules', name);
return require(findup(searchFor, {cwd: path.dirname(config)}));
};
} else {
requireFn = require;
}
configObject = (typeof config === 'string') ? require(config) : config;
if(!configObject) {
throw new Error('Could not find dependencies. Do you have a package.json file in your project?');
}
var names = scope.reduce(function(result, prop) {
return result.concat(Object.keys(configObject[prop] || {}));
}, []);
pattern.push('!koa-load-middlewares');
multimatch(names, pattern).forEach(function(name) {
var requireName;
if(renameObj[name]) {
requireName = options.rename[name];
} else {
requireName = name.replace(replaceString, '');
requireName = camelizePluginName ? camelize(requireName) : requireName;
}
if(lazy) {
Object.defineProperty(finalObject, requireName, {
get: function() {
return requireFn(name);
}
});
} else {
finalObject[requireName] = requireFn(name);
}
});
return finalObject;
};
var parentDir = path.dirname(module.parent.filename);
// Necessary to get the current `module.parent` and resolve paths correctly.
delete require.cache[__filename];