sync.js 6.1 KB

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