Initial pass

This commit is contained in:
Danny McCormick 2019-06-26 21:12:00 -04:00
commit 39c08a0eaa
7242 changed files with 1886006 additions and 0 deletions

View file

@ -0,0 +1,4 @@
language: node_js
node_js:
- 0.6
- 0.8

View file

@ -0,0 +1,18 @@
This software is released under the MIT license:
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

View file

@ -0,0 +1,5 @@
var resolve = require('../');
resolve('tap', { basedir: __dirname }, function (err, res) {
if (err) console.error(err)
else console.log(res)
});

View file

@ -0,0 +1,3 @@
var resolve = require('../');
var res = resolve.sync('tap', { basedir: __dirname });
console.log(res);

View file

@ -0,0 +1,5 @@
var core = require('./lib/core');
exports = module.exports = require('./lib/async');
exports.core = core;
exports.isCore = function (x) { return core[x] };
exports.sync = require('./lib/sync');

View file

@ -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));
}
};

View file

@ -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();
};

View file

@ -0,0 +1,4 @@
module.exports = require('./core.json').reduce(function (acc, x) {
acc[x] = true;
return acc;
}, {});

View file

@ -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"
]

View file

@ -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);
}

View file

@ -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;
}
}
};

View file

@ -0,0 +1,62 @@
{
"_args": [
[
"resolve@1.1.7",
"C:\\Users\\Administrator\\Documents\\setup-python"
]
],
"_development": true,
"_from": "resolve@1.1.7",
"_id": "resolve@1.1.7",
"_inBundle": false,
"_integrity": "sha1-IDEU2CrSxe2ejgQRs5ModeiJ6Xs=",
"_location": "/browser-resolve/resolve",
"_phantomChildren": {},
"_requested": {
"type": "version",
"registry": true,
"raw": "resolve@1.1.7",
"name": "resolve",
"escapedName": "resolve",
"rawSpec": "1.1.7",
"saveSpec": null,
"fetchSpec": "1.1.7"
},
"_requiredBy": [
"/browser-resolve"
],
"_resolved": "https://registry.npmjs.org/resolve/-/resolve-1.1.7.tgz",
"_spec": "1.1.7",
"_where": "C:\\Users\\Administrator\\Documents\\setup-python",
"author": {
"name": "James Halliday",
"email": "mail@substack.net",
"url": "http://substack.net"
},
"bugs": {
"url": "https://github.com/substack/node-resolve/issues"
},
"description": "resolve like require.resolve() on behalf of files asynchronously and synchronously",
"devDependencies": {
"tap": "0.4.13",
"tape": "^3.5.0"
},
"homepage": "https://github.com/substack/node-resolve#readme",
"keywords": [
"resolve",
"require",
"node",
"module"
],
"license": "MIT",
"main": "index.js",
"name": "resolve",
"repository": {
"type": "git",
"url": "git://github.com/substack/node-resolve.git"
},
"scripts": {
"test": "tape test/*.js"
},
"version": "1.1.7"
}

View file

