123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. var core = require('./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 defaultIsFile = function isFile(file, cb) {
  7. fs.stat(file, function (err, stat) {
  8. if (!err) {
  9. return cb(null, stat.isFile() || stat.isFIFO());
  10. }
  11. if (err.code === 'ENOENT' || err.code === 'ENOTDIR') return cb(null, false);
  12. return cb(err);
  13. });
  14. };
  15. module.exports = function resolve(x, options, callback) {
  16. var cb = callback;
  17. var opts = options || {};
  18. if (typeof opts === 'function') {
  19. cb = opts;
  20. opts = {};
  21. }
  22. if (typeof x !== 'string') {
  23. var err = new TypeError('Path must be a string.');
  24. return process.nextTick(function () {
  25. cb(err);
  26. });
  27. }
  28. var isFile = opts.isFile || defaultIsFile;
  29. var readFile = opts.readFile || fs.readFile;
  30. var extensions = opts.extensions || ['.js'];
  31. var basedir = opts.basedir || path.dirname(caller());
  32. var parent = opts.filename || basedir;
  33. opts.paths = opts.paths || [];
  34. if (/^(?:\.\.?(?:\/|$)|\/|([A-Za-z]:)?[/\\])/.test(x)) {
  35. var res = path.resolve(basedir, x);
  36. if (x === '..' || x.slice(-1) === '/') res += '/';
  37. if (/\/$/.test(x) && res === basedir) {
  38. loadAsDirectory(res, opts.package, onfile);
  39. } else loadAsFile(res, opts.package, onfile);
  40. } else loadNodeModules(x, basedir, function (err, n, pkg) {
  41. if (err) cb(err);
  42. else if (n) cb(null, n, pkg);
  43. else if (core[x]) return cb(null, x);
  44. else {
  45. var moduleError = new Error("Cannot find module '" + x + "' from '" + parent + "'");
  46. moduleError.code = 'MODULE_NOT_FOUND';
  47. cb(moduleError);
  48. }
  49. });
  50. function onfile(err, m, pkg) {
  51. if (err) cb(err);
  52. else if (m) cb(null, m, pkg);
  53. else loadAsDirectory(res, function (err, d, pkg) {
  54. if (err) cb(err);
  55. else if (d) cb(null, d, pkg);
  56. else {
  57. var moduleError = new Error("Cannot find module '" + x + "' from '" + parent + "'");
  58. moduleError.code = 'MODULE_NOT_FOUND';
  59. cb(moduleError);
  60. }
  61. });
  62. }
  63. function loadAsFile(x, thePackage, callback) {
  64. var loadAsFilePackage = thePackage;
  65. var cb = callback;
  66. if (typeof loadAsFilePackage === 'function') {
  67. cb = loadAsFilePackage;
  68. loadAsFilePackage = undefined;
  69. }
  70. var exts = [''].concat(extensions);
  71. load(exts, x, loadAsFilePackage);
  72. function load(exts, x, loadPackage) {
  73. if (exts.length === 0) return cb(null, undefined, loadPackage);
  74. var file = x + exts[0];
  75. var pkg = loadPackage;
  76. if (pkg) onpkg(null, pkg);
  77. else loadpkg(path.dirname(file), onpkg);
  78. function onpkg(err, pkg_, dir) {
  79. pkg = pkg_;
  80. if (err) return cb(err);
  81. if (dir && pkg && opts.pathFilter) {
  82. var rfile = path.relative(dir, file);
  83. var rel = rfile.slice(0, rfile.length - exts[0].length);
  84. var r = opts.pathFilter(pkg, x, rel);
  85. if (r) return load(
  86. [''].concat(extensions.slice()),
  87. path.resolve(dir, r),
  88. pkg
  89. );
  90. }
  91. isFile(file, onex);
  92. }
  93. function onex(err, ex) {
  94. if (err) return cb(err);
  95. if (ex) return cb(null, file, pkg);
  96. load(exts.slice(1), x, pkg);
  97. }
  98. }
  99. }
  100. function loadpkg(dir, cb) {
  101. if (dir === '' || dir === '/') return cb(null);
  102. if (process.platform === 'win32' && (/^\w:[/\\]*$/).test(dir)) {
  103. return cb(null);
  104. }
  105. if (/[/\\]node_modules[/\\]*$/.test(dir)) return cb(null);
  106. var pkgfile = path.join(dir, 'package.json');
  107. isFile(pkgfile, function (err, ex) {
  108. // on err, ex is false
  109. if (!ex) return loadpkg(path.dirname(dir), cb);
  110. readFile(pkgfile, function (err, body) {
  111. if (err) cb(err);
  112. try { var pkg = JSON.parse(body); } catch (jsonErr) {}
  113. if (pkg && opts.packageFilter) {
  114. pkg = opts.packageFilter(pkg, pkgfile);
  115. }
  116. cb(null, pkg, dir);
  117. });
  118. });
  119. }
  120. function loadAsDirectory(x, loadAsDirectoryPackage, callback) {
  121. var cb = callback;
  122. var fpkg = loadAsDirectoryPackage;
  123. if (typeof fpkg === 'function') {
  124. cb = fpkg;
  125. fpkg = opts.package;
  126. }
  127. var pkgfile = path.join(x, 'package.json');
  128. isFile(pkgfile, function (err, ex) {
  129. if (err) return cb(err);
  130. if (!ex) return loadAsFile(path.join(x, 'index'), fpkg, cb);
  131. readFile(pkgfile, function (err, body) {
  132. if (err) return cb(err);
  133. try {
  134. var pkg = JSON.parse(body);
  135. } catch (jsonErr) {}
  136. if (opts.packageFilter) {
  137. pkg = opts.packageFilter(pkg, pkgfile);
  138. }
  139. if (pkg.main) {
  140. if (pkg.main === '.' || pkg.main === './') {
  141. pkg.main = 'index';
  142. }
  143. loadAsFile(path.resolve(x, pkg.main), pkg, function (err, m, pkg) {
  144. if (err) return cb(err);
  145. if (m) return cb(null, m, pkg);
  146. if (!pkg) return loadAsFile(path.join(x, 'index'), pkg, cb);
  147. var dir = path.resolve(x, pkg.main);
  148. loadAsDirectory(dir, pkg, function (err, n, pkg) {
  149. if (err) return cb(err);
  150. if (n) return cb(null, n, pkg);
  151. loadAsFile(path.join(x, 'index'), pkg, cb);
  152. });
  153. });
  154. return;
  155. }
  156. loadAsFile(path.join(x, '/index'), pkg, cb);
  157. });
  158. });
  159. }
  160. function processDirs(cb, dirs) {
  161. if (dirs.length === 0) return cb(null, undefined);
  162. var dir = dirs[0];
  163. var file = path.join(dir, x);
  164. loadAsFile(file, opts.package, onfile);
  165. function onfile(err, m, pkg) {
  166. if (err) return cb(err);
  167. if (m) return cb(null, m, pkg);
  168. loadAsDirectory(path.join(dir, x), opts.package, ondir);
  169. }
  170. function ondir(err, n, pkg) {
  171. if (err) return cb(err);
  172. if (n) return cb(null, n, pkg);
  173. processDirs(cb, dirs.slice(1));
  174. }
  175. }
  176. function loadNodeModules(x, start, cb) {
  177. processDirs(cb, nodeModulesPaths(start, opts));
  178. }
  179. };