Skip to content

Commit f3b7354

Browse files
committed
Made loadAsFileSync() work the same as async loadAsFile().
1 parent 2764758 commit f3b7354

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

lib/sync.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,16 @@ module.exports = function (x, options) {
4242
throw err;
4343

4444
function loadAsFileSync(x) {
45+
var pkg = loadpkg(path.dirname(x))
46+
47+
if (pkg && pkg.dir && pkg.pkg && opts.pathFilter) {
48+
var rfile = path.relative(pkg.dir, x);
49+
var r = opts.pathFilter(pkg.pkg, x, rfile);
50+
if (r) {
51+
x = path.resolve(pkg.dir, r);
52+
}
53+
}
54+
4555
if (isFile(x)) {
4656
return x;
4757
}
@@ -54,6 +64,30 @@ module.exports = function (x, options) {
5464
}
5565
}
5666

67+
function loadpkg(dir) {
68+
if (dir === '' || dir === '/' || /[/\\]node_modules[/\\]*$/.test(dir)) {
69+
return null;
70+
}
71+
72+
var pkgfile = path.join(dir, 'package.json');
73+
74+
if (!isFile(pkgfile)) {
75+
return loadpkg(path.dirname(dir));
76+
}
77+
78+
var body = readFileSync(pkgfile);
79+
80+
try {
81+
var pkg = JSON.parse(body);
82+
} catch (jsonErr) {}
83+
84+
if (pkg && opts.packageFilter) {
85+
pkg = opts.packageFilter(pkg, pkgfile);
86+
}
87+
88+
return { pkg: pkg, dir: dir };
89+
}
90+
5791
function loadAsDirectorySync(x) {
5892
var pkgfile = path.join(x, '/package.json');
5993
if (isFile(pkgfile)) {

0 commit comments

Comments
 (0)