-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
139 lines (112 loc) · 3.61 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
var Module = require('module'),
assert = require('assert').ok,
fs = require('fs'),
path = require('path');
var moduleCache = {},
options;
function hasOwnProperty(obj, prop) {
return Object.prototype.hasOwnProperty.call(obj, prop);
}
function statPath(path) {
try {
return fs.statSync(path);
} catch (ex) {}
return false;
}
// check if the directory is a package.json dir
var packageMainCache = {};
function readPackage(requestPath) {
if (hasOwnProperty(packageMainCache, requestPath)) {
return packageMainCache[requestPath];
}
try {
var jsonPath = path.resolve(requestPath, 'package.json');
var json = fs.readFileSync(jsonPath, 'utf8');
} catch (e) {
return false;
}
try {
var pkg = packageMainCache[requestPath] = JSON.parse(json);
} catch (e) {
e.path = jsonPath;
e.message = 'Error parsing ' + jsonPath + ': ' + e.message;
throw e;
}
return pkg;
}
function tryPackage(requestPath, exts) {
var pkg = readPackage(requestPath);
if (!(pkg && pkg.main)) return false;
var resolved = pkg['_resolved'],
main = pkg.main;
var filename;
if(resolved) {
filename = moduleCache[resolved] || (moduleCache[resolved] = path.resolve(requestPath, main));
}else{
filename = path.resolve(requestPath, main);
}
return tryFile(filename) || tryExtensions(filename, exts) ||
tryExtensions(path.resolve(filename, 'index'), exts);
}
// check if the file exists and is not a directory
function tryFile(requestPath) {
var stats = statPath(requestPath);
if (stats && !stats.isDirectory()) {
return fs.realpathSync(requestPath, Module._realpathCache);
}
return false;
}
// given a path check a the file exists with any of the set extensions
function tryExtensions(p, exts) {
for (var i = 0, EL = exts.length; i < EL; i++) {
var filename = tryFile(p + exts[i]);
if (filename) {
return filename;
}
}
return false;
}
module.exports = {
init: function(_options){
if(options) {
return;
}
options = _options||{};
Module._findPath = function(request, paths) {
var exts = Object.keys(Module._extensions);
if (request.charAt(0) === '/') {
paths = [''];
}
var trailingSlash = (request.slice(-1) === '/');
var cacheKey = JSON.stringify({request: request, paths: paths});
if (Module._pathCache[cacheKey]) {
return Module._pathCache[cacheKey];
}
// For each path
for (var i = 0, PL = paths.length; i < PL; i++) {
var basePath = path.resolve(paths[i], request);
var filename;
if (!trailingSlash) {
// try to join the request to the path
filename = tryFile(basePath);
if (!filename && !trailingSlash) {
// try it with each of the extensions
filename = tryExtensions(basePath, exts);
}
}
if (!filename) {
filename = tryPackage(basePath, exts);
}
if (!filename) {
filename = tryExtensions(path.resolve(basePath, 'index'), exts);
}
if (filename) {
Module._pathCache[cacheKey] = filename;
return filename;
}
}
return false;
};
},
moduleCache: moduleCache
}