sync.js 6.8 KB

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