Skip to content

Commit 7b8960c

Browse files
lbogdanljharb
authored andcommitted
Made loadAsFileSync() work the same as async loadAsFile().
1 parent 1f86d6c commit 7b8960c

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
@@ -44,6 +44,16 @@ module.exports = function (x, options) {
4444
throw err;
4545

4646
function loadAsFileSync(x) {
47+
var pkg = loadpkg(path.dirname(x));
48+
49+
if (pkg && pkg.dir && pkg.pkg && opts.pathFilter) {
50+
var rfile = path.relative(pkg.dir, x);
51+
var r = opts.pathFilter(pkg.pkg, x, rfile);
52+
if (r) {
53+
x = path.resolve(pkg.dir, r);
54+
}
55+
}
56+
4757
if (isFile(x)) {
4858
return x;
4959
}
@@ -56,6 +66,30 @@ module.exports = function (x, options) {
5666
}
5767
}
5868

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

0 commit comments

Comments
 (0)