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