@@ -44,6 +44,16 @@ module.exports = function (x, options) {
44
44
throw err ;
45
45
46
46
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 ) ; // eslint-disable-line no-param-reassign
54
+ }
55
+ }
56
+
47
57
if ( isFile ( x ) ) {
48
58
return x ;
49
59
}
@@ -56,6 +66,32 @@ module.exports = function (x, options) {
56
66
}
57
67
}
58
68
69
+ function loadpkg ( dir ) {
70
+ if ( dir === '' || dir === '/' ) return ;
71
+ if ( process . platform === 'win32' && ( / ^ \w : [ / \\ ] * $ / ) . test ( dir ) ) {
72
+ return ;
73
+ }
74
+ if ( / [ / \\ ] n o d e _ m o d u l e s [ / \\ ] * $ / . test ( dir ) ) return ;
75
+
76
+ var pkgfile = path . join ( dir , 'package.json' ) ;
77
+
78
+ if ( ! isFile ( pkgfile ) ) {
79
+ return loadpkg ( path . dirname ( dir ) ) ;
80
+ }
81
+
82
+ var body = readFileSync ( pkgfile ) ;
83
+
84
+ try {
85
+ var pkg = JSON . parse ( body ) ;
86
+ } catch ( jsonErr ) { }
87
+
88
+ if ( pkg && opts . packageFilter ) {
89
+ pkg = opts . packageFilter ( pkg , pkgfile ) ;
90
+ }
91
+
92
+ return { pkg : pkg , dir : dir } ;
93
+ }
94
+
59
95
function loadAsDirectorySync ( x ) {
60
96
var pkgfile = path . join ( x , '/package.json' ) ;
61
97
if ( isFile ( pkgfile ) ) {
0 commit comments