Skip to content

Commit 2dcfba4

Browse files
committed
[Refactor] make maybeUnwrapSymlink
1 parent a136689 commit 2dcfba4

File tree

2 files changed

+33
-19
lines changed

2 files changed

+33
-19
lines changed

lib/async.js

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,17 @@ var defaultIsDir = function isDirectory(dir, cb) {
2525
});
2626
};
2727

28+
var maybeUnwrapSymlink = function maybeUnwrapSymlink(x, opts, cb) {
29+
if (!opts || !opts.preserveSymlinks) {
30+
fs.realpath(x, function (realPathErr, realPath) {
31+
if (realPathErr && realPathErr.code !== 'ENOENT') cb(realPathErr);
32+
else cb(null, realPathErr ? x : realPath);
33+
});
34+
} else {
35+
cb(null, x);
36+
}
37+
};
38+
2839
module.exports = function resolve(x, options, callback) {
2940
var cb = callback;
3041
var opts = options;
@@ -54,14 +65,14 @@ module.exports = function resolve(x, options, callback) {
5465
// ensure that `basedir` is an absolute path at this point, resolving against the process' current working directory
5566
var absoluteStart = path.resolve(basedir);
5667

57-
if (!opts || !opts.preserveSymlinks) {
58-
fs.realpath(absoluteStart, function (realPathErr, realStart) {
59-
if (realPathErr && realPathErr.code !== 'ENOENT') cb(err);
60-
else validateBasedir(realPathErr ? absoluteStart : realStart);
61-
});
62-
} else {
63-
validateBasedir(absoluteStart);
64-
}
68+
maybeUnwrapSymlink(
69+
absoluteStart,
70+
opts,
71+
function (err, realStart) {
72+
if (err) cb(err);
73+
else validateBasedir(realStart);
74+
}
75+
);
6576

6677
function validateBasedir(basedir) {
6778
if (opts.basedir) {

lib/sync.js

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,19 @@ var defaultIsDir = function isDirectory(dir) {
2525
return stat.isDirectory();
2626
};
2727

28+
var maybeUnwrapSymlink = function maybeUnwrapSymlink(x, opts) {
29+
if (!opts || !opts.preserveSymlinks) {
30+
try {
31+
return fs.realpathSync(x);
32+
} catch (realPathErr) {
33+
if (realPathErr.code !== 'ENOENT') {
34+
throw realPathErr;
35+
}
36+
}
37+
}
38+
return x;
39+
};
40+
2841
module.exports = function (x, options) {
2942
if (typeof x !== 'string') {
3043
throw new TypeError('Path must be a string.');
@@ -42,17 +55,7 @@ module.exports = function (x, options) {
4255
opts.paths = opts.paths || [];
4356

4457
// ensure that `basedir` is an absolute path at this point, resolving against the process' current working directory
45-
var absoluteStart = path.resolve(basedir);
46-
47-
if (!opts.preserveSymlinks) {
48-
try {
49-
absoluteStart = fs.realpathSync(absoluteStart);
50-
} catch (realPathErr) {
51-
if (realPathErr.code !== 'ENOENT') {
52-
throw realPathErr;
53-
}
54-
}
55-
}
58+
var absoluteStart = maybeUnwrapSymlink(path.resolve(basedir), opts);
5659

5760
if (opts.basedir && !isDirectory(absoluteStart)) {
5861
var dirError = new TypeError('Provided basedir "' + opts.basedir + '" is not a directory' + (opts.preserveSymlinks ? '' : ', or a symlink to a directory'));

0 commit comments

Comments
 (0)