@@ -3,7 +3,7 @@ var fs = require('fs');
3
3
var path = require ( 'path' ) ;
4
4
var caller = require ( './caller.js' ) ;
5
5
var nodeModulesPaths = require ( './node-modules-paths.js' ) ;
6
- var packagePathAndRemainder = require ( './package-path-and-remainder.js' ) ;
6
+ var splitRe = process . platform === 'win32' ? / [ \/ \\ ] / : / \/ / ;
7
7
8
8
module . exports = function resolve ( x , opts , cb ) {
9
9
if ( typeof opts === 'function' ) {
@@ -34,7 +34,7 @@ module.exports = function resolve (x, opts, cb) {
34
34
if ( / ^ (?: \. \. ? (?: \/ | $ ) | \/ | ( [ A - Z a - z ] : ) ? [ \\ \/ ] ) / . test ( x ) ) {
35
35
var res = path . resolve ( y , x ) ;
36
36
if ( x === '..' ) res += '/' ;
37
- loadAsFile ( res , function ( err , m , pkg ) {
37
+ loadAsFile ( res , opts . package , function ( err , m , pkg ) {
38
38
if ( err ) cb ( err )
39
39
else if ( m ) cb ( null , m , pkg )
40
40
else loadAsDirectory ( path . resolve ( y , x ) , function ( err , d , pkg ) {
@@ -52,62 +52,71 @@ module.exports = function resolve (x, opts, cb) {
52
52
} ) ;
53
53
54
54
function loadAsFile ( x , pkg , cb ) {
55
-
56
55
if ( typeof pkg === 'function' ) {
57
56
cb = pkg ;
58
- pkg = opts . package ;
57
+ pkg = undefined ;
59
58
}
60
59
61
- var pathAndRemainder = packagePathAndRemainder ( x ) ;
62
60
var exts = [ '' ] . concat ( extensions ) ;
63
61
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 ) {
94
63
return loadAsDirectory ( y , pkg , cb ) ;
95
64
}
96
65
else {
97
- return load ( exts , x ) ;
66
+ return load ( exts , x , pkg ) ;
98
67
}
99
68
100
- function load ( exts , x ) {
69
+ function load ( exts , x , pkg ) {
101
70
if ( exts . length === 0 ) return cb ( null , undefined , pkg ) ;
102
71
var file = x + exts [ 0 ] ;
103
72
104
73
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
+ } ) ;
108
91
} ) ;
109
92
}
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 ( / [ \\ \/ ] n o d e _ m o d u l e s [ \\ \/ ] * $ / . 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
+ } ) ;
111
120
}
112
121
113
122
function loadAsDirectory ( x , fpkg , cb ) {
0 commit comments