-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
156 lines (121 loc) · 3.21 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
'use strict';
module.exports = (options) => {
let util = require('util');
let path = require('path');
let fs = require('fs');
let useOptions = {paths: new Set(), mapping: {}, guess: (name) => ''};
let target = {};
let handler = {};
let setMapping = (items) => {
if (false === util.isObject(items)) {
return;
}
let allowed = {'string': true, 'object': false, 'function': false};
Object.keys(items).forEach((index) => {
let item = items[index];
let type = typeof item;
if ((null === item) || (false === allowed.hasOwnProperty(type))) {
return;
}
if (allowed[type]) {
useOptions.mapping[index] = item;
} else {
target[index] = item;
}
});
};
let setPaths = (items) => {
if (false === util.isArray(items)) {
return;
}
items.forEach((item) => {
if (path.isAbsolute(item) && fs.existsSync(item) && fs.statSync(item).isDirectory()) {
useOptions.paths.add(item);
}
});
};
let setOptions = (items) => {
if (false === util.isObject(items)) {
return;
}
if (items.hasOwnProperty('paths')) {
setPaths(items.paths);
}
if (items.hasOwnProperty('guess') && ('function' === typeof items.guess)) {
useOptions.guess = items.guess;
}
if (items.hasOwnProperty('mapping')) {
setMapping(items.mapping)
}
};
let packageName = (property) => property.trim()
.replace(/([a-z\d])([A-Z]+)/g, '$1-$2')
.replace(/[-\s]+/g, '-')
.toLowerCase();
let getNames = (property) => {
let mapping = useOptions.mapping;
let items = new Set();
if (mapping.hasOwnProperty(property)) {
items.add(mapping[property]);
}
items.add(property).add(packageName(property));
try {
let guess = useOptions.guess(property);
if (('string' === typeof guess) && ('' !== guess)) {
items.add(guess);
}
return items;
} catch (error) {
return items;
}
};
let buildNames = (property) => {
let names = getNames(property);
let items = new Set();
if (0 === useOptions.paths.length) {
return names;
}
names.forEach((name) => {
if (path.isAbsolute(name)) {
items.add(name);
return;
}
useOptions.paths.forEach((from) => {
items.add(path.resolve(from, name));
});
items.add(name);
});
return items;
};
let getRequire = (property) => {
let value = null;
let names = buildNames(property);
[...names].every((name) => {
try {
value = require(name);
return false;
} catch (error) {
if ('MODULE_NOT_FOUND' === error.code) {
return true;
}
throw error;
}
});
return value;
};
handler.get = (target, property) => {
if (target.hasOwnProperty(property)) {
return target[property];
}
target[property] = getRequire(property);
if (null !== target[property]) {
return target[property];
}
delete target[property];
let error = new Error("Cannot find module '" + property + "'");
error.code = 'MODULE_NOT_FOUND';
throw error;
};
setOptions(options);
return new Proxy(target, handler);
};