Skip to content

Commit f5c2a41

Browse files
committed
[Fix] revert proper but unintended breaking change in sync packageFilter
Fixes #157
1 parent bdf1210 commit f5c2a41

File tree

4 files changed

+37
-8
lines changed

4 files changed

+37
-8
lines changed

lib/sync.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ module.exports = function (x, options) {
8686
} catch (jsonErr) {}
8787

8888
if (pkg && opts.packageFilter) {
89-
pkg = opts.packageFilter(pkg, pkgfile);
89+
pkg = opts.packageFilter(pkg, dir);
9090
}
9191

9292
return { pkg: pkg, dir: dir };

readme.markdown

+5-1
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ options are:
6060
* opts.isFile - function to asynchronously test whether a file exists
6161

6262
* `opts.packageFilter(pkg, pkgfile)` - transform the parsed package.json contents before looking at the "main" field
63+
* pkg - package data
64+
* pkgfile - path to package.json
6365

6466
* `opts.pathFilter(pkg, path, relativePath)` - transform a path within a package
6567
* pkg - package data
@@ -113,7 +115,9 @@ options are:
113115

114116
* opts.isFile - function to synchronously test whether a file exists
115117

116-
* `opts.packageFilter(pkg, pkgfile)` - transform the parsed package.json contents before looking at the "main" field
118+
* `opts.packageFilter(pkg, dir)` - transform the parsed package.json contents before looking at the "main" field
119+
* pkg - package data
120+
* dir - directory for package.json (Note: the second argument will change to "pkgfile" in v2)
117121

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

test/filter.js

+19-4
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,32 @@ var test = require('tape');
33
var resolve = require('../');
44

55
test('filter', function (t) {
6-
t.plan(2);
6+
t.plan(4);
77
var dir = path.join(__dirname, 'resolver');
8+
var packageFilterArgs;
89
resolve('./baz', {
910
basedir: dir,
10-
packageFilter: function (pkg) {
11+
packageFilter: function (pkg, pkgfile) {
1112
pkg.main = 'doom';
13+
packageFilterArgs = [pkg, pkgfile];
1214
return pkg;
1315
}
1416
}, function (err, res, pkg) {
1517
if (err) t.fail(err);
16-
t.equal(res, path.join(dir, 'baz/doom.js'));
17-
t.equal(pkg.main, 'doom');
18+
19+
t.equal(res, path.join(dir, 'baz/doom.js'), 'changing the package "main" works');
20+
21+
var packageData = packageFilterArgs[0];
22+
t.equal(pkg, packageData, 'first packageFilter argument is "pkg"');
23+
t.equal(packageData.main, 'doom', 'package "main" was altered');
24+
25+
var packageFile = packageFilterArgs[1];
26+
t.equal(
27+
packageFile,
28+
path.join(dir, 'baz/package.json'),
29+
'second packageFilter argument is "pkgfile"'
30+
);
31+
32+
t.end();
1833
});
1934
});

test/filter_sync.js

+12-2
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,23 @@ var resolve = require('../');
44

55
test('filter', function (t) {
66
var dir = path.join(__dirname, 'resolver');
7+
var packageFilterArgs;
78
var res = resolve.sync('./baz', {
89
basedir: dir,
9-
packageFilter: function (pkg) {
10+
packageFilter: function (pkg, dir) {
1011
pkg.main = 'doom';
12+
packageFilterArgs = [pkg, dir];
1113
return pkg;
1214
}
1315
});
14-
t.equal(res, path.join(dir, 'baz/doom.js'));
16+
17+
t.equal(res, path.join(dir, 'baz/doom.js'), 'changing the package "main" works');
18+
19+
var packageData = packageFilterArgs[0];
20+
t.equal(packageData.main, 'doom', 'package "main" was altered');
21+
22+
var packageFile = packageFilterArgs[1];
23+
t.equal(packageFile, path.join(dir, 'baz'), 'second packageFilter argument is "dir"');
24+
1525
t.end();
1626
});

0 commit comments

Comments
 (0)