Skip to content

Commit 60ff554

Browse files
author
James Halliday
committed
re-implemented pathfilter feature nearly passes the test
1 parent 7f0a3f1 commit 60ff554

File tree

1 file changed

+50
-41
lines changed

1 file changed

+50
-41
lines changed

lib/async.js

Lines changed: 50 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ var fs = require('fs');
33
var path = require('path');
44
var caller = require('./caller.js');
55
var nodeModulesPaths = require('./node-modules-paths.js');
6-
var packagePathAndRemainder = require('./package-path-and-remainder.js');
6+
var splitRe = process.platform === 'win32' ? /[\/\\]/ : /\//;
77

88
module.exports = function resolve (x, opts, cb) {
99
if (typeof opts === 'function') {
@@ -34,7 +34,7 @@ module.exports = function resolve (x, opts, cb) {
3434
if (/^(?:\.\.?(?:\/|$)|\/|([A-Za-z]:)?[\\\/])/.test(x)) {
3535
var res = path.resolve(y, x);
3636
if (x === '..') res += '/';
37-
loadAsFile(res, function (err, m, pkg) {
37+
loadAsFile(res, opts.package, function (err, m, pkg) {
3838
if (err) cb(err)
3939
else if (m) cb(null, m, pkg)
4040
else loadAsDirectory(path.resolve(y, x), function (err, d, pkg) {
@@ -52,62 +52,71 @@ module.exports = function resolve (x, opts, cb) {
5252
});
5353

5454
function loadAsFile (x, pkg, cb) {
55-
5655
if (typeof pkg === 'function') {
5756
cb = pkg;
58-
pkg = opts.package;
57+
pkg = undefined;
5958
}
6059

61-
var pathAndRemainder = packagePathAndRemainder(x);
6260
var exts = [''].concat(extensions);
6361

64-
if (pathAndRemainder) {
65-
var pkgfile = path.join(pathAndRemainder.path, 'package.json');
66-
var remainder = pathAndRemainder.remainder;
67-
68-
isFile(pkgfile, function (err, ex) {
69-
if (!ex) return load(exts, x);
70-
71-
readFile(pkgfile, function (err, body) {
72-
if (err) return load(exts, x);
73-
try {
74-
pkg = JSON.parse(body);
75-
}
76-
catch (err) {}
77-
78-
if (opts.packageFilter) {
79-
pkg = opts.packageFilter(pkg, pkgfile);
80-
}
81-
if (opts.pathFilter && remainder) {
82-
var newRemainder = opts.pathFilter(pkg, x, remainder);
83-
if (newRemainder) {
84-
return load(exts, path.resolve(
85-
pathAndRemainder.path, newRemainder)
86-
);
87-
}
88-
}
89-
return load(exts, x);
90-
});
91-
});
92-
}
93-
else if (x === y) {
62+
if (x === y) {
9463
return loadAsDirectory(y, pkg, cb);
9564
}
9665
else {
97-
return load(exts, x);
66+
return load(exts, x, pkg);
9867
}
9968

100-
function load (exts, x) {
69+
function load (exts, x, pkg) {
10170
if (exts.length === 0) return cb(null, undefined, pkg);
10271
var file = x + exts[0];
10372

10473
isFile(file, function (err, ex) {
105-
if (err) cb(err)
106-
else if (ex) cb(null, file, pkg)
107-
else load(exts.slice(1), x)
74+
if (err) return cb(err)
75+
if (!ex) return load(exts.slice(1), x, pkg)
76+
if (pkg) return cb(null, file, pkg)
77+
78+
loadpkg(path.dirname(file), function (err, pkg, dir) {
79+
if (err) return cb(err)
80+
if (pkg && opts.pathFilter) {
81+
var rel = path.relative(dir, file);
82+
var r = opts.pathFilter(pkg, x, rel);
83+
if (r) return load(
84+
extensions.slice(),
85+
path.resolve(dir, r),
86+
pkg
87+
);
88+
}
89+
cb(null, file, pkg)
90+
});
10891
});
10992
}
110-
93+
}
94+
95+
function loadpkg (dir, cb) {
96+
if (dir === '' || dir === '/') return cb(null);
97+
if (process.platform === 'win32' && /^\w:[\\\/]*$/.test(dir)) {
98+
return cb(null);
99+
}
100+
if (/[\\\/]node_modules[\\\/]*$/.test(dir)) return cb(null);
101+
102+
var pkgfile = path.join(dir, 'package.json');
103+
isFile(pkgfile, function (err, ex) {
104+
if (err) return cb(err);
105+
if (!ex) return loadpkg(
106+
dir.replace(/[\\\/]*[^\\\/]+[\\\/]*/, ''), cb
107+
);
108+
109+
readFile(pkgfile, function (err, body) {
110+
if (err) cb(err);
111+
try { var pkg = JSON.parse(body) }
112+
catch (err) {}
113+
114+
if (pkg && opts.packageFilter) {
115+
pkg = opts.packageFilter(pkg, pkgfile);
116+
}
117+
cb(null, pkg, dir);
118+
});
119+
});
111120
}
112121

113122
function loadAsDirectory (x, fpkg, cb) {

0 commit comments

Comments
 (0)