Comparing version 0.2.8 to 0.3.0
112
index.js
@@ -1,109 +0,5 @@ | ||
var fs = require('fs'); | ||
var path = require('path'); | ||
var existsSync = fs.existsSync || path.existsSync; | ||
// taken from `ls -1 lib` in node 0.6.11 | ||
var core = exports.core = [ | ||
'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' | ||
].reduce(function (acc, x) { acc[x] = true; return acc }, {}); | ||
var core = require('./lib/core'); | ||
exports = module.exports = require('./lib/async'); | ||
exports.core = core; | ||
exports.isCore = function (x) { return core[x] }; | ||
exports.sync = function (x, opts) { | ||
if (core[x]) return x; | ||
if (!opts) opts = {}; | ||
var isFile = opts.isFile || function (file) { | ||
return existsSync(file) && fs.statSync(file).isFile() | ||
}; | ||
var readFileSync = opts.readFileSync || fs.readFileSync; | ||
var extensions = opts.extensions || [ '.js' ]; | ||
var y = opts.basedir | ||
|| path.dirname(require.cache[__filename].parent.filename) | ||
; | ||
opts.paths = opts.paths || []; | ||
if (x.match(/^(?:\.\.?\/|\/|([A-Za-z]:)?\\)/)) { | ||
var m = loadAsFileSync(path.resolve(y, x)) | ||
|| loadAsDirectorySync(path.resolve(y, x)); | ||
if (m) return m; | ||
} else { | ||
var n = loadNodeModulesSync(x, y); | ||
if (n) return n; | ||
} | ||
throw new Error("Cannot find module '" + x + "'"); | ||
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 = nodeModulesPathsSync(start); | ||
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; | ||
} | ||
} | ||
function nodeModulesPathsSync (start) { | ||
var splitRe = process.platform === 'win32' ? /[\/\\]/ : /\/+/; | ||
var parts = start.split(splitRe); | ||
var dirs = []; | ||
for (var i = parts.length - 1; i >= 0; i--) { | ||
if (parts[i] === 'node_modules') continue; | ||
var dir = path.join( | ||
path.join.apply(path, parts.slice(0, i + 1)), | ||
'node_modules' | ||
); | ||
if (!parts[0].match(/([A-Za-z]:)/)) { | ||
dir = '/' + dir; | ||
} | ||
dirs.push(dir); | ||
} | ||
return dirs.concat(opts.paths); | ||
} | ||
}; | ||
exports.sync = require('./lib/sync'); |
{ | ||
"name" : "resolve", | ||
"description" : "A more hookable require.resolve() implementation", | ||
"version" : "0.2.8", | ||
"description" : "resolve like require.resolve() on behalf of files asynchronously and synchronously", | ||
"version" : "0.3.0", | ||
"repository" : { | ||
@@ -16,17 +16,8 @@ "type" : "git", | ||
], | ||
"directories" : { | ||
"lib" : ".", | ||
"example" : "example", | ||
"test" : "test" | ||
}, | ||
"scripts" : { | ||
"test" : "tap test/*.js" | ||
}, | ||
"dependencies" : {}, | ||
"devDependencies" : { | ||
"tap" : "~0.2.4" | ||
"tap" : "~0.4.0" | ||
}, | ||
"engines" : { | ||
"node" : ">=0.4.0" | ||
}, | ||
"license" : "MIT", | ||
@@ -33,0 +24,0 @@ "author" : { |
@@ -5,12 +5,14 @@ var test = require('tap').test; | ||
test('filter', function (t) { | ||
t.plan(1); | ||
var dir = __dirname + '/resolver'; | ||
var res = resolve.sync('./baz', { | ||
resolve('./baz', { | ||
basedir : dir, | ||
packageFilter : function (pkg) { | ||
pkg.main = 'doom' | ||
pkg.main = 'doom'; | ||
return pkg; | ||
} | ||
}, function (err, res) { | ||
if (err) t.fail(err); | ||
t.equal(res, dir + '/baz/doom.js'); | ||
}); | ||
t.equal(res, dir + '/baz/doom.js'); | ||
t.end(); | ||
}); |
@@ -14,7 +14,7 @@ var test = require('tap').test; | ||
basedir : basedir, | ||
isFile : function (file) { | ||
return files.hasOwnProperty(file) | ||
isFile : function (file, cb) { | ||
cb(null, files.hasOwnProperty(file)); | ||
}, | ||
readFileSync : function (file) { | ||
return files[file] | ||
readFile : function (file, cb) { | ||
cb(null, files[file]); | ||
} | ||
@@ -24,18 +24,18 @@ } | ||
t.equal( | ||
resolve.sync('./baz', opts('/foo/bar')), | ||
'/foo/bar/baz.js' | ||
); | ||
resolve('./baz', opts('/foo/bar'), function (err, res) { | ||
if (err) t.fail(err); | ||
t.equal(res, '/foo/bar/baz.js'); | ||
}); | ||
t.equal( | ||
resolve.sync('./baz.js', opts('/foo/bar')), | ||
'/foo/bar/baz.js' | ||
); | ||
resolve('./baz.js', opts('/foo/bar'), function (err, res) { | ||
if (err) t.fail(err); | ||
t.equal(res, '/foo/bar/baz.js'); | ||
}); | ||
t.throws(function () { | ||
resolve.sync('baz', opts('/foo/bar')); | ||
resolve('baz', opts('/foo/bar'), function (err, res) { | ||
t.equal(err.message, "Cannot find module 'baz'"); | ||
}); | ||
t.throws(function () { | ||
resolve.sync('../baz', opts('/foo/bar')); | ||
resolve('../baz', opts('/foo/bar'), function (err, res) { | ||
t.equal(err.message, "Cannot find module '../baz'"); | ||
}); | ||
@@ -57,7 +57,7 @@ }); | ||
basedir : basedir, | ||
isFile : function (file) { | ||
return files.hasOwnProperty(file) | ||
isFile : function (file, cb) { | ||
cb(null, files.hasOwnProperty(file)); | ||
}, | ||
readFileSync : function (file) { | ||
return files[file] | ||
readFile : function (file, cb) { | ||
cb(null, files[file]); | ||
} | ||
@@ -67,6 +67,6 @@ } | ||
t.equal( | ||
resolve.sync('bar', opts('/foo')), | ||
'/foo/node_modules/bar/baz.js' | ||
); | ||
resolve('bar', opts('/foo'), function (err, res) { | ||
if (err) t.fail(err); | ||
t.equal(res, '/foo/node_modules/bar/baz.js'); | ||
}); | ||
}); |
var test = require('tap').test; | ||
var resolve = require('../'); | ||
test('foo', function (t) { | ||
test('async foo', function (t) { | ||
t.plan(3); | ||
var dir = __dirname + '/resolver'; | ||
t.equal( | ||
resolve.sync('./foo', { basedir : dir }), | ||
dir + '/foo.js' | ||
); | ||
resolve('./foo', { basedir : dir }, function (err, res) { | ||
if (err) t.fail(err); | ||
t.equal(res, dir + '/foo.js'); | ||
}); | ||
t.equal( | ||
resolve.sync('./foo.js', { basedir : dir }), | ||
dir + '/foo.js' | ||
); | ||
resolve('./foo.js', { basedir : dir }, function (err, res) { | ||
if (err) t.fail(err); | ||
t.equal(res, dir + '/foo.js'); | ||
}); | ||
t.throws(function () { | ||
resolve.sync('foo', { basedir : dir }); | ||
resolve('foo', { basedir : dir }, function (err) { | ||
t.equal(err.message, "Cannot find module 'foo'"); | ||
}); | ||
t.end(); | ||
}); | ||
test('bar', function (t) { | ||
t.plan(2); | ||
var dir = __dirname + '/resolver'; | ||
t.equal( | ||
resolve.sync('foo', { basedir : dir + '/bar' }), | ||
dir + '/bar/node_modules/foo/index.js' | ||
); | ||
t.end(); | ||
resolve('foo', { basedir : dir + '/bar' }, function (err, res) { | ||
if (err) t.fail(err); | ||
t.equal(res, dir + '/bar/node_modules/foo/index.js'); | ||
}); | ||
resolve('foo', { basedir : dir + '/bar' }, function (err, res) { | ||
if (err) t.fail(err); | ||
t.equal(res, dir + '/bar/node_modules/foo/index.js'); | ||
}); | ||
}); | ||
test('baz', function (t) { | ||
t.plan(1); | ||
var dir = __dirname + '/resolver'; | ||
t.equal( | ||
resolve.sync('./baz', { basedir : dir }), | ||
dir + '/baz/quux.js' | ||
); | ||
t.end(); | ||
resolve('./baz', { basedir : dir }, function (err, res) { | ||
if (err) t.fail(err); | ||
t.equal(res, dir + '/baz/quux.js'); | ||
}); | ||
}); | ||
test('biz', function (t) { | ||
t.plan(3); | ||
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' | ||
); | ||
resolve('./grux', { basedir : dir }, function (err, res) { | ||
if (err) t.fail(err); | ||
t.equal(res, dir + '/grux/index.js'); | ||
}); | ||
t.equal( | ||
resolve.sync('grux', { basedir : dir + '/tiv' }), | ||
dir + '/grux/index.js' | ||
); | ||
t.end(); | ||
resolve('tiv', { basedir : dir + '/grux' }, function (err, res) { | ||
if (err) t.fail(err); | ||
t.equal(res, dir + '/tiv/index.js'); | ||
}); | ||
resolve('grux', { basedir : dir + '/tiv' }, function (err, res) { | ||
if (err) t.fail(err); | ||
t.equal(res, dir + '/grux/index.js'); | ||
}); | ||
}); | ||
test('normalize', function (t) { | ||
t.plan(1); | ||
var dir = __dirname + '/resolver/biz/node_modules/grux'; | ||
t.equal( | ||
resolve.sync('../grux', { basedir : dir }), | ||
dir + '/index.js' | ||
); | ||
t.end(); | ||
resolve('../grux', { basedir : dir }, function (err, res) { | ||
if (err) t.fail(err); | ||
t.equal(res, dir + '/index.js'); | ||
}); | ||
}); | ||
test('cup', function (t) { | ||
t.plan(3); | ||
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' | ||
); | ||
resolve('./cup', { basedir : dir, extensions : [ '.js', '.coffee' ] }, | ||
function (err, res) { | ||
if (err) t.fail(err); | ||
t.equal(res, dir + '/cup.coffee'); | ||
}); | ||
t.throws(function () { | ||
resolve.sync('./cup', { | ||
basedir : dir, | ||
extensions : [ '.js' ] | ||
}) | ||
resolve('./cup.coffee', { basedir : dir }, function (err, res) { | ||
if (err) t.fail(err); | ||
t.equal(res, dir + '/cup.coffee'); | ||
}); | ||
t.end(); | ||
resolve('./cup', { basedir : dir, extensions : [ '.js' ] }, | ||
function (err, res) { | ||
t.equal(err.message, "Cannot find module './cup'"); | ||
}); | ||
}); | ||
test('mug', function (t) { | ||
t.plan(3); | ||
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' | ||
); | ||
resolve('./mug', { basedir : dir }, function (err, res) { | ||
if (err) t.fail(err); | ||
t.equal(res, dir + '/mug.js'); | ||
}); | ||
t.equal( | ||
resolve.sync('./mug', { | ||
basedir : dir, | ||
extensions : [ '.js', '.coffee' ] | ||
}), | ||
dir + '/mug.js' | ||
); | ||
resolve('./mug', { basedir : dir, extensions : [ '.coffee', '.js' ] }, | ||
function (err, res) { | ||
if (err) t.fail(err); | ||
t.equal(res, dir + '/mug.coffee'); | ||
}); | ||
t.end(); | ||
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'; | ||
var path = require('path'); | ||
t.equal( | ||
resolve.sync('root', { | ||
basedir : dir, | ||
paths: [otherDir] }), | ||
resolverDir + '/other_path/root.js' | ||
); | ||
resolve('root', { basedir : dir, paths: [otherDir] }, function (err, res) { | ||
if (err) t.fail(err); | ||
t.equal(res, 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, }); | ||
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'); | ||
}); | ||
t.throws(function () { | ||
resolve.sync('zzz', { | ||
basedir : dir, | ||
paths: [otherDir] }); | ||
resolve('root', { basedir : dir, }, function (err, res) { | ||
t.equal(err.message, "Cannot find module 'root'"); | ||
}); | ||
t.end(); | ||
resolve('zzz', { basedir : dir, paths: [otherDir] }, function (err, res) { | ||
t.equal(err.message, "Cannot find module 'zzz'"); | ||
}); | ||
}); |
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
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
25071
30
657
135
2
1