Skip to content

Commit

Permalink
Use path.dirname to walk up looking for a package.json
Browse files Browse the repository at this point in the history
This fixes async resolution when looking for a package.json file.
Previously a regex was being used that was not allowing the recursive
behavior the code is attempting. Replacing with path.dirname fixes the
issue.

Fixes browserify#76
  • Loading branch information
matthewp committed Mar 11, 2015
1 parent a225602 commit e83630d
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 7 deletions.
2 changes: 1 addition & 1 deletion lib/async.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ module.exports = function resolve (x, opts, cb) {
isFile(pkgfile, function (err, ex) {
// on err, ex is false
if (!ex) return loadpkg(
dir.replace(/[\\\/]*[^\\\/]+[\\\/]*/, ''), cb
path.dirname(dir), cb
);

readFile(pkgfile, function (err, body) {
Expand Down
11 changes: 10 additions & 1 deletion test/pathfilter.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ var test = require('tape');
var resolve = require('../');

test('#62: deep module references and the pathFilter', function(t){
t.plan(6);
t.plan(9);

var resolverDir = __dirname + '/pathfilter/deep_ref';
var pathFilter = function(pkg, x, remainder){
Expand All @@ -14,9 +14,18 @@ test('#62: deep module references and the pathFilter', function(t){

resolve('deep/ref', { basedir : resolverDir }, function (err, res, pkg) {
if (err) t.fail(err);

t.equal(pkg.version, "1.2.3");
t.equal(res, resolverDir + '/node_modules/deep/ref.js');
});

resolve('deep/deeper/ref', { basedir: resolverDir },
function(err, res, pkg) {
if(err) t.fail(err);
t.notEqual(pkg, undefined);
t.equal(pkg.version, "1.2.3");
t.equal(res, resolverDir + '/node_modules/deep/deeper/ref.js');
});

resolve('deep/ref', { basedir : resolverDir, pathFilter : pathFilter },
function (err, res, pkg) {
Expand Down
Empty file.
3 changes: 1 addition & 2 deletions test/precedence.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@ var test = require('tape');
var resolve = require('../');

test('precedence', function (t) {
t.plan(3);
t.plan(2);
var dir = path.join(__dirname, 'precedence/aaa');

resolve('./', { basedir : dir }, function (err, res, pkg) {
t.ifError(err);
t.equal(res, path.join(dir, 'index.js'));
t.equal(pkg, undefined);
});
});

Expand Down
4 changes: 1 addition & 3 deletions test/resolver.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,17 @@ var test = require('tape');
var resolve = require('../');

test('async foo', function (t) {
t.plan(9);
t.plan(7);
var dir = __dirname + '/resolver';

resolve('./foo', { basedir : dir }, function (err, res, pkg) {
if (err) t.fail(err);
t.equal(res, dir + '/foo.js');
t.equal(pkg, undefined);
});

resolve('./foo.js', { basedir : dir }, function (err, res, pkg) {
if (err) t.fail(err);
t.equal(res, dir + '/foo.js');
t.equal(pkg, undefined);
});

resolve('./foo', { basedir : dir, package: { main: 'resolver' } }, function (err, res, pkg) {
Expand Down

0 comments on commit e83630d

Please sign in to comment.