aboutsummaryrefslogtreecommitdiff
path: root/node_modules/resolve/lib
diff options
context:
space:
mode:
Diffstat (limited to 'node_modules/resolve/lib')
-rw-r--r--node_modules/resolve/lib/async.js192
-rw-r--r--node_modules/resolve/lib/caller.js8
-rw-r--r--node_modules/resolve/lib/core.js4
-rw-r--r--node_modules/resolve/lib/core.json38
-rw-r--r--node_modules/resolve/lib/node-modules-paths.js38
-rw-r--r--node_modules/resolve/lib/sync.js81
6 files changed, 361 insertions, 0 deletions
diff --git a/node_modules/resolve/lib/async.js b/node_modules/resolve/lib/async.js
new file mode 100644
index 000000000..0f0eeca56
--- /dev/null
+++ b/node_modules/resolve/lib/async.js
@@ -0,0 +1,192 @@
+var core = require('./core');
+var fs = require('fs');
+var path = require('path');
+var caller = require('./caller.js');
+var nodeModulesPaths = require('./node-modules-paths.js');
+var splitRe = process.platform === 'win32' ? /[\/\\]/ : /\//;
+
+module.exports = function resolve (x, opts, cb) {
+ if (typeof opts === 'function') {
+ cb = opts;
+ opts = {};
+ }
+ if (!opts) opts = {};
+ if (typeof x !== 'string') {
+ return process.nextTick(function () {
+ cb(new Error('path must be a string'));
+ });
+ }
+
+ var isFile = opts.isFile || function (file, cb) {
+ fs.stat(file, function (err, stat) {
+ if (err && err.code === 'ENOENT') cb(null, false)
+ else if (err) cb(err)
+ else cb(null, stat.isFile() || stat.isFIFO())
+ });
+ };
+ var readFile = opts.readFile || fs.readFile;
+
+ var extensions = opts.extensions || [ '.js' ];
+ var y = opts.basedir || path.dirname(caller());
+
+ opts.paths = opts.paths || [];
+
+ if (/^(?:\.\.?(?:\/|$)|\/|([A-Za-z]:)?[\\\/])/.test(x)) {
+ var res = path.resolve(y, x);
+ if (x === '..') res += '/';
+ if (/\/$/.test(x) && res === y) {
+ loadAsDirectory(res, opts.package, onfile);
+ }
+ else loadAsFile(res, opts.package, onfile);
+ }
+ else loadNodeModules(x, y, function (err, n, pkg) {
+ if (err) cb(err)
+ else if (n) cb(null, n, pkg)
+ else if (core[x]) return cb(null, x);
+ else cb(new Error("Cannot find module '" + x + "' from '" + y + "'"))
+ });
+
+ function onfile (err, m, pkg) {
+ if (err) cb(err)
+ else if (m) cb(null, m, pkg)
+ else loadAsDirectory(res, function (err, d, pkg) {
+ if (err) cb(err)
+ else if (d) cb(null, d, pkg)
+ else cb(new Error("Cannot find module '" + x + "' from '" + y + "'"))
+ })
+ }
+
+ function loadAsFile (x, pkg, cb) {
+ if (typeof pkg === 'function') {
+ cb = pkg;
+ pkg = undefined;
+ }
+
+ var exts = [''].concat(extensions);
+ load(exts, x, pkg)
+
+ function load (exts, x, pkg) {
+ if (exts.length === 0) return cb(null, undefined, pkg);
+ var file = x + exts[0];
+
+ if (pkg) onpkg(null, pkg)
+ else loadpkg(path.dirname(file), onpkg);
+
+ function onpkg (err, pkg_, dir) {
+ pkg = pkg_;
+ if (err) return cb(err)
+ if (dir && pkg && opts.pathFilter) {
+ var rfile = path.relative(dir, file);
+ var rel = rfile.slice(0, rfile.length - exts[0].length);
+ var r = opts.pathFilter(pkg, x, rel);
+ if (r) return load(
+ [''].concat(extensions.slice()),
+ path.resolve(dir, r),
+ pkg
+ );
+ }
+ isFile(file, onex);
+ }
+ function onex (err, ex) {
+ if (err) cb(err)
+ else if (!ex) load(exts.slice(1), x, pkg)
+ else cb(null, file, pkg)
+ }
+ }
+ }
+
+ function loadpkg (dir, cb) {
+ if (dir === '' || dir === '/') return cb(null);
+ if (process.platform === 'win32' && /^\w:[\\\/]*$/.test(dir)) {
+ return cb(null);
+ }
+ if (/[\\\/]node_modules[\\\/]*$/.test(dir)) return cb(null);
+
+ var pkgfile = path.join(dir, 'package.json');
+ isFile(pkgfile, function (err, ex) {
+ // on err, ex is false
+ if (!ex) return loadpkg(
+ path.dirname(dir), cb
+ );
+
+ readFile(pkgfile, function (err, body) {
+ if (err) cb(err);
+ try { var pkg = JSON.parse(body) }
+ catch (err) {}
+
+ if (pkg && opts.packageFilter) {
+ pkg = opts.packageFilter(pkg, pkgfile);
+ }
+ cb(null, pkg, dir);
+ });
+ });
+ }
+
+ function loadAsDirectory (x, fpkg, cb) {
+ if (typeof fpkg === 'function') {
+ cb = fpkg;
+ fpkg = opts.package;
+ }
+
+ var pkgfile = path.join(x, '/package.json');
+ isFile(pkgfile, function (err, ex) {
+ if (err) return cb(err);
+ if (!ex) return loadAsFile(path.join(x, '/index'), fpkg, cb);
+
+ readFile(pkgfile, function (err, body) {
+ if (err) return cb(err);
+ try {
+ var pkg = JSON.parse(body);
+ }
+ catch (err) {}
+
+ if (opts.packageFilter) {
+ pkg = opts.packageFilter(pkg, pkgfile);
+ }
+
+ if (pkg.main) {
+ if (pkg.main === '.' || pkg.main === './'){
+ pkg.main = 'index'
+ }
+ loadAsFile(path.resolve(x, pkg.main), pkg, function (err, m, pkg) {
+ if (err) return cb(err);
+ if (m) return cb(null, m, pkg);
+ if (!pkg) return loadAsFile(path.join(x, '/index'), pkg, cb);
+
+ var dir = path.resolve(x, pkg.main);
+ loadAsDirectory(dir, pkg, function (err, n, pkg) {
+ if (err) return cb(err);
+ if (n) return cb(null, n, pkg);
+ loadAsFile(path.join(x, '/index'), pkg, cb);
+ });
+ });
+ return;
+ }
+
+ loadAsFile(path.join(x, '/index'), pkg, cb);
+ });
+ });
+ }
+
+ function loadNodeModules (x, start, cb) {
+ (function process (dirs) {
+ if (dirs.length === 0) return cb(null, undefined);
+ var dir = dirs[0];
+
+ var file = path.join(dir, '/', x);
+ loadAsFile(file, undefined, onfile);
+
+ function onfile (err, m, pkg) {
+ if (err) return cb(err);
+ if (m) return cb(null, m, pkg);
+ loadAsDirectory(path.join(dir, '/', x), undefined, ondir);
+ }
+
+ function ondir (err, n, pkg) {
+ if (err) return cb(err);
+ if (n) return cb(null, n, pkg);
+ process(dirs.slice(1));
+ }
+ })(nodeModulesPaths(start, opts));
+ }
+};
diff --git a/node_modules/resolve/lib/caller.js b/node_modules/resolve/lib/caller.js
new file mode 100644
index 000000000..5536549b0
--- /dev/null
+++ b/node_modules/resolve/lib/caller.js
@@ -0,0 +1,8 @@
+module.exports = function () {
+ // see https://code.google.com/p/v8/wiki/JavaScriptStackTraceApi
+ var origPrepareStackTrace = Error.prepareStackTrace;
+ Error.prepareStackTrace = function (_, stack) { return stack };
+ var stack = (new Error()).stack;
+ Error.prepareStackTrace = origPrepareStackTrace;
+ return stack[2].getFileName();
+};
diff --git a/node_modules/resolve/lib/core.js b/node_modules/resolve/lib/core.js
new file mode 100644
index 000000000..ea4a6c87e
--- /dev/null
+++ b/node_modules/resolve/lib/core.js
@@ -0,0 +1,4 @@
+module.exports = require('./core.json').reduce(function (acc, x) {
+ acc[x] = true;
+ return acc;
+}, {});
diff --git a/node_modules/resolve/lib/core.json b/node_modules/resolve/lib/core.json
new file mode 100644
index 000000000..28560f7ef
--- /dev/null
+++ b/node_modules/resolve/lib/core.json
@@ -0,0 +1,38 @@
+[
+ "assert",
+ "buffer_ieee754",
+ "buffer",
+ "child_process",
+ "cluster",
+ "console",
+ "constants",
+ "crypto",
+ "_debugger",
+ "dgram",
+ "dns",
+ "domain",
+ "events",
+ "freelist",
+ "fs",
+ "http",
+ "https",
+ "_linklist",
+ "module",
+ "net",
+ "os",
+ "path",
+ "punycode",
+ "querystring",
+ "readline",
+ "repl",
+ "stream",
+ "string_decoder",
+ "sys",
+ "timers",
+ "tls",
+ "tty",
+ "url",
+ "util",
+ "vm",
+ "zlib"
+]
diff --git a/node_modules/resolve/lib/node-modules-paths.js b/node_modules/resolve/lib/node-modules-paths.js
new file mode 100644
index 000000000..ce0a0d9f2
--- /dev/null
+++ b/node_modules/resolve/lib/node-modules-paths.js
@@ -0,0 +1,38 @@
+var path = require('path');
+
+module.exports = function (start, opts) {
+ var modules = opts.moduleDirectory
+ ? [].concat(opts.moduleDirectory)
+ : ['node_modules']
+ ;
+
+ // ensure that `start` is an absolute path at this point,
+ // resolving against the process' current working directory
+ start = path.resolve(start);
+
+ var prefix = '/';
+ if (/^([A-Za-z]:)/.test(start)) {
+ prefix = '';
+ } else if (/^\\\\/.test(start)) {
+ prefix = '\\\\';
+ }
+
+ var splitRe = process.platform === 'win32' ? /[\/\\]/ : /\/+/;
+
+ var parts = start.split(splitRe);
+
+ var dirs = [];
+ for (var i = parts.length - 1; i >= 0; i--) {
+ if (modules.indexOf(parts[i]) !== -1) continue;
+ dirs = dirs.concat(modules.map(function(module_dir) {
+ return prefix + path.join(
+ path.join.apply(path, parts.slice(0, i + 1)),
+ module_dir
+ );
+ }));
+ }
+ if (process.platform === 'win32'){
+ dirs[dirs.length-1] = dirs[dirs.length-1].replace(":", ":\\");
+ }
+ return dirs.concat(opts.paths);
+}
diff --git a/node_modules/resolve/lib/sync.js b/node_modules/resolve/lib/sync.js
new file mode 100644
index 000000000..ef91eddbb
--- /dev/null
+++ b/node_modules/resolve/lib/sync.js
@@ -0,0 +1,81 @@
+var core = require('./core');
+var fs = require('fs');
+var path = require('path');
+var caller = require('./caller.js');
+var nodeModulesPaths = require('./node-modules-paths.js');
+
+module.exports = function (x, opts) {
+ if (!opts) opts = {};
+ var isFile = opts.isFile || function (file) {
+ try { var stat = fs.statSync(file) }
+ catch (err) { if (err && err.code === 'ENOENT') return false }
+ return stat.isFile() || stat.isFIFO();
+ };
+ var readFileSync = opts.readFileSync || fs.readFileSync;
+
+ var extensions = opts.extensions || [ '.js' ];
+ var y = opts.basedir || path.dirname(caller());
+
+ opts.paths = opts.paths || [];
+
+ if (/^(?:\.\.?(?:\/|$)|\/|([A-Za-z]:)?[\\\/])/.test(x)) {
+ var res = path.resolve(y, x);
+ if (x === '..') res += '/';
+ var m = loadAsFileSync(res) || loadAsDirectorySync(res);
+ if (m) return m;
+ } else {
+ var n = loadNodeModulesSync(x, y);
+ if (n) return n;
+ }
+
+ if (core[x]) return x;
+
+ throw new Error("Cannot find module '" + x + "' from '" + y + "'");
+
+ function loadAsFileSync (x) {
+ if (isFile(x)) {
+ return x;
+ }
+
+ for (var i = 0; i < extensions.length; i++) {
+ var file = x + extensions[i];
+ if (isFile(file)) {
+ return file;
+ }
+ }
+ }
+
+ function loadAsDirectorySync (x) {
+ var pkgfile = path.join(x, '/package.json');
+ if (isFile(pkgfile)) {
+ var body = readFileSync(pkgfile, 'utf8');
+ try {
+ var pkg = JSON.parse(body);
+ if (opts.packageFilter) {
+ pkg = opts.packageFilter(pkg, x);
+ }
+
+ if (pkg.main) {
+ var m = loadAsFileSync(path.resolve(x, pkg.main));
+ if (m) return m;
+ var n = loadAsDirectorySync(path.resolve(x, pkg.main));
+ if (n) return n;
+ }
+ }
+ catch (err) {}
+ }
+
+ return loadAsFileSync(path.join( x, '/index'));
+ }
+
+ function loadNodeModulesSync (x, start) {
+ var dirs = nodeModulesPaths(start, opts);
+ for (var i = 0; i < dirs.length; i++) {
+ var dir = dirs[i];
+ var m = loadAsFileSync(path.join( dir, '/', x));
+ if (m) return m;
+ var n = loadAsDirectorySync(path.join( dir, '/', x ));
+ if (n) return n;
+ }
+ }
+};