3
3
var fs = require ( 'fs' ) ;
4
4
var path = require ( 'path' ) ;
5
5
var isGlob = require ( 'is-glob' ) ;
6
- var each = require ( 'async- each' ) ;
6
+ var each = require ( 'each-parallel-async ' ) ;
7
7
var spawn = require ( 'cross-spawn' ) ;
8
8
var isExtglob = require ( 'is-extglob' ) ;
9
9
var extend = require ( 'extend-shallow' ) ;
@@ -38,7 +38,10 @@ function glob(pattern, options, cb) {
38
38
}
39
39
40
40
if ( typeof cb !== 'function' ) {
41
- throw new TypeError ( 'expected callback to be a function' ) ;
41
+ if ( typeof cb !== 'undefined' ) {
42
+ throw new TypeError ( 'expected callback to be a function' ) ;
43
+ }
44
+ return glob . promise . apply ( glob , arguments ) ;
42
45
}
43
46
44
47
if ( typeof pattern !== 'string' ) {
@@ -57,7 +60,7 @@ function glob(pattern, options, cb) {
57
60
files = err ;
58
61
}
59
62
60
- if ( Array . isArray ( files ) && ! files . length && opts . nullglob ) {
63
+ if ( opts . nullglob === true && Array . isArray ( files ) && ! files . length ) {
61
64
files = [ pattern ] ;
62
65
}
63
66
@@ -207,6 +210,22 @@ glob.sync = function(pattern, options) {
207
210
return files ;
208
211
} ;
209
212
213
+ /**
214
+ * Emit `end` and remove listeners
215
+ */
216
+
217
+ glob . promise = function ( pattern , options , cb ) {
218
+ return new Promise ( function ( resolve , reject ) {
219
+ glob ( pattern , options , function ( err , files ) {
220
+ if ( err ) {
221
+ reject ( err ) ;
222
+ } else {
223
+ resolve ( files ) ;
224
+ }
225
+ } ) ;
226
+ } ) ;
227
+ } ;
228
+
210
229
/**
211
230
* Emit `end` and remove listeners
212
231
*/
@@ -261,8 +280,6 @@ function bash(pattern, options, cb) {
261
280
cb ( code , getFiles ( buf . toString ( ) , pattern , options ) ) ;
262
281
} ) ;
263
282
} ) ;
264
-
265
- return glob ;
266
283
}
267
284
268
285
/**
@@ -271,12 +288,7 @@ function bash(pattern, options, cb) {
271
288
272
289
function normalize ( val ) {
273
290
if ( Array . isArray ( val ) ) {
274
- var len = val . length ;
275
- var idx = - 1 ;
276
- while ( ++ idx < len ) {
277
- val [ idx ] = normalize ( val [ idx ] ) ;
278
- }
279
- return val . join ( ' ' ) ;
291
+ val = val . join ( ' ' ) ;
280
292
}
281
293
return val . split ( ' ' ) . join ( '\\ ' ) ;
282
294
}
@@ -287,14 +299,25 @@ function normalize(val) {
287
299
288
300
function cmd ( patterns , options ) {
289
301
var str = normalize ( patterns ) ;
290
- var valid = [ 'dotglob' , 'extglob' , 'failglob' , 'globstar' , 'nocaseglob' , 'nullglob' ] ;
302
+ var keys = Object . keys ( options ) ;
291
303
var args = [ ] ;
292
- for ( var key in options ) {
293
- if ( options . hasOwnProperty ( key ) && valid . indexOf ( key ) !== - 1 ) {
304
+ var valid = [
305
+ 'dotglob' ,
306
+ 'extglob' ,
307
+ 'failglob' ,
308
+ 'globstar' ,
309
+ 'nocaseglob' ,
310
+ 'nullglob'
311
+ ] ;
312
+
313
+ for ( var i = 0 ; i < keys . length ; i ++ ) {
314
+ var key = keys [ i ] ;
315
+ if ( valid . indexOf ( key ) !== - 1 ) {
294
316
args . push ( '-O' , key ) ;
295
317
}
296
318
}
297
- args . push ( '-c' , `for i in ${ str } ; do echo $i; done` ) ;
319
+
320
+ args . push ( '-c' , 'for i in ' + str + '; do echo $i; done' ) ;
298
321
return args ;
299
322
}
300
323
@@ -368,7 +391,10 @@ function getFiles(res, pattern, options) {
368
391
return new Error ( 'no matches:' + pattern ) ;
369
392
}
370
393
}
371
- return files ;
394
+
395
+ return files . filter ( function ( filepath ) {
396
+ return filepath !== '.' && filepath !== '..' ;
397
+ } ) ;
372
398
}
373
399
374
400
/**
0 commit comments