resolve
Advanced tools
Comparing version 1.3.2 to 1.3.3
11
index.js
var core = require('./lib/core'); | ||
exports = module.exports = require('./lib/async'); | ||
exports.core = core; | ||
exports.isCore = function isCore(x) { return core[x]; }; | ||
exports.sync = require('./lib/sync'); | ||
var async = require('./lib/async'); | ||
async.core = core; | ||
async.isCore = function isCore(x) { return core[x]; }; | ||
async.sync = require('./lib/sync'); | ||
exports = async; | ||
module.exports = async; |
@@ -15,3 +15,3 @@ var core = require('./core'); | ||
if (typeof x !== 'string') { | ||
var err = new TypeError('path must be a string'); | ||
var err = new TypeError('Path must be a string.'); | ||
return process.nextTick(function () { | ||
@@ -24,5 +24,7 @@ cb(err); | ||
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()); | ||
if (!err) { | ||
return cb(null, stat.isFile() || stat.isFIFO()); | ||
} | ||
if (err.code === 'ENOENT' || err.code === 'ENOTDIR') return cb(null, false); | ||
return cb(err); | ||
}); | ||
@@ -37,5 +39,5 @@ }; | ||
if (/^(?:\.\.?(?:\/|$)|\/|([A-Za-z]:)?[\\\/])/.test(x)) { | ||
if (/^(?:\.\.?(?:\/|$)|\/|([A-Za-z]:)?[/\\])/.test(x)) { | ||
var res = path.resolve(y, x); | ||
if (x === '..') res += '/'; | ||
if (x === '..' || x.slice(-1) === '/') res += '/'; | ||
if (/\/$/.test(x) && res === y) { | ||
@@ -69,16 +71,18 @@ loadAsDirectory(res, opts.package, onfile); | ||
function loadAsFile(x, pkg, callback) { | ||
function loadAsFile(x, thePackage, callback) { | ||
var loadAsFilePackage = thePackage; | ||
var cb = callback; | ||
if (typeof pkg === 'function') { | ||
cb = pkg; | ||
pkg = undefined; | ||
if (typeof loadAsFilePackage === 'function') { | ||
cb = loadAsFilePackage; | ||
loadAsFilePackage = undefined; | ||
} | ||
var exts = [''].concat(extensions); | ||
load(exts, x, pkg); | ||
load(exts, x, loadAsFilePackage); | ||
function load(exts, x, pkg) { | ||
if (exts.length === 0) return cb(null, undefined, pkg); | ||
function load(exts, x, loadPackage) { | ||
if (exts.length === 0) return cb(null, undefined, loadPackage); | ||
var file = x + exts[0]; | ||
var pkg = loadPackage; | ||
if (pkg) onpkg(null, pkg); | ||
@@ -103,5 +107,5 @@ else loadpkg(path.dirname(file), onpkg); | ||
function onex(err, ex) { | ||
if (err) cb(err); | ||
else if (!ex) load(exts.slice(1), x, pkg); | ||
else cb(null, file, pkg); | ||
if (err) return cb(err); | ||
if (ex) return cb(null, file, pkg); | ||
load(exts.slice(1), x, pkg); | ||
} | ||
@@ -113,6 +117,6 @@ } | ||
if (dir === '' || dir === '/') return cb(null); | ||
if (process.platform === 'win32' && (/^\w:[\\\/]*$/).test(dir)) { | ||
if (process.platform === 'win32' && (/^\w:[/\\]*$/).test(dir)) { | ||
return cb(null); | ||
} | ||
if (/[\\\/]node_modules[\\\/]*$/.test(dir)) return cb(null); | ||
if (/[/\\]node_modules[/\\]*$/.test(dir)) return cb(null); | ||
@@ -136,4 +140,5 @@ var pkgfile = path.join(dir, 'package.json'); | ||
function loadAsDirectory(x, fpkg, callback) { | ||
function loadAsDirectory(x, loadAsDirectoryPackage, callback) { | ||
var cb = callback; | ||
var fpkg = loadAsDirectoryPackage; | ||
if (typeof fpkg === 'function') { | ||
@@ -140,0 +145,0 @@ cb = fpkg; |
@@ -1,2 +0,2 @@ | ||
var current = process.versions && process.versions.node && process.versions.node.split('.') || []; | ||
var current = (process.versions && process.versions.node && process.versions.node.split('.')) || []; | ||
@@ -15,3 +15,3 @@ function versionIncluded(version) { | ||
var core = {}; | ||
for (var version in data) { | ||
for (var version in data) { // eslint-disable-line no-restricted-syntax | ||
if (Object.prototype.hasOwnProperty.call(data, version) && versionIncluded(version)) { | ||
@@ -18,0 +18,0 @@ for (var i = 0; i < data[version].length; ++i) { |
@@ -12,13 +12,13 @@ var path = require('path'); | ||
// resolving against the process' current working directory | ||
start = path.resolve(start); | ||
var absoluteStart = path.resolve(start); | ||
var prefix = '/'; | ||
if (/^([A-Za-z]:)/.test(start)) { | ||
if (/^([A-Za-z]:)/.test(absoluteStart)) { | ||
prefix = ''; | ||
} else if (/^\\\\/.test(start)) { | ||
} else if (/^\\\\/.test(absoluteStart)) { | ||
prefix = '\\\\'; | ||
} | ||
var paths = [start]; | ||
var parsed = parse(start); | ||
var paths = [absoluteStart]; | ||
var parsed = parse(absoluteStart); | ||
while (parsed.dir !== paths[paths.length - 1]) { | ||
@@ -25,0 +25,0 @@ paths.push(parsed.dir); |
@@ -8,2 +8,5 @@ var core = require('./core'); | ||
module.exports = function (x, options) { | ||
if (typeof x !== 'string') { | ||
throw new TypeError('Path must be a string.'); | ||
} | ||
var opts = options || {}; | ||
@@ -14,3 +17,3 @@ var isFile = opts.isFile || function (file) { | ||
} catch (e) { | ||
if (e && e.code === 'ENOENT') return false; | ||
if (e && (e.code === 'ENOENT' || e.code === 'ENOTDIR')) return false; | ||
throw e; | ||
@@ -27,5 +30,5 @@ } | ||
if (/^(?:\.\.?(?:\/|$)|\/|([A-Za-z]:)?[\\\/])/.test(x)) { | ||
if (/^(?:\.\.?(?:\/|$)|\/|([A-Za-z]:)?[/\\])/.test(x)) { | ||
var res = path.resolve(y, x); | ||
if (x === '..') res += '/'; | ||
if (x === '..' || x.slice(-1) === '/') res += '/'; | ||
var m = loadAsFileSync(res) || loadAsDirectorySync(res); | ||
@@ -32,0 +35,0 @@ if (m) return m; |
{ | ||
"name": "resolve", | ||
"description": "resolve like require.resolve() on behalf of files asynchronously and synchronously", | ||
"version": "1.3.2", | ||
"version": "1.3.3", | ||
"repository": { | ||
@@ -18,6 +18,10 @@ "type": "git", | ||
"prepublish": "safe-publish-latest", | ||
"lint": "eslint .", | ||
"tests-only": "tape test/*.js", | ||
"pretest": "npm run lint", | ||
"test": "npm run --silent tests-only" | ||
}, | ||
"devDependencies": { | ||
"@ljharb/eslint-config": "^11.0.0", | ||
"eslint": "^3.19.0", | ||
"object-keys": "^1.0.11", | ||
@@ -24,0 +28,0 @@ "safe-publish-latest": "^1.1.1", |
@@ -20,3 +20,3 @@ var test = require('tape'); | ||
st.doesNotThrow( | ||
function () { require(resolve.core[i]); }, | ||
function () { require(resolve.core[i]); }, // eslint-disable-line no-loop-func | ||
'requiring ' + resolve.core[i] + ' does not throw' | ||
@@ -23,0 +23,0 @@ ); |
@@ -46,5 +46,5 @@ var path = require('path'); | ||
}, function (err, res) { | ||
var root = require('tap/package.json').main; | ||
var root = require('tap/package.json').main; | ||
t.equal(res, path.resolve(__dirname, '..', 'node_modules/tap', root)); | ||
}); | ||
}); |
@@ -9,4 +9,3 @@ var test = require('tape'); | ||
var verifyDirs = function verifyDirs(t, start, dirs, moduleDirectories, paths) { | ||
moduleDirectories = [].concat(moduleDirectories || 'node_modules'); | ||
if (!paths) { paths = []; } | ||
var moduleDirs = [].concat(moduleDirectories || 'node_modules'); | ||
@@ -25,3 +24,3 @@ var foundModuleDirs = {}; | ||
var foundModuleDirNames = keys(foundModuleDirs); | ||
t.deepEqual(foundModuleDirNames, moduleDirectories.concat(paths), 'all desired module dirs were found'); | ||
t.deepEqual(foundModuleDirNames, moduleDirs.concat(paths || []), 'all desired module dirs were found'); | ||
t.equal(keys(uniqueDirs).length, dirs.length, 'all dirs provided were unique'); | ||
@@ -28,0 +27,0 @@ |
@@ -16,3 +16,3 @@ var path = require('path'); | ||
test('./ should not load ${dir}.js', function (t) { | ||
test('./ should not load ${dir}.js', function (t) { // eslint-disable-line no-template-curly-in-string | ||
t.plan(1); | ||
@@ -19,0 +19,0 @@ var dir = path.join(__dirname, 'precedence/bbb'); |
@@ -207,1 +207,48 @@ var path = require('path'); | ||
test('#52 - incorrectly resolves module-paths like "./someFolder/" when there is a file of the same name', function (t) { | ||
var dir = path.join(__dirname, 'resolver'); | ||
t.equal( | ||
resolve.sync('./foo', { basedir: path.join(dir, 'same_names') }), | ||
path.join(dir, 'same_names/foo.js') | ||
); | ||
t.equal( | ||
resolve.sync('./foo/', { basedir: path.join(dir, 'same_names') }), | ||
path.join(dir, 'same_names/foo/index.js') | ||
); | ||
t.end(); | ||
}); | ||
test('sync: #121 - treating an existing file as a dir when no basedir', function (t) { | ||
var testFile = path.basename(__filename); | ||
t.test('sanity check', function (st) { | ||
st.equal( | ||
resolve.sync('./' + testFile), | ||
__filename, | ||
'sanity check' | ||
); | ||
st.end(); | ||
}); | ||
t.test('with a fake directory', function (st) { | ||
function run() { return resolve.sync('./' + testFile + '/blah'); } | ||
st.throws(run, 'throws an error'); | ||
try { | ||
run(); | ||
} catch (e) { | ||
st.equal(e.code, 'MODULE_NOT_FOUND', 'error code matches require.resolve'); | ||
st.equal( | ||
e.message, | ||
'Cannot find module \'./' + testFile + '/blah\' from \'' + __dirname + '\'', | ||
'can not find nonexistent module' | ||
); | ||
} | ||
st.end(); | ||
}); | ||
t.end(); | ||
}); |
@@ -12,3 +12,3 @@ var path = require('path'); | ||
t.equal(res, path.join(dir, 'foo.js')); | ||
t.equal(pkg.name, 'resolve'); | ||
t.equal(pkg && pkg.name, 'resolve'); | ||
}); | ||
@@ -19,3 +19,3 @@ | ||
t.equal(res, path.join(dir, 'foo.js')); | ||
t.equal(pkg.name, 'resolve'); | ||
t.equal(pkg && pkg.name, 'resolve'); | ||
}); | ||
@@ -26,3 +26,3 @@ | ||
t.equal(res, path.join(dir, 'foo.js')); | ||
t.equal(pkg.main, 'resolver'); | ||
t.equal(pkg && pkg.main, 'resolver'); | ||
}); | ||
@@ -284,1 +284,48 @@ | ||
}); | ||
test('#52 - incorrectly resolves module-paths like "./someFolder/" when there is a file of the same name', function (t) { | ||
t.plan(2); | ||
var dir = path.join(__dirname, 'resolver'); | ||
resolve('./foo', { basedir: path.join(dir, 'same_names') }, function (err, res, pkg) { | ||
if (err) t.fail(err); | ||
t.equal(res, path.join(dir, 'same_names/foo.js')); | ||
}); | ||
resolve('./foo/', { basedir: path.join(dir, 'same_names') }, function (err, res, pkg) { | ||
if (err) t.fail(err); | ||
t.equal(res, path.join(dir, 'same_names/foo/index.js')); | ||
}); | ||
}); | ||
test('async: #121 - treating an existing file as a dir when no basedir', function (t) { | ||
var testFile = path.basename(__filename); | ||
t.test('sanity check', function (st) { | ||
st.plan(1); | ||
resolve('./' + testFile, function (err, res, pkg) { | ||
if (err) t.fail(err); | ||
st.equal(res, __filename, 'sanity check'); | ||
}); | ||
}); | ||
t.test('with a fake directory', function (st) { | ||
st.plan(4); | ||
resolve('./' + testFile + '/blah', function (err, res, pkg) { | ||
st.ok(err, 'there is an error'); | ||
st.notOk(res, 'no result'); | ||
st.equal(err && err.code, 'MODULE_NOT_FOUND', 'error code matches require.resolve'); | ||
st.equal( | ||
err && err.message, | ||
'Cannot find module \'./' + testFile + '/blah\' from \'' + __dirname + '\'', | ||
'can not find nonexistent module' | ||
); | ||
st.end(); | ||
}); | ||
}); | ||
t.end(); | ||
}); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
61046
64
1375
6