1 | var isCore = require('is-core-module');
|
---|
2 | var fs = require('fs');
|
---|
3 | var path = require('path');
|
---|
4 | var caller = require('./caller');
|
---|
5 | var nodeModulesPaths = require('./node-modules-paths');
|
---|
6 | var normalizeOptions = require('./normalize-options');
|
---|
7 |
|
---|
8 | var realpathFS = fs.realpathSync && typeof fs.realpathSync.native === 'function' ? fs.realpathSync.native : fs.realpathSync;
|
---|
9 |
|
---|
10 | var defaultIsFile = function isFile(file) {
|
---|
11 | try {
|
---|
12 | var stat = fs.statSync(file);
|
---|
13 | } catch (e) {
|
---|
14 | if (e && (e.code === 'ENOENT' || e.code === 'ENOTDIR')) return false;
|
---|
15 | throw e;
|
---|
16 | }
|
---|
17 | return stat.isFile() || stat.isFIFO();
|
---|
18 | };
|
---|
19 |
|
---|
20 | var defaultIsDir = function isDirectory(dir) {
|
---|
21 | try {
|
---|
22 | var stat = fs.statSync(dir);
|
---|
23 | } catch (e) {
|
---|
24 | if (e && (e.code === 'ENOENT' || e.code === 'ENOTDIR')) return false;
|
---|
25 | throw e;
|
---|
26 | }
|
---|
27 | return stat.isDirectory();
|
---|
28 | };
|
---|
29 |
|
---|
30 | var defaultRealpathSync = function realpathSync(x) {
|
---|
31 | try {
|
---|
32 | return realpathFS(x);
|
---|
33 | } catch (realpathErr) {
|
---|
34 | if (realpathErr.code !== 'ENOENT') {
|
---|
35 | throw realpathErr;
|
---|
36 | }
|
---|
37 | }
|
---|
38 | return x;
|
---|
39 | };
|
---|
40 |
|
---|
41 | var maybeRealpathSync = function maybeRealpathSync(realpathSync, x, opts) {
|
---|
42 | if (opts && opts.preserveSymlinks === false) {
|
---|
43 | return realpathSync(x);
|
---|
44 | }
|
---|
45 | return x;
|
---|
46 | };
|
---|
47 |
|
---|
48 | var defaultReadPackageSync = function defaultReadPackageSync(readFileSync, pkgfile) {
|
---|
49 | var body = readFileSync(pkgfile);
|
---|
50 | try {
|
---|
51 | var pkg = JSON.parse(body);
|
---|
52 | return pkg;
|
---|
53 | } catch (jsonErr) {}
|
---|
54 | };
|
---|
55 |
|
---|
56 | var getPackageCandidates = function getPackageCandidates(x, start, opts) {
|
---|
57 | var dirs = nodeModulesPaths(start, opts, x);
|
---|
58 | for (var i = 0; i < dirs.length; i++) {
|
---|
59 | dirs[i] = path.join(dirs[i], x);
|
---|
60 | }
|
---|
61 | return dirs;
|
---|
62 | };
|
---|
63 |
|
---|
64 | module.exports = function resolveSync(x, options) {
|
---|
65 | if (typeof x !== 'string') {
|
---|
66 | throw new TypeError('Path must be a string.');
|
---|
67 | }
|
---|
68 | var opts = normalizeOptions(x, options);
|
---|
69 |
|
---|
70 | var isFile = opts.isFile || defaultIsFile;
|
---|
71 | var readFileSync = opts.readFileSync || fs.readFileSync;
|
---|
72 | var isDirectory = opts.isDirectory || defaultIsDir;
|
---|
73 | var realpathSync = opts.realpathSync || defaultRealpathSync;
|
---|
74 | var readPackageSync = opts.readPackageSync || defaultReadPackageSync;
|
---|
75 | if (opts.readFileSync && opts.readPackageSync) {
|
---|
76 | throw new TypeError('`readFileSync` and `readPackageSync` are mutually exclusive.');
|
---|
77 | }
|
---|
78 | var packageIterator = opts.packageIterator;
|
---|
79 |
|
---|
80 | var extensions = opts.extensions || ['.js'];
|
---|
81 | var includeCoreModules = opts.includeCoreModules !== false;
|
---|
82 | var basedir = opts.basedir || path.dirname(caller());
|
---|
83 | var parent = opts.filename || basedir;
|
---|
84 |
|
---|
85 | opts.paths = opts.paths || [];
|
---|
86 |
|
---|
87 | // ensure that `basedir` is an absolute path at this point, resolving against the process' current working directory
|
---|
88 | var absoluteStart = maybeRealpathSync(realpathSync, path.resolve(basedir), opts);
|
---|
89 |
|
---|
90 | if ((/^(?:\.\.?(?:\/|$)|\/|([A-Za-z]:)?[/\\])/).test(x)) {
|
---|
91 | var res = path.resolve(absoluteStart, x);
|
---|
92 | if (x === '.' || x === '..' || x.slice(-1) === '/') res += '/';
|
---|
93 | var m = loadAsFileSync(res) || loadAsDirectorySync(res);
|
---|
94 | if (m) return maybeRealpathSync(realpathSync, m, opts);
|
---|
95 | } else if (includeCoreModules && isCore(x)) {
|
---|
96 | return x;
|
---|
97 | } else {
|
---|
98 | var n = loadNodeModulesSync(x, absoluteStart);
|
---|
99 | if (n) return maybeRealpathSync(realpathSync, n, opts);
|
---|
100 | }
|
---|
101 |
|
---|
102 | var err = new Error("Cannot find module '" + x + "' from '" + parent + "'");
|
---|
103 | err.code = 'MODULE_NOT_FOUND';
|
---|
104 | throw err;
|
---|
105 |
|
---|
106 | function loadAsFileSync(x) {
|
---|
107 | var pkg = loadpkg(path.dirname(x));
|
---|
108 |
|
---|
109 | if (pkg && pkg.dir && pkg.pkg && opts.pathFilter) {
|
---|
110 | var rfile = path.relative(pkg.dir, x);
|
---|
111 | var r = opts.pathFilter(pkg.pkg, x, rfile);
|
---|
112 | if (r) {
|
---|
113 | x = path.resolve(pkg.dir, r); // eslint-disable-line no-param-reassign
|
---|
114 | }
|
---|
115 | }
|
---|
116 |
|
---|
117 | if (isFile(x)) {
|
---|
118 | return x;
|
---|
119 | }
|
---|
120 |
|
---|
121 | for (var i = 0; i < extensions.length; i++) {
|
---|
122 | var file = x + extensions[i];
|
---|
123 | if (isFile(file)) {
|
---|
124 | return file;
|
---|
125 | }
|
---|
126 | }
|
---|
127 | }
|
---|
128 |
|
---|
129 | function loadpkg(dir) {
|
---|
130 | if (dir === '' || dir === '/') return;
|
---|
131 | if (process.platform === 'win32' && (/^\w:[/\\]*$/).test(dir)) {
|
---|
132 | return;
|
---|
133 | }
|
---|
134 | if ((/[/\\]node_modules[/\\]*$/).test(dir)) return;
|
---|
135 |
|
---|
136 | var pkgfile = path.join(maybeRealpathSync(realpathSync, dir, opts), 'package.json');
|
---|
137 |
|
---|
138 | if (!isFile(pkgfile)) {
|
---|
139 | return loadpkg(path.dirname(dir));
|
---|
140 | }
|
---|
141 |
|
---|
142 | var pkg = readPackageSync(readFileSync, pkgfile);
|
---|
143 |
|
---|
144 | if (pkg && opts.packageFilter) {
|
---|
145 | // v2 will pass pkgfile
|
---|
146 | pkg = opts.packageFilter(pkg, /*pkgfile,*/ dir); // eslint-disable-line spaced-comment
|
---|
147 | }
|
---|
148 |
|
---|
149 | return { pkg: pkg, dir: dir };
|
---|
150 | }
|
---|
151 |
|
---|
152 | function loadAsDirectorySync(x) {
|
---|
153 | var pkgfile = path.join(maybeRealpathSync(realpathSync, x, opts), '/package.json');
|
---|
154 | if (isFile(pkgfile)) {
|
---|
155 | try {
|
---|
156 | var pkg = readPackageSync(readFileSync, pkgfile);
|
---|
157 | } catch (e) {}
|
---|
158 |
|
---|
159 | if (pkg && opts.packageFilter) {
|
---|
160 | // v2 will pass pkgfile
|
---|
161 | pkg = opts.packageFilter(pkg, /*pkgfile,*/ x); // eslint-disable-line spaced-comment
|
---|
162 | }
|
---|
163 |
|
---|
164 | if (pkg && pkg.main) {
|
---|
165 | if (typeof pkg.main !== 'string') {
|
---|
166 | var mainError = new TypeError('package “' + pkg.name + '” `main` must be a string');
|
---|
167 | mainError.code = 'INVALID_PACKAGE_MAIN';
|
---|
168 | throw mainError;
|
---|
169 | }
|
---|
170 | if (pkg.main === '.' || pkg.main === './') {
|
---|
171 | pkg.main = 'index';
|
---|
172 | }
|
---|
173 | try {
|
---|
174 | var m = loadAsFileSync(path.resolve(x, pkg.main));
|
---|
175 | if (m) return m;
|
---|
176 | var n = loadAsDirectorySync(path.resolve(x, pkg.main));
|
---|
177 | if (n) return n;
|
---|
178 | } catch (e) {}
|
---|
179 | }
|
---|
180 | }
|
---|
181 |
|
---|
182 | return loadAsFileSync(path.join(x, '/index'));
|
---|
183 | }
|
---|
184 |
|
---|
185 | function loadNodeModulesSync(x, start) {
|
---|
186 | var thunk = function () { return getPackageCandidates(x, start, opts); };
|
---|
187 | var dirs = packageIterator ? packageIterator(x, start, thunk, opts) : thunk();
|
---|
188 |
|
---|
189 | for (var i = 0; i < dirs.length; i++) {
|
---|
190 | var dir = dirs[i];
|
---|
191 | if (isDirectory(path.dirname(dir))) {
|
---|
192 | var m = loadAsFileSync(dir);
|
---|
193 | if (m) return m;
|
---|
194 | var n = loadAsDirectorySync(dir);
|
---|
195 | if (n) return n;
|
---|
196 | }
|
---|
197 | }
|
---|
198 | }
|
---|
199 | };
|
---|