sync.js 6.5 KB

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