source: imaps-frontend/node_modules/resolve/lib/sync.js@ d565449

main
Last change on this file since d565449 was d565449, checked in by stefan toskovski <stefantoska84@…>, 4 weeks ago

Update repo after prototype presentation

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