@@ -3,79 +3,78 @@ 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 splitRe = process . platform === 'win32' ? / [ \/ \\ ] / : / \/ / ;
7
6
8
- module . exports = function resolve ( x , opts , cb ) {
7
+ module . exports = function resolve ( x , options , callback ) {
8
+ var cb = callback ;
9
+ var opts = options || { } ;
9
10
if ( typeof opts === 'function' ) {
10
11
cb = opts ;
11
12
opts = { } ;
12
13
}
13
- if ( ! opts ) opts = { } ;
14
14
if ( typeof x !== 'string' ) {
15
15
var err = new TypeError ( 'path must be a string' ) ;
16
16
return process . nextTick ( function ( ) {
17
17
cb ( err ) ;
18
18
} ) ;
19
19
}
20
-
20
+
21
21
var isFile = opts . isFile || function ( file , cb ) {
22
22
fs . stat ( file , function ( err , stat ) {
23
- if ( err && err . code === 'ENOENT' ) cb ( null , false )
24
- else if ( err ) cb ( err )
25
- else cb ( null , stat . isFile ( ) || stat . isFIFO ( ) )
23
+ if ( err && err . code === 'ENOENT' ) cb ( null , false ) ;
24
+ else if ( err ) cb ( err ) ;
25
+ else cb ( null , stat . isFile ( ) || stat . isFIFO ( ) ) ;
26
26
} ) ;
27
27
} ;
28
28
var readFile = opts . readFile || fs . readFile ;
29
-
30
- var extensions = opts . extensions || [ '.js' ] ;
29
+
30
+ var extensions = opts . extensions || [ '.js' ] ;
31
31
var y = opts . basedir || path . dirname ( caller ( ) ) ;
32
-
32
+
33
33
opts . paths = opts . paths || [ ] ;
34
-
34
+
35
35
if ( / ^ (?: \. \. ? (?: \/ | $ ) | \/ | ( [ A - Z a - z ] : ) ? [ \\ \/ ] ) / . test ( x ) ) {
36
36
var res = path . resolve ( y , x ) ;
37
37
if ( x === '..' ) res += '/' ;
38
38
if ( / \/ $ / . test ( x ) && res === y ) {
39
39
loadAsDirectory ( res , opts . package , onfile ) ;
40
- }
41
- else loadAsFile ( res , opts . package , onfile ) ;
42
- }
43
- else loadNodeModules ( x , y , function ( err , n , pkg ) {
44
- if ( err ) cb ( err )
45
- else if ( n ) cb ( null , n , pkg )
40
+ } else loadAsFile ( res , opts . package , onfile ) ;
41
+ } else loadNodeModules ( x , y , function ( err , n , pkg ) {
42
+ if ( err ) cb ( err ) ;
43
+ else if ( n ) cb ( null , n , pkg ) ;
46
44
else if ( core [ x ] ) return cb ( null , x ) ;
47
- else cb ( new Error ( "Cannot find module '" + x + "' from '" + y + "'" ) )
45
+ else cb ( new Error ( "Cannot find module '" + x + "' from '" + y + "'" ) ) ;
48
46
} ) ;
49
-
50
- function onfile ( err , m , pkg ) {
51
- if ( err ) cb ( err )
52
- else if ( m ) cb ( null , m , pkg )
47
+
48
+ function onfile ( err , m , pkg ) {
49
+ if ( err ) cb ( err ) ;
50
+ else if ( m ) cb ( null , m , pkg ) ;
53
51
else loadAsDirectory ( res , function ( err , d , pkg ) {
54
- if ( err ) cb ( err )
55
- else if ( d ) cb ( null , d , pkg )
56
- else cb ( new Error ( "Cannot find module '" + x + "' from '" + y + "'" ) )
57
- } )
52
+ if ( err ) cb ( err ) ;
53
+ else if ( d ) cb ( null , d , pkg ) ;
54
+ else cb ( new Error ( "Cannot find module '" + x + "' from '" + y + "'" ) ) ;
55
+ } ) ;
58
56
}
59
-
60
- function loadAsFile ( x , pkg , cb ) {
57
+
58
+ function loadAsFile ( x , pkg , callback ) {
59
+ var cb = callback ;
61
60
if ( typeof pkg === 'function' ) {
62
61
cb = pkg ;
63
62
pkg = undefined ;
64
63
}
65
-
64
+
66
65
var exts = [ '' ] . concat ( extensions ) ;
67
- load ( exts , x , pkg )
66
+ load ( exts , x , pkg ) ;
68
67
69
- function load ( exts , x , pkg ) {
68
+ function load ( exts , x , pkg ) {
70
69
if ( exts . length === 0 ) return cb ( null , undefined , pkg ) ;
71
70
var file = x + exts [ 0 ] ;
72
-
73
- if ( pkg ) onpkg ( null , pkg )
71
+
72
+ if ( pkg ) onpkg ( null , pkg ) ;
74
73
else loadpkg ( path . dirname ( file ) , onpkg ) ;
75
-
76
- function onpkg ( err , pkg_ , dir ) {
74
+
75
+ function onpkg ( err , pkg_ , dir ) {
77
76
pkg = pkg_ ;
78
- if ( err ) return cb ( err )
77
+ if ( err ) return cb ( err ) ;
79
78
if ( dir && pkg && opts . pathFilter ) {
80
79
var rfile = path . relative ( dir , file ) ;
81
80
var rel = rfile . slice ( 0 , rfile . length - exts [ 0 ] . length ) ;
@@ -88,66 +87,63 @@ module.exports = function resolve (x, opts, cb) {
88
87
}
89
88
isFile ( file , onex ) ;
90
89
}
91
- function onex ( err , ex ) {
92
- if ( err ) cb ( err )
93
- else if ( ! ex ) load ( exts . slice ( 1 ) , x , pkg )
94
- else cb ( null , file , pkg )
90
+ function onex ( err , ex ) {
91
+ if ( err ) cb ( err ) ;
92
+ else if ( ! ex ) load ( exts . slice ( 1 ) , x , pkg ) ;
93
+ else cb ( null , file , pkg ) ;
95
94
}
96
95
}
97
96
}
98
-
99
- function loadpkg ( dir , cb ) {
97
+
98
+ function loadpkg ( dir , cb ) {
100
99
if ( dir === '' || dir === '/' ) return cb ( null ) ;
101
- if ( process . platform === 'win32' && / ^ \w : [ \\ \/ ] * $ / . test ( dir ) ) {
100
+ if ( process . platform === 'win32' && ( / ^ \w : [ \\ \/ ] * $ / ) . test ( dir ) ) {
102
101
return cb ( null ) ;
103
102
}
104
103
if ( / [ \\ \/ ] n o d e _ m o d u l e s [ \\ \/ ] * $ / . test ( dir ) ) return cb ( null ) ;
105
-
104
+
106
105
var pkgfile = path . join ( dir , 'package.json' ) ;
107
106
isFile ( pkgfile , function ( err , ex ) {
108
107
// on err, ex is false
109
- if ( ! ex ) return loadpkg (
110
- path . dirname ( dir ) , cb
111
- ) ;
112
-
108
+ if ( ! ex ) return loadpkg ( path . dirname ( dir ) , cb ) ;
109
+
113
110
readFile ( pkgfile , function ( err , body ) {
114
111
if ( err ) cb ( err ) ;
115
- try { var pkg = JSON . parse ( body ) }
116
- catch ( err ) { }
117
-
112
+ try { var pkg = JSON . parse ( body ) ; } catch ( jsonErr ) { }
113
+
118
114
if ( pkg && opts . packageFilter ) {
119
115
pkg = opts . packageFilter ( pkg , pkgfile ) ;
120
116
}
121
117
cb ( null , pkg , dir ) ;
122
118
} ) ;
123
119
} ) ;
124
120
}
125
-
126
- function loadAsDirectory ( x , fpkg , cb ) {
121
+
122
+ function loadAsDirectory ( x , fpkg , callback ) {
123
+ var cb = callback ;
127
124
if ( typeof fpkg === 'function' ) {
128
125
cb = fpkg ;
129
126
fpkg = opts . package ;
130
127
}
131
-
128
+
132
129
var pkgfile = path . join ( x , '/package.json' ) ;
133
130
isFile ( pkgfile , function ( err , ex ) {
134
131
if ( err ) return cb ( err ) ;
135
132
if ( ! ex ) return loadAsFile ( path . join ( x , '/index' ) , fpkg , cb ) ;
136
-
133
+
137
134
readFile ( pkgfile , function ( err , body ) {
138
135
if ( err ) return cb ( err ) ;
139
136
try {
140
137
var pkg = JSON . parse ( body ) ;
141
- }
142
- catch ( err ) { }
143
-
138
+ } catch ( jsonErr ) { }
139
+
144
140
if ( opts . packageFilter ) {
145
141
pkg = opts . packageFilter ( pkg , pkgfile ) ;
146
142
}
147
-
143
+
148
144
if ( pkg . main ) {
149
- if ( pkg . main === '.' || pkg . main === './' ) {
150
- pkg . main = 'index'
145
+ if ( pkg . main === '.' || pkg . main === './' ) {
146
+ pkg . main = 'index' ;
151
147
}
152
148
loadAsFile ( path . resolve ( x , pkg . main ) , pkg , function ( err , m , pkg ) {
153
149
if ( err ) return cb ( err ) ;
@@ -163,31 +159,32 @@ module.exports = function resolve (x, opts, cb) {
163
159
} ) ;
164
160
return ;
165
161
}
166
-
162
+
167
163
loadAsFile ( path . join ( x , '/index' ) , pkg , cb ) ;
168
164
} ) ;
169
165
} ) ;
170
166
}
171
-
172
- function loadNodeModules ( x , start , cb ) {
173
- ( function process ( dirs ) {
174
- if ( dirs . length === 0 ) return cb ( null , undefined ) ;
175
- var dir = dirs [ 0 ] ;
176
-
177
- var file = path . join ( dir , '/' , x ) ;
178
- loadAsFile ( file , undefined , onfile ) ;
179
-
180
- function onfile ( err , m , pkg ) {
181
- if ( err ) return cb ( err ) ;
182
- if ( m ) return cb ( null , m , pkg ) ;
183
- loadAsDirectory ( path . join ( dir , '/' , x ) , undefined , ondir ) ;
184
- }
185
-
186
- function ondir ( err , n , pkg ) {
187
- if ( err ) return cb ( err ) ;
188
- if ( n ) return cb ( null , n , pkg ) ;
189
- process ( dirs . slice ( 1 ) ) ;
190
- }
191
- } ) ( nodeModulesPaths ( start , opts ) ) ;
167
+
168
+ function processDirs ( cb , dirs ) {
169
+ if ( dirs . length === 0 ) return cb ( null , undefined ) ;
170
+ var dir = dirs [ 0 ] ;
171
+
172
+ var file = path . join ( dir , '/' , x ) ;
173
+ loadAsFile ( file , undefined , onfile ) ;
174
+
175
+ function onfile ( err , m , pkg ) {
176
+ if ( err ) return cb ( err ) ;
177
+ if ( m ) return cb ( null , m , pkg ) ;
178
+ loadAsDirectory ( path . join ( dir , '/' , x ) , undefined , ondir ) ;
179
+ }
180
+
181
+ function ondir ( err , n , pkg ) {
182
+ if ( err ) return cb ( err ) ;
183
+ if ( n ) return cb ( null , n , pkg ) ;
184
+ processDirs ( cb , dirs . slice ( 1 ) ) ;
185
+ }
186
+ }
187
+ function loadNodeModules ( x , start , cb ) {
188
+ processDirs ( cb , nodeModulesPaths ( start , opts ) ) ;
192
189
}
193
190
} ;
0 commit comments