Comparing version 0.3.1 to 0.4.0
@@ -31,8 +31,8 @@ var core = require('./core'); | ||
if (x.match(/^(?:\.\.?\/|\/|([A-Za-z]:)?\\)/)) { | ||
loadAsFile(path.resolve(y, x), function (err, m) { | ||
loadAsFile(path.resolve(y, x), function (err, m, pkg) { | ||
if (err) cb(err) | ||
else if (m) cb(null, m) | ||
else loadAsDirectory(path.resolve(y, x), function (err, d) { | ||
else if (m) cb(null, m, pkg) | ||
else loadAsDirectory(path.resolve(y, x), function (err, d, pkg) { | ||
if (err) cb(err) | ||
else if (d) cb(null, d) | ||
else if (d) cb(null, d, pkg) | ||
else cb(new Error("Cannot find module '" + x + "'")) | ||
@@ -42,9 +42,14 @@ }) | ||
} | ||
else loadNodeModules(x, y, function (err, n) { | ||
else loadNodeModules(x, y, function (err, n, pkg) { | ||
if (err) cb(err) | ||
else if (n) cb(null, n) | ||
else if (n) cb(null, n, pkg) | ||
else cb(new Error("Cannot find module '" + x + "'")) | ||
}); | ||
function loadAsFile (x, cb) { | ||
function loadAsFile (x, pkg, cb) { | ||
if (typeof pkg === 'function') { | ||
cb = pkg; | ||
pkg = opts.package; | ||
} | ||
(function load (exts) { | ||
@@ -56,3 +61,3 @@ if (exts.length === 0) return cb(null, undefined); | ||
if (err) cb(err) | ||
else if (ex) cb(null, file) | ||
else if (ex) cb(null, file, pkg) | ||
else load(exts.slice(1)) | ||
@@ -63,7 +68,12 @@ }); | ||
function loadAsDirectory (x, cb) { | ||
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'), cb); | ||
if (!ex) return loadAsFile(path.join(x, '/index'), fpkg, cb); | ||
@@ -82,10 +92,10 @@ readFile(pkgfile, function (err, body) { | ||
if (pkg.main) { | ||
loadAsFile(path.resolve(x, pkg.main), function (err, m) { | ||
loadAsFile(path.resolve(x, pkg.main), pkg, function (err, m, pkg) { | ||
if (err) return cb(err); | ||
if (m) return cb(null, m); | ||
if (m) return cb(null, m, pkg); | ||
var dir = path.resolve(x, pkg.main); | ||
loadAsDirectory(dir, function (err, n) { | ||
loadAsDirectory(dir, pkg, function (err, n, pkg) { | ||
if (err) return cb(err); | ||
if (n) return cb(null, n); | ||
loadAsFile(path.join(x, '/index'), cb); | ||
if (n) return cb(null, n, pkg); | ||
loadAsFile(path.join(x, '/index'), pkg, cb); | ||
}); | ||
@@ -96,3 +106,3 @@ }); | ||
loadAsFile(path.join(x, '/index'), cb); | ||
loadAsFile(path.join(x, '/index'), pkg, cb); | ||
}); | ||
@@ -107,8 +117,8 @@ }); | ||
loadAsFile(path.join(dir, '/', x), function (err, m) { | ||
loadAsFile(path.join(dir, '/', x), undefined, function (err, m, pkg) { | ||
if (err) return cb(err); | ||
if (m) return cb(null, m); | ||
loadAsDirectory(path.join(dir, '/', x), function (err, n) { | ||
if (m) return cb(null, m, pkg); | ||
loadAsDirectory(path.join(dir, '/', x), undefined, function (err, n, pkg) { | ||
if (err) return cb(err); | ||
if (n) return cb(null, n); | ||
if (n) return cb(null, n, pkg); | ||
process(dirs.slice(1)); | ||
@@ -115,0 +125,0 @@ }); |
{ | ||
"name" : "resolve", | ||
"description" : "resolve like require.resolve() on behalf of files asynchronously and synchronously", | ||
"version" : "0.3.1", | ||
"version" : "0.4.0", | ||
"repository" : { | ||
@@ -6,0 +6,0 @@ "type" : "git", |
@@ -5,3 +5,3 @@ var test = require('tap').test; | ||
test('filter', function (t) { | ||
t.plan(1); | ||
t.plan(2); | ||
var dir = __dirname + '/resolver'; | ||
@@ -14,6 +14,7 @@ resolve('./baz', { | ||
} | ||
}, function (err, res) { | ||
}, function (err, res, pkg) { | ||
if (err) t.fail(err); | ||
t.equal(res, dir + '/baz/doom.js'); | ||
t.equal(pkg.main, 'doom'); | ||
}); | ||
}); |
@@ -5,3 +5,3 @@ var test = require('tap').test; | ||
test('mock', function (t) { | ||
t.plan(4); | ||
t.plan(6); | ||
@@ -24,10 +24,12 @@ var files = { | ||
resolve('./baz', opts('/foo/bar'), function (err, res) { | ||
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) { | ||
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); | ||
}); | ||
@@ -44,4 +46,45 @@ | ||
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'"); | ||
}); | ||
resolve('../baz', opts('/foo/bar'), function (err, res) { | ||
t.equal(err.message, "Cannot find module '../baz'"); | ||
}); | ||
}); | ||
test('mock package', function (t) { | ||
t.plan(1); | ||
t.plan(2); | ||
@@ -67,6 +110,37 @@ var files = { | ||
resolve('bar', opts('/foo'), function (err, res) { | ||
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'); | ||
}); | ||
}); |
@@ -5,15 +5,29 @@ var test = require('tap').test; | ||
test('async foo', function (t) { | ||
t.plan(3); | ||
t.plan(9); | ||
var dir = __dirname + '/resolver'; | ||
resolve('./foo', { basedir : dir }, function (err, res) { | ||
resolve('./foo', { basedir : dir }, function (err, res, pkg) { | ||
if (err) t.fail(err); | ||
t.equal(res, dir + '/foo.js'); | ||
t.equal(pkg, undefined); | ||
}); | ||
resolve('./foo.js', { basedir : dir }, function (err, res) { | ||
resolve('./foo.js', { basedir : dir }, function (err, res, pkg) { | ||
if (err) t.fail(err); | ||
t.equal(res, dir + '/foo.js'); | ||
t.equal(pkg, undefined); | ||
}); | ||
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) { | ||
@@ -25,53 +39,101 @@ t.equal(err.message, "Cannot find module 'foo'"); | ||
test('bar', function (t) { | ||
t.plan(2); | ||
t.plan(6); | ||
var dir = __dirname + '/resolver'; | ||
resolve('foo', { basedir : dir + '/bar' }, function (err, res) { | ||
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) { | ||
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(1); | ||
t.plan(4); | ||
var dir = __dirname + '/resolver'; | ||
resolve('./baz', { basedir : dir }, function (err, res) { | ||
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(3); | ||
t.plan(12); | ||
var dir = __dirname + '/resolver/biz/node_modules'; | ||
resolve('./grux', { basedir : dir }, function (err, res) { | ||
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('tiv', { basedir : dir + '/grux' }, function (err, res) { | ||
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('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('grux', { basedir : dir + '/tiv' }, function (err, res) { | ||
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('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); | ||
}); | ||
}); | ||
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(1); | ||
t.plan(2); | ||
var dir = __dirname + '/resolver/biz/node_modules/grux'; | ||
resolve('../grux', { basedir : dir }, function (err, res) { | ||
resolve('../grux', { basedir : dir }, function (err, res, pkg) { | ||
if (err) t.fail(err); | ||
t.equal(res, dir + '/index.js'); | ||
t.equal(pkg, undefined); | ||
}); | ||
@@ -78,0 +140,0 @@ }); |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
29872
31
784
137