@ -0,0 +1,148 @@
# resolve
implements the [node `require.resolve()`
algorithm](http://nodejs.org/docs/v0.4.8/api/all.html#all_Together...)
such that you can `require.resolve()` on behalf of a file asynchronously and
synchronously
[![build status](https://secure.travis-ci.org/substack/node-resolve.png)](http://travis-ci.org/substack/node-resolve)
# example
asynchronously resolve:
``` js
var resolve = require('resolve');
resolve('tap', { basedir: __dirname }, function (err, res) {
if (err) console.error(err)
else console.log(res)
});
```
```
$ node example/async.js
/home/substack/projects/node-resolve/node_modules/tap/lib/main.js
```
synchronously resolve:
``` js
var resolve = require('resolve');
var res = resolve.sync('tap', { basedir: __dirname });
console.log(res);
```
```
$ node example/sync.js
/home/substack/projects/node-resolve/node_modules/tap/lib/main.js
```
# methods
``` js
var resolve = require('resolve')
```
## resolve(id, opts={}, cb)
Asynchronously resolve the module path string `id` into `cb(err, res [, pkg])`, where `pkg` (if defined) is the data from `package.json`.
options are:
* opts.basedir - directory to begin resolving from
* opts.package - `package.json` data applicable to the module being loaded
* opts.extensions - array of file extensions to search in order
* opts.readFile - how to read files asynchronously
* opts.isFile - function to asynchronously test whether a file exists
* opts.packageFilter - transform the parsed package.json contents before looking
at the "main" field
* opts.pathFilter(pkg, path, relativePath) - transform a path within a package
* pkg - package data
* path - the path being resolved
* relativePath - the path relative from the package.json location
* returns - a relative path that will be joined from the package.json location
* opts.paths - require.paths array to use if nothing is found on the normal
node_modules recursive walk (probably don't use this)
* opts.moduleDirectory - directory (or directories) in which to recursively look for modules. default: `"node_modules"`
default `opts` values:
``` javascript
{
paths: [],
basedir: __dirname,
extensions: [ '.js' ],
readFile: fs.readFile,
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())
});
},
moduleDirectory: 'node_modules'
}
```
## resolve.sync(id, opts)
Synchronously resolve the module path string `id`, returning the result and
throwing an error when `id` can't be resolved.
options are:
* opts.basedir - directory to begin resolving from
* opts.extensions - array of file extensions to search in order
* opts.readFile - how to read files synchronously
* opts.isFile - function to synchronously test whether a file exists
* `opts.packageFilter(pkg, pkgfile)` - transform the parsed package.json
* contents before looking at the "main" field
* opts.paths - require.paths array to use if nothing is found on the normal
node_modules recursive walk (probably don't use this)
* opts.moduleDirectory - directory (or directories) in which to recursively look for modules. default: `"node_modules"`
default `opts` values:
``` javascript
{
paths: [],
basedir: __dirname,
extensions: [ '.js' ],
readFileSync: fs.readFileSync,
isFile: function (file) {
try { return fs.statSync(file).isFile() }
catch (e) { return false }
},
moduleDirectory: 'node_modules'
}
````
## resolve.isCore(pkg)
Return whether a package is in core.
# install
With [npm](https://npmjs.org) do:
```
npm install resolve
```
# license
MIT

View file

@ -0,0 +1,12 @@
var test = require('tape');
var resolve = require('../');
test('core modules', function (t) {
t.ok(resolve.isCore('fs'));
t.ok(resolve.isCore('net'));
t.ok(resolve.isCore('http'));
t.ok(!resolve.isCore('seq'));
t.ok(!resolve.isCore('../'));
t.end();
});

View file

@ -0,0 +1,29 @@
var path = require('path');
var test = require('tape');
var resolve = require('../');
test('dotdot', function (t) {
t.plan(4);
var dir = __dirname + '/dotdot/abc';
resolve('..', { basedir : dir }, function (err, res, pkg) {
t.ifError(err);
t.equal(res, __dirname + '/dotdot/index.js');
});
resolve('.', { basedir : dir }, function (err, res, pkg) {
t.ifError(err);
t.equal(res, dir + '/index.js');
});
});
test('dotdot sync', function (t) {
t.plan(2);
var dir = __dirname + '/dotdot/abc';
var a = resolve.sync('..', { basedir : dir });
t.equal(a, __dirname + '/dotdot/index.js');
var b = resolve.sync('.', { basedir : dir });
t.equal(b, dir + '/index.js');
});

View file

@ -0,0 +1,2 @@
var x = require('..');
console.log(x);

View file

@ -0,0 +1 @@
module.exports = 'whatever'

View file

@ -0,0 +1,17 @@
var path = require('path');
var test = require('tape');
var resolve = require('../');
// not sure what's up with this test anymore
if (process.platform !== 'win32') return;
test('faulty basedir must produce error in windows', function (t) {
t.plan(1);
var resolverDir = 'C:\\a\\b\\c\\d';
resolve('tape/lib/test.js', { basedir : resolverDir }, function (err, res, pkg) {
t.equal(true, !!err);
});
});

View file

@ -0,0 +1,18 @@
var test = require('tape');
var resolve = require('../');
test('filter', function (t) {
t.plan(2);
var dir = __dirname + '/resolver';
resolve('./baz', {
basedir : dir,
packageFilter : function (pkg) {
pkg.main = 'doom';
return pkg;
}
}, function (err, res, pkg) {
if (err) t.fail(err);
t.equal(res, dir + '/baz/doom.js');
t.equal(pkg.main, 'doom');
});
});

View file

@ -0,0 +1,15 @@
var test = require('tape');
var resolve = require('../');
test('filter', function (t) {
var dir = __dirname + '/resolver';
var res = resolve.sync('./baz', {
basedir : dir,
packageFilter : function (pkg) {
pkg.main = 'doom'
return pkg;
}
});
t.equal(res, dir + '/baz/doom.js');
t.end();
});

View file

@ -0,0 +1,142 @@
var test = require('tape');
var resolve = require('../');
test('mock', function (t) {
t.plan(6);
var files = {
'/foo/bar/baz.js' : 'beep'
};
function opts (basedir) {
return {
basedir : basedir,
isFile : function (file, cb) {
cb(null, files.hasOwnProperty(file));
},
readFile : function (file, cb) {
cb(null, files[file]);
}
}
}
resolve('./baz', opts('/foo/bar'), function (err, res, pkg) {
if (err) t.fail(err);
t.equal(res, '/foo/bar/baz.js');
t.equal(pkg, undefined);
});
resolve('./baz.js', opts('/foo/bar'), function (err, res, pkg) {
if (err) t.fail(err);
t.equal(res, '/foo/bar/baz.js');
t.equal(pkg, undefined);
});
resolve('baz', opts('/foo/bar'), function (err, res) {
t.equal(err.message, "Cannot find module 'baz' from '/foo/bar'");
});
resolve('../baz', opts('/foo/bar'), function (err, res) {
t.equal(err.message, "Cannot find module '../baz' from '/foo/bar'");
});
});
test('mock from package', function (t) {
t.plan(6);
var files = {
'/foo/bar/baz.js' : 'beep'
};
function opts (basedir) {
return {
basedir : basedir,
package : { main: 'bar' },
isFile : function (file, cb) {
cb(null, files.hasOwnProperty(file));
},
readFile : function (file, cb) {
cb(null, files[file]);
}
}
}
resolve('./baz', opts('/foo/bar'), function (err, res, pkg) {
if (err) t.fail(err);
t.equal(res, '/foo/bar/baz.js');
t.equal(pkg.main, 'bar');
});
resolve('./baz.js', opts('/foo/bar'), function (err, res, pkg) {
if (err) t.fail(err);
t.equal(res, '/foo/bar/baz.js');
t.equal(pkg.main, 'bar');
});
resolve('baz', opts('/foo/bar'), function (err, res) {
t.equal(err.message, "Cannot find module 'baz' from '/foo/bar'");
});
resolve('../baz', opts('/foo/bar'), function (err, res) {
t.equal(err.message, "Cannot find module '../baz' from '/foo/bar'");
});
});
test('mock package', function (t) {
t.plan(2);
var files = {
'/foo/node_modules/bar/baz.js' : 'beep',
'/foo/node_modules/bar/package.json' : JSON.stringify({
main : './baz.js'
})
};
function opts (basedir) {
return {
basedir : basedir,
isFile : function (file, cb) {
cb(null, files.hasOwnProperty(file));
},
readFile : function (file, cb) {
cb(null, files[file]);
}
}
}
resolve('bar', opts('/foo'), function (err, res, pkg) {
if (err) t.fail(err);
t.equal(res, '/foo/node_modules/bar/baz.js');
t.equal(pkg.main, './baz.js');
});
});
test('mock package from package', function (t) {
t.plan(2);
var files = {
'/foo/node_modules/bar/baz.js' : 'beep',
'/foo/node_modules/bar/package.json' : JSON.stringify({
main : './baz.js'
})
};
function opts (basedir) {
return {
basedir : basedir,
package : { main: 'bar' },
isFile : function (file, cb) {
cb(null, files.hasOwnProperty(file));
},
readFile : function (file, cb) {
cb(null, files[file]);
}
}
}
resolve('bar', opts('/foo'), function (err, res, pkg) {
if (err) t.fail(err);
t.equal(res, '/foo/node_modules/bar/baz.js');
t.equal(pkg.main, './baz.js');
});
});

View file

@ -0,0 +1,68 @@
var test = require('tape');
var resolve = require('../');
test('mock', function (t) {
t.plan(4);
var files = {
'/foo/bar/baz.js' : 'beep'
};
function opts (basedir) {
return {
basedir : basedir,
isFile : function (file) {
return files.hasOwnProperty(file)
},
readFileSync : function (file) {
return files[file]
}
}
}
t.equal(
resolve.sync('./baz', opts('/foo/bar')),
'/foo/bar/baz.js'
);
t.equal(
resolve.sync('./baz.js', opts('/foo/bar')),
'/foo/bar/baz.js'
);
t.throws(function () {
resolve.sync('baz', opts('/foo/bar'));
});
t.throws(function () {
resolve.sync('../baz', opts('/foo/bar'));
});
});
test('mock package', function (t) {
t.plan(1);
var files = {
'/foo/node_modules/bar/baz.js' : 'beep',
'/foo/node_modules/bar/package.json' : JSON.stringify({
main : './baz.js'
})
};
function opts (basedir) {
return {
basedir : basedir,
isFile : function (file) {
return files.hasOwnProperty(file)
},
readFileSync : function (file) {
return files[file]
}
}
}
t.equal(
resolve.sync('bar', opts('/foo')),
'/foo/node_modules/bar/baz.js'
);
});

View file

@ -0,0 +1,56 @@
var path = require('path');
var test = require('tape');
var resolve = require('../');
test('moduleDirectory strings', function (t) {
t.plan(4);
var dir = __dirname + '/module_dir';
var xopts = {
basedir : dir,
moduleDirectory: 'xmodules'
};
resolve('aaa', xopts, function (err, res, pkg) {
t.ifError(err);
t.equal(res, dir + '/xmodules/aaa/index.js');
});
var yopts = {
basedir : dir,
moduleDirectory: 'ymodules'
};
resolve('aaa', yopts, function (err, res, pkg) {
t.ifError(err);
t.equal(res, dir + '/ymodules/aaa/index.js');
});
});
test('moduleDirectory array', function (t) {
t.plan(6);
var dir = __dirname + '/module_dir';
var aopts = {
basedir : dir,
moduleDirectory: [ 'xmodules', 'ymodules', 'zmodules' ]
};
resolve('aaa', aopts, function (err, res, pkg) {
t.ifError(err);
t.equal(res, dir + '/xmodules/aaa/index.js');
});
var bopts = {
basedir : dir,
moduleDirectory: [ 'zmodules', 'ymodules', 'xmodules' ]
};
resolve('aaa', bopts, function (err, res, pkg) {
t.ifError(err);
t.equal(res, dir + '/ymodules/aaa/index.js');
});
var copts = {
basedir : dir,
moduleDirectory: [ 'xmodules', 'ymodules', 'zmodules' ]
};
resolve('bbb', copts, function (err, res, pkg) {
t.ifError(err);
t.equal(res, dir + '/zmodules/bbb/main.js');
});
});

View file

@ -0,0 +1 @@
module.exports = function (x) { return x * 100 }

View file

@ -0,0 +1 @@
module.exports = function (x) { return x + 100 }

View file

@ -0,0 +1 @@
module.exports = function (n) { return n * 111 }

View file

@ -0,0 +1,3 @@
{
"main": "main.js"
}

View file

@ -0,0 +1,48 @@
var path = require('path');
var test = require('tape');
var resolve = require('../');
test('$NODE_PATH', function (t) {
t.plan(4);
resolve('aaa', {
paths: [
__dirname + '/node_path/x',
__dirname + '/node_path/y'
],
basedir: __dirname,
}, function (err, res) {
t.equal(res, __dirname + '/node_path/x/aaa/index.js');
});
resolve('bbb', {
paths: [
__dirname + '/node_path/x',
__dirname + '/node_path/y'
],
basedir: __dirname,
}, function (err, res) {
t.equal(res, __dirname + '/node_path/y/bbb/index.js');
});
resolve('ccc', {
paths: [
__dirname + '/node_path/x',
__dirname + '/node_path/y'
],
basedir: __dirname,
}, function (err, res) {
t.equal(res, __dirname + '/node_path/x/ccc/index.js');
});
// ensure that relative paths still resolve against the
// regular `node_modules` correctly
resolve('tap', {
paths: [
'node_path',
],
basedir: 'node_path/x',
}, function (err, res) {
t.equal(res, path.resolve(__dirname, '..', 'node_modules/tap/lib/main.js'));
});
});

View file

@ -0,0 +1 @@
module.exports = 'A'

View file

@ -0,0 +1 @@
module.exports = 'C'

View file

@ -0,0 +1 @@
module.exports = 'B'

View file

@ -0,0 +1 @@
module.exports = 'CY'

View file

@ -0,0 +1,9 @@
var test = require('tape');
var resolve = require('../');
test('nonstring', function (t) {
t.plan(1);
resolve(555, function (err, res, pkg) {
t.ok(err);
});
});

View file

@ -0,0 +1,35 @@
var test = require('tape');
var resolve = require('../');
test('#62: deep module references and the pathFilter', function(t){
t.plan(9);
var resolverDir = __dirname + '/pathfilter/deep_ref';
var pathFilter = function(pkg, x, remainder){
t.equal(pkg.version, "1.2.3");
t.equal(x, resolverDir + '/node_modules/deep/ref');
t.equal(remainder, "ref");
return "alt";
};
resolve('deep/ref', { basedir : resolverDir }, function (err, res, pkg) {
if (err) t.fail(err);
t.equal(pkg.version, "1.2.3");
t.equal(res, resolverDir + '/node_modules/deep/ref.js');
});
resolve('deep/deeper/ref', { basedir: resolverDir },
function(err, res, pkg) {
if(err) t.fail(err);
t.notEqual(pkg, undefined);
t.equal(pkg.version, "1.2.3");
t.equal(res, resolverDir + '/node_modules/deep/deeper/ref.js');
});
resolve('deep/ref', { basedir : resolverDir, pathFilter : pathFilter },
function (err, res, pkg) {
if (err) t.fail(err);
t.equal(res, resolverDir + '/node_modules/deep/alt.js');
});
});

View file

@ -0,0 +1,4 @@
{
"name": "deep",
"version": "1.2.3"
}

View file

@ -0,0 +1,23 @@
var path = require('path');
var test = require('tape');
var resolve = require('../');
test('precedence', function (t) {
t.plan(3);
var dir = path.join(__dirname, 'precedence/aaa');
resolve('./', { basedir : dir }, function (err, res, pkg) {
t.ifError(err);
t.equal(res, path.join(dir, 'index.js'));
t.equal(pkg.name, 'resolve');
});
});
test('./ should not load ${dir}.js', function (t) {
t.plan(1);
var dir = path.join(__dirname, 'precedence/bbb');
resolve('./', { basedir : dir }, function (err, res, pkg) {
t.ok(err);
});
});

View file

@ -0,0 +1 @@
module.exports = 'wtf'

View file

@ -0,0 +1 @@
module.exports = 'okok'

View file

@ -0,0 +1 @@
console.log(require('./'))

View file

@ -0,0 +1 @@
module.exports '>_<'

View file

@ -0,0 +1 @@
console.log(require('./')); // should throw

View file

@ -0,0 +1,281 @@
var path = require('path');
var test = require('tape');
var resolve = require('../');
test('async foo', function (t) {
t.plan(9);
var dir = __dirname + '/resolver';
resolve('./foo', { basedir : dir }, function (err, res, pkg) {
if (err) t.fail(err);
t.equal(res, dir + '/foo.js');
t.equal(pkg.name, 'resolve');
});
resolve('./foo.js', { basedir : dir }, function (err, res, pkg) {
if (err) t.fail(err);
t.equal(res, dir + '/foo.js');
t.equal(pkg.name, 'resolve');
});
resolve('./foo', { basedir : dir, package: { main: 'resolver' } }, function (err, res, pkg) {
if (err) t.fail(err);
t.equal(res, dir + '/foo.js');
t.equal(pkg.main, 'resolver');
});
resolve('./foo.js', { basedir : dir, package: { main: 'resolver' } }, function (err, res, pkg) {
if (err) t.fail(err);
t.equal(res, dir + '/foo.js');
t.equal(pkg.main, 'resolver');
});
resolve('foo', { basedir : dir }, function (err) {
t.equal(err.message, "Cannot find module 'foo' from '" + path.resolve(dir) + "'");
});
});
test('bar', function (t) {
t.plan(6);
var dir = __dirname + '/resolver';
resolve('foo', { basedir : dir + '/bar' }, function (err, res, pkg) {
if (err) t.fail(err);
t.equal(res, dir + '/bar/node_modules/foo/index.js');
t.equal(pkg, undefined);
});
resolve('foo', { basedir : dir + '/bar' }, function (err, res, pkg) {
if (err) t.fail(err);
t.equal(res, dir + '/bar/node_modules/foo/index.js');
t.equal(pkg, undefined);
});
resolve('foo', { basedir : dir + '/bar', package: { main: 'bar' } }, function (err, res, pkg) {
if (err) t.fail(err);
t.equal(res, dir + '/bar/node_modules/foo/index.js');
t.equal(pkg, undefined);
});
});
test('baz', function (t) {
t.plan(4);
var dir = __dirname + '/resolver';
resolve('./baz', { basedir : dir }, function (err, res, pkg) {
if (err) t.fail(err);
t.equal(res, dir + '/baz/quux.js');
t.equal(pkg.main, 'quux.js');
});
resolve('./baz', { basedir : dir, package: { main: 'resolver' } }, function (err, res, pkg) {
if (err) t.fail(err);
t.equal(res, dir + '/baz/quux.js');
t.equal(pkg.main, 'quux.js');
});
});
test('biz', function (t) {
t.plan(24);
var dir = __dirname + '/resolver/biz/node_modules';
resolve('./grux', { basedir : dir }, function (err, res, pkg) {
if (err) t.fail(err);
t.equal(res, dir + '/grux/index.js');
t.equal(pkg, undefined);
});
resolve('./grux', { basedir : dir, package: { main: 'biz' } }, function (err, res, pkg) {
if (err) t.fail(err);
t.equal(res, dir + '/grux/index.js');
t.equal(pkg.main, 'biz');
});
resolve('./garply', { basedir : dir }, function (err, res, pkg) {
if (err) t.fail(err);
t.equal(res, dir + '/garply/lib/index.js');
t.equal(pkg.main, './lib');
});
resolve('./garply', { basedir : dir, package: { main: 'biz' } }, function (err, res, pkg) {
if (err) t.fail(err);
t.equal(res, dir + '/garply/lib/index.js');
t.equal(pkg.main, './lib');
});
resolve('tiv', { basedir : dir + '/grux' }, function (err, res, pkg) {
if (err) t.fail(err);
t.equal(res, dir + '/tiv/index.js');
t.equal(pkg, undefined);
});
resolve('tiv', { basedir : dir + '/grux', package: { main: 'grux' } }, function (err, res, pkg) {
if (err) t.fail(err);
t.equal(res, dir + '/tiv/index.js');
t.equal(pkg, undefined);
});
resolve('tiv', { basedir : dir + '/garply' }, function (err, res, pkg) {
if (err) t.fail(err);
t.equal(res, dir + '/tiv/index.js');
t.equal(pkg, undefined);
});
resolve('tiv', { basedir : dir + '/garply', package: { main: './lib' } }, function (err, res, pkg) {
if (err) t.fail(err);
t.equal(res, dir + '/tiv/index.js');
t.equal(pkg, undefined);
});
resolve('grux', { basedir : dir + '/tiv' }, function (err, res, pkg) {
if (err) t.fail(err);
t.equal(res, dir + '/grux/index.js');
t.equal(pkg, undefined);
});
resolve('grux', { basedir : dir + '/tiv', package: { main: 'tiv' } }, function (err, res, pkg) {
if (err) t.fail(err);
t.equal(res, dir + '/grux/index.js');
t.equal(pkg, undefined);
});
resolve('garply', { basedir : dir + '/tiv' }, function (err, res, pkg) {
if (err) t.fail(err);
t.equal(res, dir + '/garply/lib/index.js');
t.equal(pkg.main, './lib');
});
resolve('garply', { basedir : dir + '/tiv', package: { main: 'tiv' } }, function (err, res, pkg) {
if (err) t.fail(err);
t.equal(res, dir + '/garply/lib/index.js');
t.equal(pkg.main, './lib');
});
});
test('quux', function (t) {
t.plan(2);
var dir = __dirname + '/resolver/quux';
resolve('./foo', { basedir : dir, package: { main: 'quux' } }, function (err, res, pkg) {
if (err) t.fail(err);
t.equal(res, dir + '/foo/index.js');
t.equal(pkg.main, 'quux');
});
});
test('normalize', function (t) {
t.plan(2);
var dir = __dirname + '/resolver/biz/node_modules/grux';
resolve('../grux', { basedir : dir }, function (err, res, pkg) {
if (err) t.fail(err);
t.equal(res, dir + '/index.js');
t.equal(pkg, undefined);
});
});
test('cup', function (t) {
t.plan(3);
var dir = __dirname + '/resolver';
resolve('./cup', { basedir : dir, extensions : [ '.js', '.coffee' ] },
function (err, res) {
if (err) t.fail(err);
t.equal(res, dir + '/cup.coffee');
});
resolve('./cup.coffee', { basedir : dir }, function (err, res) {
if (err) t.fail(err);
t.equal(res, dir + '/cup.coffee');
});
resolve('./cup', { basedir : dir, extensions : [ '.js' ] },
function (err, res) {
t.equal(err.message, "Cannot find module './cup' from '" + path.resolve(dir) + "'");
});
});
test('mug', function (t) {
t.plan(3);
var dir = __dirname + '/resolver';
resolve('./mug', { basedir : dir }, function (err, res) {
if (err) t.fail(err);
t.equal(res, dir + '/mug.js');
});
resolve('./mug', { basedir : dir, extensions : [ '.coffee', '.js' ] },
function (err, res) {
if (err) t.fail(err);
t.equal(res, dir + '/mug.coffee');
});
resolve('./mug', { basedir : dir, extensions : [ '.js', '.coffee' ] },
function (err, res) {
t.equal(res, dir + '/mug.js');
});
});
test('other path', function (t) {
t.plan(4);
var resolverDir = __dirname + '/resolver';
var dir = resolverDir + '/bar';
var otherDir = resolverDir + '/other_path';
resolve('root', { basedir : dir, paths: [otherDir] }, function (err, res) {
if (err) t.fail(err);
t.equal(res, resolverDir + '/other_path/root.js');
});
resolve('lib/other-lib', { basedir : dir, paths: [otherDir] },
function (err, res) {
if (err) t.fail(err);
t.equal(res, resolverDir + '/other_path/lib/other-lib.js');
});
resolve('root', { basedir : dir, }, function (err, res) {
t.equal(err.message, "Cannot find module 'root' from '" + path.resolve(dir) + "'");
});
resolve('zzz', { basedir : dir, paths: [otherDir] }, function (err, res) {
t.equal(err.message, "Cannot find module 'zzz' from '" + path.resolve(dir) + "'");
});
});
test('incorrect main', function (t) {
t.plan(1)
var resolverDir = __dirname + '/resolver';
var dir = resolverDir + '/incorrect_main';
resolve('./incorrect_main', { basedir : resolverDir }, function (err, res, pkg) {
if (err) t.fail(err);
t.equal(res, dir + '/index.js');
});
});
test('without basedir', function (t) {
t.plan(1);
var dir = __dirname + '/resolver/without_basedir';
var tester = require(dir + '/main.js');
tester(t, function (err, res, pkg){
if (err) {
t.fail(err);
} else {
t.equal(res, dir + '/node_modules/mymodule.js');
}
});
});
test('#25: node modules with the same name as node stdlib modules', function (t) {
t.plan(1);
var resolverDir = __dirname + '/resolver/punycode';
resolve('punycode', { basedir : resolverDir }, function (err, res, pkg) {
if (err) t.fail(err);
t.equal(res, resolverDir + '/node_modules/punycode/index.js');
});
});

View file

@ -0,0 +1 @@
module.exports = 1;

View file

@ -0,0 +1,3 @@
{
"main" : "quux.js"
}

View file

@ -0,0 +1 @@
module.exports = 1;

View file

@ -0,0 +1 @@
module.exports = 'hello garply';

View file

@ -0,0 +1,3 @@
{
"main" : "./lib"
}

View file

@ -0,0 +1 @@
module.exports = require('tiv') * 100;

View file

@ -0,0 +1 @@
module.exports = 3;

View file

@ -0,0 +1 @@

View file

@ -0,0 +1 @@
module.exports = 1;

View file

@ -0,0 +1,2 @@
// this is the actual main file 'index.js', not 'wrong.js' like the package.json would indicate
module.exports = 1;

View file

@ -0,0 +1,3 @@
{
"main" : "wrong.js"
}

View file

@ -0,0 +1 @@
module.exports = 1;

View file

@ -0,0 +1,6 @@
resolve = require('../../../');
module.exports = function(t, cb) {
resolve('mymodule', null, cb);
}

View file

@ -0,0 +1 @@
module.exports = "The tools we use have a profound (and devious!) influence on our thinking habits, and, therefore, on our thinking abilities.- E. Dijkstra"

View file

@ -0,0 +1,180 @@
var test = require('tape');
var resolve = require('../');
test('foo', function (t) {
var dir = __dirname + '/resolver';
t.equal(
resolve.sync('./foo', { basedir : dir }),
dir + '/foo.js'
);
t.equal(
resolve.sync('./foo.js', { basedir : dir }),
dir + '/foo.js'
);
t.throws(function () {
resolve.sync('foo', { basedir : dir });
});
t.end();
});
test('bar', function (t) {
var dir = __dirname + '/resolver';
t.equal(
resolve.sync('foo', { basedir : dir + '/bar' }),
dir + '/bar/node_modules/foo/index.js'
);
t.end();
});
test('baz', function (t) {
var dir = __dirname + '/resolver';
t.equal(
resolve.sync('./baz', { basedir : dir }),
dir + '/baz/quux.js'
);
t.end();
});
test('biz', function (t) {
var dir = __dirname + '/resolver/biz/node_modules';
t.equal(
resolve.sync('./grux', { basedir : dir }),
dir + '/grux/index.js'
);
t.equal(
resolve.sync('tiv', { basedir : dir + '/grux' }),
dir + '/tiv/index.js'
);
t.equal(
resolve.sync('grux', { basedir : dir + '/tiv' }),
dir + '/grux/index.js'
);
t.end();
});
test('normalize', function (t) {
var dir = __dirname + '/resolver/biz/node_modules/grux';
t.equal(
resolve.sync('../grux', { basedir : dir }),
dir + '/index.js'
);
t.end();
});
test('cup', function (t) {
var dir = __dirname + '/resolver';
t.equal(
resolve.sync('./cup', {
basedir : dir,
extensions : [ '.js', '.coffee' ]
}),
dir + '/cup.coffee'
);
t.equal(
resolve.sync('./cup.coffee', {
basedir : dir
}),
dir + '/cup.coffee'
);
t.throws(function () {
resolve.sync('./cup', {
basedir : dir,
extensions : [ '.js' ]
})
});
t.end();
});
test('mug', function (t) {
var dir = __dirname + '/resolver';
t.equal(
resolve.sync('./mug', { basedir : dir }),
dir + '/mug.js'
);
t.equal(
resolve.sync('./mug', {
basedir : dir,
extensions : [ '.coffee', '.js' ]
}),
dir + '/mug.coffee'
);
t.equal(
resolve.sync('./mug', {
basedir : dir,
extensions : [ '.js', '.coffee' ]
}),
dir + '/mug.js'
);
t.end();
});
test('other path', function (t) {
var resolverDir = __dirname + '/resolver';
var dir = resolverDir + '/bar';
var otherDir = resolverDir + '/other_path';
var path = require('path');
t.equal(
resolve.sync('root', {
basedir : dir,
paths: [otherDir] }),
resolverDir + '/other_path/root.js'
);
t.equal(
resolve.sync('lib/other-lib', {
basedir : dir,
paths: [otherDir] }),
resolverDir + '/other_path/lib/other-lib.js'
);
t.throws(function () {
resolve.sync('root', { basedir : dir, });
});
t.throws(function () {
resolve.sync('zzz', {
basedir : dir,
paths: [otherDir] });
});
t.end();
});
test('incorrect main', function (t) {
var resolverDir = __dirname + '/resolver';
var dir = resolverDir + '/incorrect_main';
t.equal(
resolve.sync('./incorrect_main', { basedir : resolverDir }),
dir + '/index.js'
)
t.end()
});
test('#25: node modules with the same name as node stdlib modules', function (t) {
var resolverDir = __dirname + '/resolver/punycode';
t.equal(
resolve.sync('punycode', { basedir : resolverDir }),
resolverDir + '/node_modules/punycode/index.js'
)
t.end()
});

View file

@ -0,0 +1,13 @@
var test = require('tape');
var resolve = require('../');
var path = require('path');
test('subdirs', function (t) {
t.plan(2);
var dir = path.join(__dirname, '/subdirs');
resolve('a/b/c/x.json', { basedir: dir }, function (err, res) {
t.ifError(err);
t.equal(res, path.join(dir, 'node_modules/a/b/c/x.json'));
});
});

View file

@ -0,0 +1 @@
[1,2,3]

View file

@ -0,0 +1 @@
{}