async.js 7.8 KB

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