Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
80665 views
1
var core = require('./core');
2
var fs = require('fs');
3
var path = require('path');
4
var caller = require('./caller.js');
5
var nodeModulesPaths = require('./node-modules-paths.js');
6
7
module.exports = function resolve (x, opts, cb) {
8
if (typeof opts === 'function') {
9
cb = opts;
10
opts = {};
11
}
12
if (!opts) opts = {};
13
14
var isFile = opts.isFile || function (file, cb) {
15
fs.stat(file, function (err, stat) {
16
if (err && err.code === 'ENOENT') cb(null, false)
17
else if (err) cb(err)
18
else cb(null, stat.isFile() || stat.isFIFO())
19
});
20
};
21
var readFile = opts.readFile || fs.readFile;
22
23
var extensions = opts.extensions || [ '.js' ];
24
var y = opts.basedir || path.dirname(caller());
25
var modules = opts.moduleDirectory || 'node_modules';
26
27
opts.paths = opts.paths || [];
28
29
if (x.match(/^(?:\.\.?\/|\/|([A-Za-z]:)?\\)/)) {
30
loadAsFile(path.resolve(y, x), function (err, m, pkg) {
31
if (err) cb(err)
32
else if (m) cb(null, m, pkg)
33
else loadAsDirectory(path.resolve(y, x), function (err, d, pkg) {
34
if (err) cb(err)
35
else if (d) cb(null, d, pkg)
36
else cb(new Error("Cannot find module '" + x + "' from '" + y + "'"))
37
})
38
});
39
}
40
else loadNodeModules(x, y, function (err, n, pkg) {
41
if (err) cb(err)
42
else if (n) cb(null, n, pkg)
43
else if (core[x]) return cb(null, x);
44
else cb(new Error("Cannot find module '" + x + "' from '" + y + "'"))
45
});
46
47
function loadAsFile (x, pkg, cb) {
48
if (typeof pkg === 'function') {
49
cb = pkg;
50
pkg = opts.package;
51
}
52
53
(function load (exts) {
54
if (exts.length === 0) return cb(null, undefined, pkg);
55
var file = x + exts[0];
56
57
isFile(file, function (err, ex) {
58
if (err) cb(err)
59
else if (ex) cb(null, file, pkg)
60
else load(exts.slice(1))
61
});
62
})([''].concat(extensions));
63
}
64
65
function loadAsDirectory (x, fpkg, cb) {
66
if (typeof fpkg === 'function') {
67
cb = fpkg;
68
fpkg = opts.package;
69
}
70
71
var pkgfile = path.join(x, '/package.json');
72
isFile(pkgfile, function (err, ex) {
73
if (err) return cb(err);
74
if (!ex) return loadAsFile(path.join(x, '/index'), fpkg, cb);
75
76
readFile(pkgfile, function (err, body) {
77
if (err) return cb(err);
78
try {
79
var pkg = JSON.parse(body);
80
}
81
catch (err) {}
82
83
if (opts.packageFilter) {
84
pkg = opts.packageFilter(pkg, x);
85
}
86
87
if (pkg.main) {
88
if (pkg.main === '.' || pkg.main === './'){
89
pkg.main = 'index'
90
}
91
loadAsFile(path.resolve(x, pkg.main), pkg, function (err, m, pkg) {
92
if (err) return cb(err);
93
if (m) return cb(null, m, pkg);
94
if (!pkg) return loadAsFile(path.join(x, '/index'), pkg, cb);
95
96
var dir = path.resolve(x, pkg.main);
97
loadAsDirectory(dir, pkg, function (err, n, pkg) {
98
if (err) return cb(err);
99
if (n) return cb(null, n, pkg);
100
loadAsFile(path.join(x, '/index'), pkg, cb);
101
});
102
});
103
return;
104
}
105
106
loadAsFile(path.join(x, '/index'), pkg, cb);
107
});
108
});
109
}
110
111
function loadNodeModules (x, start, cb) {
112
(function process (dirs) {
113
if (dirs.length === 0) return cb(null, undefined);
114
var dir = dirs[0];
115
116
loadAsFile(path.join(dir, '/', x), undefined, function (err, m, pkg) {
117
if (err) return cb(err);
118
if (m) return cb(null, m, pkg);
119
loadAsDirectory(path.join(dir, '/', x), undefined, function (err, n, pkg) {
120
if (err) return cb(err);
121
if (n) return cb(null, n, pkg);
122
process(dirs.slice(1));
123
});
124
});
125
})(nodeModulesPaths(start, opts));
126
}
127
};
128
129