Skip to content

Commit a01ddfb

Browse files
committed
Merge tag 'v1.12.2'
2 parents f4081ef + d930f86 commit a01ddfb

File tree

8 files changed

+145
-53
lines changed

8 files changed

+145
-53
lines changed

lib/async.js

Lines changed: 51 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -185,19 +185,22 @@ module.exports = function resolve(x, options, callback) {
185185
}
186186
if ((/[/\\]node_modules[/\\]*$/).test(dir)) return cb(null);
187187

188-
var pkgfile = path.join(dir, 'package.json');
189-
isFile(pkgfile, function (err, ex) {
190-
// on err, ex is false
191-
if (!ex) return loadpkg(path.dirname(dir), cb);
192-
193-
readFile(pkgfile, function (err, body) {
194-
if (err) cb(err);
195-
try { var pkg = JSON.parse(body); } catch (jsonErr) {}
196-
197-
if (pkg && opts.packageFilter) {
198-
pkg = opts.packageFilter(pkg, pkgfile, dir);
199-
}
200-
cb(null, pkg, dir);
188+
maybeUnwrapSymlink(dir, opts, function (unwrapErr, pkgdir) {
189+
if (unwrapErr) return loadpkg(path.dirname(dir), cb);
190+
var pkgfile = path.join(pkgdir, 'package.json');
191+
isFile(pkgfile, function (err, ex) {
192+
// on err, ex is false
193+
if (!ex) return loadpkg(path.dirname(dir), cb);
194+
195+
readFile(pkgfile, function (err, body) {
196+
if (err) cb(err);
197+
try { var pkg = JSON.parse(body); } catch (jsonErr) {}
198+
199+
if (pkg && opts.packageFilter) {
200+
pkg = opts.packageFilter(pkg, pkgfile, dir);
201+
}
202+
cb(null, pkg, dir);
203+
});
201204
});
202205
});
203206
}
@@ -210,46 +213,49 @@ module.exports = function resolve(x, options, callback) {
210213
fpkg = opts.package;
211214
}
212215

213-
var pkgfile = path.join(x, 'package.json');
214-
isFile(pkgfile, function (err, ex) {
215-
if (err) return cb(err);
216-
if (!ex) return loadAsFile(path.join(x, 'index'), fpkg, cb);
217-
218-
readFile(pkgfile, function (err, body) {
216+
maybeUnwrapSymlink(x, opts, function (unwrapErr, pkgdir) {
217+
if (unwrapErr) return loadAsDirectory(path.dirname(x), fpkg, cb);
218+
var pkgfile = path.join(pkgdir, 'package.json');
219+
isFile(pkgfile, function (err, ex) {
219220
if (err) return cb(err);
220-
try {
221-
var pkg = JSON.parse(body);
222-
} catch (jsonErr) {}
221+
if (!ex) return loadAsFile(path.join(x, 'index'), fpkg, cb);
223222

224-
if (opts.packageFilter) {
225-
pkg = opts.packageFilter(pkg, pkgfile, x);
226-
}
223+
readFile(pkgfile, function (err, body) {
224+
if (err) return cb(err);
225+
try {
226+
var pkg = JSON.parse(body);
227+
} catch (jsonErr) {}
227228

228-
if (pkg.main) {
229-
if (typeof pkg.main !== 'string') {
230-
var mainError = new TypeError('package “' + pkg.name + '” `main` must be a string');
231-
mainError.code = 'INVALID_PACKAGE_MAIN';
232-
return cb(mainError);
233-
}
234-
if (pkg.main === '.' || pkg.main === './') {
235-
pkg.main = 'index';
229+
if (pkg && opts.packageFilter) {
230+
pkg = opts.packageFilter(pkg, pkgfile, pkgdir);
236231
}
237-
loadAsFile(path.resolve(x, pkg.main), pkg, function (err, m, pkg) {
238-
if (err) return cb(err);
239-
if (m) return cb(null, m, pkg);
240-
if (!pkg) return loadAsFile(path.join(x, 'index'), pkg, cb);
241232

242-
var dir = path.resolve(x, pkg.main);
243-
loadAsDirectory(dir, pkg, function (err, n, pkg) {
233+
if (pkg && pkg.main) {
234+
if (typeof pkg.main !== 'string') {
235+
var mainError = new TypeError('package “' + pkg.name + '” `main` must be a string');
236+
mainError.code = 'INVALID_PACKAGE_MAIN';
237+
return cb(mainError);
238+
}
239+
if (pkg.main === '.' || pkg.main === './') {
240+
pkg.main = 'index';
241+
}
242+
loadAsFile(path.resolve(x, pkg.main), pkg, function (err, m, pkg) {
244243
if (err) return cb(err);
245-
if (n) return cb(null, n, pkg);
246-
loadAsFile(path.join(x, 'index'), pkg, cb);
244+
if (m) return cb(null, m, pkg);
245+
if (!pkg) return loadAsFile(path.join(x, 'index'), pkg, cb);
246+
247+
var dir = path.resolve(x, pkg.main);
248+
loadAsDirectory(dir, pkg, function (err, n, pkg) {
249+
if (err) return cb(err);
250+
if (n) return cb(null, n, pkg);
251+
loadAsFile(path.join(x, 'index'), pkg, cb);
252+
});
247253
});
248-
});
249-
return;
250-
}
254+
return;
255+
}
251256

252-
loadAsFile(path.join(x, '/index'), pkg, cb);
257+
loadAsFile(path.join(x, '/index'), pkg, cb);
258+
});
253259
});
254260
});
255261
}

lib/sync.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ module.exports = function (x, options) {
111111
}
112112
if ((/[/\\]node_modules[/\\]*$/).test(dir)) return;
113113

114-
var pkgfile = path.join(dir, 'package.json');
114+
var pkgfile = path.join(isDirectory(dir) ? maybeUnwrapSymlink(dir, opts) : dir, 'package.json');
115115

116116
if (!isFile(pkgfile)) {
117117
return loadpkg(path.dirname(dir));
@@ -131,18 +131,18 @@ module.exports = function (x, options) {
131131
}
132132

133133
function loadAsDirectorySync(x) {
134-
var pkgfile = path.join(x, '/package.json');
134+
var pkgfile = path.join(isDirectory(x) ? maybeUnwrapSymlink(x, opts) : x, '/package.json');
135135
if (isFile(pkgfile)) {
136136
try {
137137
var body = readFileSync(pkgfile, 'UTF8');
138138
var pkg = JSON.parse(body);
139139
} catch (e) {}
140140

141-
if (opts.packageFilter) {
141+
if (pkg && opts.packageFilter) {
142142
pkg = opts.packageFilter(pkg, pkgfile, x);
143143
}
144144

145-
if (pkg.main) {
145+
if (pkg && pkg.main) {
146146
if (typeof pkg.main !== 'string') {
147147
var mainError = new TypeError('package “' + pkg.name + '” `main` must be a string');
148148
mainError.code = 'INVALID_PACKAGE_MAIN';

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "resolve",
33
"description": "resolve like require.resolve() on behalf of files asynchronously and synchronously",
4-
"version": "1.12.0",
4+
"version": "1.12.2",
55
"repository": {
66
"type": "git",
77
"url": "git://github.com/browserify/resolve.git"
@@ -25,6 +25,7 @@
2525
},
2626
"devDependencies": {
2727
"@ljharb/eslint-config": "^15.0.2",
28+
"array.prototype.map": "^1.0.1",
2829
"eslint": "^6.6.0",
2930
"object-keys": "^1.1.1",
3031
"safe-publish-latest": "^1.1.4",

test/filter_sync.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ test('filter', function (t) {
2626
'second packageFilter argument is "pkgfile"'
2727
);
2828

29-
var packageFileDir = packageFilterArgs[2];
30-
t.equal(packageFileDir, path.join(dir, 'baz'), 'third packageFilter argument is "dir"');
29+
var packageDir = packageFilterArgs[2];
30+
t.equal(packageDir, path.join(dir, 'baz'), 'third packageFilter argument is "dir"');
3131

3232
t.end();
3333
});

test/symlinks.js

Lines changed: 80 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,26 @@
11
var path = require('path');
22
var fs = require('fs');
33
var test = require('tape');
4+
var map = require('array.prototype.map');
45
var resolve = require('../');
56

67
var symlinkDir = path.join(__dirname, 'resolver', 'symlinked', 'symlink');
78
var packageDir = path.join(__dirname, 'resolver', 'symlinked', '_', 'node_modules', 'package');
9+
var modADir = path.join(__dirname, 'symlinks', 'source', 'node_modules', 'mod-a');
10+
var symlinkModADir = path.join(__dirname, 'symlinks', 'dest', 'node_modules', 'mod-a');
811
try {
912
fs.unlinkSync(symlinkDir);
1013
} catch (err) {}
1114
try {
1215
fs.unlinkSync(packageDir);
1316
} catch (err) {}
17+
try {
18+
fs.unlinkSync(modADir);
19+
} catch (err) {}
20+
try {
21+
fs.unlinkSync(symlinkModADir);
22+
} catch (err) {}
23+
1424
try {
1525
fs.symlinkSync('./_/symlink_target', symlinkDir, 'dir');
1626
} catch (err) {
@@ -25,6 +35,12 @@ try {
2535
// if fails then it is probably on Windows and lets try to create a junction
2636
fs.symlinkSync(path.join(__dirname, '..', '..', 'package') + '\\', packageDir, 'junction');
2737
}
38+
try {
39+
fs.symlinkSync('../../source/node_modules/mod-a', symlinkModADir, 'dir');
40+
} catch (err) {
41+
// if fails then it is probably on Windows and lets try to create a junction
42+
fs.symlinkSync(path.join(__dirname, '..', '..', 'source', 'node_modules', 'mod-a') + '\\', symlinkModADir, 'junction');
43+
}
2844

2945
test('symlink', function (t) {
3046
t.plan(2);
@@ -81,6 +97,69 @@ test('async symlink from node_modules to other dir when preserveSymlinks = false
8197
resolve('package', { basedir: basedir, preserveSymlinks: false }, function (err, result) {
8298
t.notOk(err, 'no error');
8399
t.equal(result, path.resolve(__dirname, 'resolver/symlinked/package/bar.js'));
84-
t.end();
85100
});
86101
});
102+
103+
test('packageFilter', function (t) {
104+
function testPackageFilter(preserveSymlinks) {
105+
return function (st) {
106+
st.plan(5);
107+
108+
var destMain = 'symlinks/dest/node_modules/mod-a/index.js';
109+
var destPkg = 'symlinks/dest/node_modules/mod-a/package.json';
110+
var sourceMain = 'symlinks/source/node_modules/mod-a/index.js';
111+
var sourcePkg = 'symlinks/source/node_modules/mod-a/package.json';
112+
var destDir = path.join(__dirname, 'symlinks', 'dest');
113+
114+
var packageFilterPath = [];
115+
var actualPath = resolve.sync('mod-a', {
116+
basedir: destDir,
117+
preserveSymlinks: preserveSymlinks,
118+
packageFilter: function (pkg, pkgfile) {
119+
packageFilterPath.push(pkgfile);
120+
}
121+
});
122+
st.equal(
123+
actualPath.replace(__dirname + '/', ''),
124+
preserveSymlinks ? destMain : sourceMain,
125+
'sync: actual path is correct'
126+
);
127+
st.deepEqual(
128+
map(packageFilterPath, function (x) { return x.replace(__dirname + '/', ''); }),
129+
preserveSymlinks ? [destPkg, destPkg] : [sourcePkg, sourcePkg],
130+
'sync: packageFilter pkgfile arg is correct'
131+
);
132+
133+
var asyncPackageFilterPath = [];
134+
resolve(
135+
'mod-a',
136+
{
137+
basedir: destDir,
138+
preserveSymlinks: preserveSymlinks,
139+
packageFilter: function (pkg, pkgfile) {
140+
asyncPackageFilterPath.push(pkgfile);
141+
}
142+
},
143+
function (err, actualPath) {
144+
st.error(err, 'no error');
145+
st.equal(
146+
actualPath.replace(__dirname + '/', ''),
147+
preserveSymlinks ? destMain : sourceMain,
148+
'async: actual path is correct'
149+
);
150+
st.deepEqual(
151+
map(asyncPackageFilterPath, function (x) { return x.replace(__dirname + '/', ''); }),
152+
preserveSymlinks ? [destPkg, destPkg, destPkg] : [sourcePkg, sourcePkg, sourcePkg],
153+
'async: packageFilter pkgfile arg is correct'
154+
);
155+
}
156+
);
157+
};
158+
}
159+
160+
t.test('preserveSymlinks: false', testPackageFilter(false));
161+
162+
t.test('preserveSymlinks: true', testPackageFilter(true));
163+
164+
t.end();
165+
});

test/symlinks/dest/node_modules/mod-a

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/symlinks/source/node_modules/mod-a/index.js

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/symlinks/source/node_modules/mod-a/package.json

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)