@@ -54,6 +54,7 @@ var Promise = require('bluebird'),
5454 // append a '/' for directories, a '=' for socket files
5555 // and a '|' for FIFOs
5656 f : false ,
57+ directoryFirst : false ,
5758 } ,
5859
5960 _ignores = [ ] ,
@@ -221,6 +222,16 @@ var Promise = require('bluebird'),
221222
222223 } ,
223224
225+ // calc priority when the flag directoryFirst is true.
226+ _calcPriority = function ( node ) {
227+ return node . type === 'directory' ? 1 : 0 ;
228+ } ,
229+
230+ // compare priority when the flag directoryFirst is true.
231+ _comparePriority = function ( nodeA , nodeB ) {
232+ return _calcPriority ( nodeB ) - _calcPriority ( nodeA ) ;
233+ } ,
234+
224235 appendChildNodes = function ( parent ) {
225236
226237 _debug ( 'appendTreeNode:' , parent ) ;
@@ -278,8 +289,39 @@ var Promise = require('bluebird'),
278289 if ( _flags . d && type !== 'directory' ) {
279290 return
280291 }
292+ if ( _testIgnore ( child , _ignores ) ) {
293+ child . ignore = true ;
294+ }
295+ parent . children . push ( child ) ;
296+ // for statistics.
297+ if ( ! child . ignore ) {
298+ _stats . all . push ( child ) ;
299+ _stats [ type ] . push ( child ) ;
300+ }
301+ else {
302+ if ( ! _stats . ignore [ type ] ) {
303+ _stats . ignore [ type ] = [ ] ;
304+ }
305+ _stats . ignore [ type ] . push ( child ) ;
306+ }
307+ } )
308+ . catch ( function ( err ) {
309+ _debug ( filePath , ' is invalid to access.' ) ;
310+ } )
311+ } ) ;
312+ } )
313+ . then ( function ( ) {
314+ if ( _flags . directoryFirst ) {
315+ parent . children . sort ( _comparePriority ) ;
316+ }
317+ } )
318+ . then ( function ( ) {
319+ return Promise . resolve ( parent . children )
320+ . each ( function ( child , index ) {
321+ return Promise . resolve ( )
322+ . then ( function ( ) {
281323 // otherwise every type of file counts.
282- var isLast = index === files . length - 1 ;
324+ var isLast = index === parent . children . length - 1 ;
283325 isLast && ( child . lasts [ child . level - 1 ] = true ) ;
284326 if ( _testIgnore ( child , _ignores ) ) {
285327 child . ignore = true ;
@@ -297,25 +339,10 @@ var Promise = require('bluebird'),
297339 }
298340 }
299341 }
300- parent . children . push ( child ) ;
301- // for statistics.
302- if ( ! child . ignore ) {
303- _stats . all . push ( child ) ;
304- _stats [ type ] . push ( child ) ;
305- }
306- else {
307- if ( ! _stats . ignore [ type ] ) {
308- _stats . ignore [ type ] = [ ] ;
309- }
310- _stats . ignore [ type ] . push ( child ) ;
311- }
312- if ( type === 'directory' ) {
342+ if ( child . type === 'directory' ) {
313343 return appendChildNodes ( child ) ;
314344 }
315- } )
316- . catch ( function ( err ) {
317- _debug ( filePath , ' is invalid to access.' ) ;
318- } )
345+ } ) ;
319346 } ) ;
320347 } )
321348 . catch ( function ( err ) {
@@ -327,7 +354,6 @@ var Promise = require('bluebird'),
327354 process . exit ( - 1 ) ;
328355 }
329356 } ) ;
330-
331357 } ,
332358
333359 genTree = function ( rootPath ) {
0 commit comments