Skip to content

Commit

Permalink
[Fix] revert proper but unintended breaking change in sync packageFilter
Browse files Browse the repository at this point in the history
Fixes #157
  • Loading branch information
ljharb committed Apr 12, 2018
1 parent bdf1210 commit f5c2a41
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 8 deletions.
2 changes: 1 addition & 1 deletion lib/sync.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ module.exports = function (x, options) {
} catch (jsonErr) {}

if (pkg && opts.packageFilter) {
pkg = opts.packageFilter(pkg, pkgfile);
pkg = opts.packageFilter(pkg, dir);
}

return { pkg: pkg, dir: dir };
Expand Down
6 changes: 5 additions & 1 deletion readme.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ options are:
* opts.isFile - function to asynchronously test whether a file exists

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

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

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

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

* `opts.pathFilter(pkg, path, relativePath)` - transform a path within a package
* pkg - package data
Expand Down
23 changes: 19 additions & 4 deletions test/filter.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,32 @@ var test = require('tape');
var resolve = require('../');

test('filter', function (t) {
t.plan(2);
t.plan(4);
var dir = path.join(__dirname, 'resolver');
var packageFilterArgs;
resolve('./baz', {
basedir: dir,
packageFilter: function (pkg) {
packageFilter: function (pkg, pkgfile) {
pkg.main = 'doom';
packageFilterArgs = [pkg, pkgfile];
return pkg;
}
}, function (err, res, pkg) {
if (err) t.fail(err);
t.equal(res, path.join(dir, 'baz/doom.js'));
t.equal(pkg.main, 'doom');

t.equal(res, path.join(dir, 'baz/doom.js'), 'changing the package "main" works');

var packageData = packageFilterArgs[0];
t.equal(pkg, packageData, 'first packageFilter argument is "pkg"');
t.equal(packageData.main, 'doom', 'package "main" was altered');

var packageFile = packageFilterArgs[1];
t.equal(
packageFile,
path.join(dir, 'baz/package.json'),
'second packageFilter argument is "pkgfile"'
);

t.end();
});
});
14 changes: 12 additions & 2 deletions test/filter_sync.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,23 @@ var resolve = require('../');

test('filter', function (t) {
var dir = path.join(__dirname, 'resolver');
var packageFilterArgs;
var res = resolve.sync('./baz', {
basedir: dir,
packageFilter: function (pkg) {
packageFilter: function (pkg, dir) {
pkg.main = 'doom';
packageFilterArgs = [pkg, dir];
return pkg;
}
});
t.equal(res, path.join(dir, 'baz/doom.js'));

t.equal(res, path.join(dir, 'baz/doom.js'), 'changing the package "main" works');

var packageData = packageFilterArgs[0];
t.equal(packageData.main, 'doom', 'package "main" was altered');

var packageFile = packageFilterArgs[1];
t.equal(packageFile, path.join(dir, 'baz'), 'second packageFilter argument is "dir"');

t.end();
});

0 comments on commit f5c2a41

Please sign in to comment.