Skip to content

Commit 07612f7

Browse files
committed
[Fix] sync: packageFilter: revert "breaking" change in 9f06a8f / #202
Fixes #204. This was a repeat of an unintentionally breaking bugfix which caused #157, which was reverted in f5c2a41, which #202 regressed to cause #204. This time, I've added lots of comments so I won't accidentally do this again.
1 parent a86405a commit 07612f7

File tree

4 files changed

+26
-13
lines changed

4 files changed

+26
-13
lines changed

lib/sync.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,8 @@ module.exports = function (x, options) {
118118
} catch (jsonErr) {}
119119

120120
if (pkg && opts.packageFilter) {
121-
pkg = opts.packageFilter(pkg, pkgfile, dir);
121+
// v2 will pass pkgfile
122+
pkg = opts.packageFilter(pkg, /*pkgfile,*/ dir); // eslint-disable-line spaced-comment
122123
}
123124

124125
return { pkg: pkg, dir: dir };
@@ -133,7 +134,8 @@ module.exports = function (x, options) {
133134
} catch (e) {}
134135

135136
if (pkg && opts.packageFilter) {
136-
pkg = opts.packageFilter(pkg, pkgfile, x);
137+
// v2 will pass pkgfile
138+
pkg = opts.packageFilter(pkg, /*pkgfile,*/ x); // eslint-disable-line spaced-comment
137139
}
138140

139141
if (pkg && pkg.main) {

readme.markdown

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,10 @@ options are:
6161

6262
* opts.isDirectory - function to asynchronously test whether a directory exists
6363

64-
* `opts.packageFilter(pkg, pkgfile)` - transform the parsed package.json contents before looking at the "main" field
64+
* `opts.packageFilter(pkg, pkgfile, dir)` - transform the parsed package.json contents before looking at the "main" field
6565
* pkg - package data
6666
* pkgfile - path to package.json
67+
* dir - directory for package.json
6768

6869
* `opts.pathFilter(pkg, path, relativePath)` - transform a path within a package
6970
* pkg - package data

test/filter_sync.js

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@ test('filter', function (t) {
77
var packageFilterArgs;
88
var res = resolve.sync('./baz', {
99
basedir: dir,
10-
packageFilter: function (pkg, pkgfile, dir) {
10+
// NOTE: in v2.x, this will be `pkg, pkgfile, dir`, but must remain "broken" here in v1.x for compatibility
11+
packageFilter: function (pkg, /*pkgfile,*/ dir) { // eslint-disable-line spaced-comment
1112
pkg.main = 'doom'; // eslint-disable-line no-param-reassign
12-
packageFilterArgs = [pkg, pkgfile, dir];
13+
packageFilterArgs = 'is 1.x' ? [pkg, dir] : [pkg, pkgfile, dir]; // eslint-disable-line no-constant-condition, no-undef
1314
return pkg;
1415
}
1516
});
@@ -19,11 +20,14 @@ test('filter', function (t) {
1920
var packageData = packageFilterArgs[0];
2021
t.equal(packageData.main, 'doom', 'package "main" was altered');
2122

22-
var packageFile = packageFilterArgs[1];
23-
t.equal(packageFile, path.join(dir, 'baz', 'package.json'), 'package.json path is correct');
23+
if (!'is 1.x') { // eslint-disable-line no-constant-condition
24+
var packageFile = packageFilterArgs[1];
25+
t.equal(packageFile, path.join(dir, 'baz', 'package.json'), 'package.json path is correct');
26+
}
2427

25-
var packageDir = packageFilterArgs[2];
26-
t.equal(packageDir, path.join(dir, 'baz'), 'third packageFilter argument is "dir"');
28+
var packageDir = packageFilterArgs['is 1.x' ? 1 : 2]; // eslint-disable-line no-constant-condition
29+
// eslint-disable-next-line no-constant-condition
30+
t.equal(packageDir, path.join(dir, 'baz'), ('is 1.x' ? 'second' : 'third') + ' packageFilter argument is "dir"');
2731

2832
t.end();
2933
});

test/symlinks.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -105,19 +105,21 @@ test('packageFilter', function (t) {
105105

106106
function testPackageFilter(preserveSymlinks) {
107107
return function (st) {
108-
st.plan(5);
108+
st.plan('is 1.x' ? 3 : 5); // eslint-disable-line no-constant-condition
109109

110110
var destMain = 'symlinks/dest/node_modules/mod-a/index.js';
111111
var destPkg = 'symlinks/dest/node_modules/mod-a/package.json';
112112
var sourceMain = 'symlinks/source/node_modules/mod-a/index.js';
113113
var sourcePkg = 'symlinks/source/node_modules/mod-a/package.json';
114114
var destDir = path.join(__dirname, 'symlinks', 'dest');
115115

116+
/* eslint multiline-comment-style: 0 */
117+
/* v2.x will restore these tests
116118
var packageFilterPath = [];
117119
var actualPath = resolve.sync('mod-a', {
118120
basedir: destDir,
119121
preserveSymlinks: preserveSymlinks,
120-
packageFilter: function (pkg, pkgfile) {
122+
packageFilter: function (pkg, pkgfile, dir) {
121123
packageFilterPath.push(pkgfile);
122124
}
123125
});
@@ -131,6 +133,7 @@ test('packageFilter', function (t) {
131133
map(preserveSymlinks ? [destPkg, destPkg] : [sourcePkg, sourcePkg], path.normalize),
132134
'sync: packageFilter pkgfile arg is correct'
133135
);
136+
*/
134137

135138
var asyncPackageFilterPath = [];
136139
resolve(
@@ -150,8 +153,11 @@ test('packageFilter', function (t) {
150153
'async: actual path is correct'
151154
);
152155
st.deepEqual(
153-
map(packageFilterPath, relative),
154-
map(preserveSymlinks ? [destPkg, destPkg] : [sourcePkg, sourcePkg], path.normalize),
156+
map(asyncPackageFilterPath, relative),
157+
map(
158+
preserveSymlinks ? [destPkg, destPkg, destPkg] : [sourcePkg, sourcePkg, sourcePkg],
159+
path.normalize
160+
),
155161
'async: packageFilter pkgfile arg is correct'
156162
);
157163
}

0 commit comments

Comments
 (0)