You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

174 lines
5.4 KiB

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