resolve
Advanced tools
Comparing version 1.11.1 to 1.12.0
@@ -28,2 +28,13 @@ var core = require('./core'); | ||
var maybeUnwrapSymlink = function maybeUnwrapSymlink(x, opts, cb) { | ||
if (opts && opts.preserveSymlinks === false) { | ||
fs.realpath(x, function (realPathErr, realPath) { | ||
if (realPathErr && realPathErr.code !== 'ENOENT') cb(realPathErr); | ||
else cb(null, realPathErr ? x : realPath); | ||
}); | ||
} else { | ||
cb(null, x); | ||
} | ||
}; | ||
module.exports = function resolve(x, options, callback) { | ||
@@ -58,10 +69,10 @@ var cb = callback; | ||
if (opts.preserveSymlinks === false) { | ||
fs.realpath(absoluteStart, function (realPathErr, realStart) { | ||
if (realPathErr && realPathErr.code !== 'ENOENT') cb(err); | ||
else init(realPathErr ? absoluteStart : realStart); | ||
}); | ||
} else { | ||
init(absoluteStart); | ||
} | ||
maybeUnwrapSymlink( | ||
absoluteStart, | ||
opts, | ||
function (err, realStart) { | ||
if (err) cb(err); | ||
else init(realStart); | ||
} | ||
); | ||
@@ -79,4 +90,11 @@ var res; | ||
else if (core[x]) return cb(null, x); | ||
else if (n) cb(null, n, pkg); | ||
else { | ||
else if (n) { | ||
return maybeUnwrapSymlink(n, opts, function (err, realN) { | ||
if (err) { | ||
cb(err); | ||
} else { | ||
cb(null, realN, pkg); | ||
} | ||
}); | ||
} else { | ||
var moduleError = new Error("Cannot find module '" + x + "' from '" + parent + "'"); | ||
@@ -94,4 +112,11 @@ moduleError.code = 'MODULE_NOT_FOUND'; | ||
if (err) cb(err); | ||
else if (d) cb(null, d, pkg); | ||
else { | ||
else if (d) { | ||
maybeUnwrapSymlink(d, opts, function (err, realD) { | ||
if (err) { | ||
cb(err); | ||
} else { | ||
cb(null, realD, pkg); | ||
} | ||
}); | ||
} else { | ||
var moduleError = new Error("Cannot find module '" + x + "' from '" + parent + "'"); | ||
@@ -98,0 +123,0 @@ moduleError.code = 'MODULE_NOT_FOUND'; |
@@ -11,2 +11,3 @@ { | ||
"crypto": true, | ||
"_debug_agent": ">= 1 && < 8", | ||
"_debugger": "< 8", | ||
@@ -13,0 +14,0 @@ "dgram": true, |
@@ -28,2 +28,15 @@ var core = require('./core'); | ||
var maybeUnwrapSymlink = function maybeUnwrapSymlink(x, opts) { | ||
if (opts && opts.preserveSymlinks === false) { | ||
try { | ||
return fs.realpathSync(x); | ||
} catch (realPathErr) { | ||
if (realPathErr.code !== 'ENOENT') { | ||
throw realPathErr; | ||
} | ||
} | ||
} | ||
return x; | ||
}; | ||
module.exports = function (x, options) { | ||
@@ -46,14 +59,4 @@ if (typeof x !== 'string') { | ||
// ensure that `basedir` is an absolute path at this point, resolving against the process' current working directory | ||
var absoluteStart = path.resolve(basedir); | ||
var absoluteStart = maybeUnwrapSymlink(path.resolve(basedir), opts); | ||
if (opts.preserveSymlinks === false) { | ||
try { | ||
absoluteStart = fs.realpathSync(absoluteStart); | ||
} catch (realPathErr) { | ||
if (realPathErr.code !== 'ENOENT') { | ||
throw realPathErr; | ||
} | ||
} | ||
} | ||
if ((/^(?:\.\.?(?:\/|$)|\/|([A-Za-z]:)?[/\\])/).test(x)) { | ||
@@ -63,3 +66,3 @@ var res = path.resolve(absoluteStart, x); | ||
var m = loadAsFileSync(res) || loadAsDirectorySync(res); | ||
if (m) return m; | ||
if (m) return maybeUnwrapSymlink(m, opts); | ||
} else if (core[x]) { | ||
@@ -69,3 +72,3 @@ return x; | ||
var n = loadNodeModulesSync(x, absoluteStart); | ||
if (n) return n; | ||
if (n) return maybeUnwrapSymlink(n, opts); | ||
} | ||
@@ -72,0 +75,0 @@ |
{ | ||
"name": "resolve", | ||
"description": "resolve like require.resolve() on behalf of files asynchronously and synchronously", | ||
"version": "1.11.1", | ||
"version": "1.12.0", | ||
"repository": { | ||
@@ -19,2 +19,3 @@ "type": "git", | ||
"lint": "eslint .", | ||
"pretests-only": "cd ./test/resolver/nested_symlinks && node mylib/sync && node mylib/async", | ||
"tests-only": "tape test/*.js", | ||
@@ -32,3 +33,3 @@ "pretest": "npm run lint", | ||
"tap": "0.4.13", | ||
"tape": "^4.10.1" | ||
"tape": "^4.11.0" | ||
}, | ||
@@ -35,0 +36,0 @@ "license": "MIT", |
@@ -7,2 +7,3 @@ var path = require('path'); | ||
var symlinkDir = path.join(__dirname, 'resolver', 'symlinked', 'symlink'); | ||
var packageDir = path.join(__dirname, 'resolver', 'symlinked', '_', 'node_modules', 'package'); | ||
try { | ||
@@ -12,2 +13,5 @@ fs.unlinkSync(symlinkDir); | ||
try { | ||
fs.unlinkSync(packageDir); | ||
} catch (err) {} | ||
try { | ||
fs.symlinkSync('./_/symlink_target', symlinkDir, 'dir'); | ||
@@ -18,2 +22,8 @@ } catch (err) { | ||
} | ||
try { | ||
fs.symlinkSync('../../package', packageDir, 'dir'); | ||
} catch (err) { | ||
// if fails then it is probably on Windows and lets try to create a junction | ||
fs.symlinkSync(path.join(__dirname, '..', '..', 'package') + '\\', packageDir, 'junction'); | ||
} | ||
@@ -60,1 +70,19 @@ test('symlink', function (t) { | ||
}); | ||
test('sync symlink from node_modules to other dir when preserveSymlinks = false', function (t) { | ||
var basedir = path.join(__dirname, 'resolver', 'symlinked', '_'); | ||
var fn = resolve.sync('package', { basedir: basedir, preserveSymlinks: false }); | ||
t.equal(fn, path.resolve(__dirname, 'resolver/symlinked/package/bar.js')); | ||
t.end(); | ||
}); | ||
test('async symlink from node_modules to other dir when preserveSymlinks = false', function (t) { | ||
t.plan(2); | ||
var basedir = path.join(__dirname, 'resolver', 'symlinked', '_'); | ||
resolve('package', { basedir: basedir, preserveSymlinks: false }, function (err, result) { | ||
t.notOk(err, 'no error'); | ||
t.equal(result, path.resolve(__dirname, 'resolver/symlinked/package/bar.js')); | ||
t.end(); | ||
}); | ||
}); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Mixed license
License(Experimental) Package contains multiple licenses.
Found 1 instance in 1 package
90
2095
99502
1