async.js 8.4 KB

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