async.js 6.5 KB

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