11var fs = require ( 'fs' ) ,
2- path = require ( 'path' ) ;
2+ path = require ( 'path' ) ;
33
44/**
55 * merge two objects by extending target object with source object
@@ -9,14 +9,14 @@ var fs = require('fs'),
99 * @returns {Object } extended object
1010 */
1111function extend ( target , source , modify ) {
12- var result = target ? modify ? target : extend ( { } , target , true ) : { } ;
13- if ( ! source ) return result ;
14- for ( var key in source ) {
15- if ( source . hasOwnProperty ( key ) && source [ key ] !== undefined ) {
16- result [ key ] = source [ key ] ;
17- }
12+ var result = target ? modify ? target : extend ( { } , target , true ) : { } ;
13+ if ( ! source ) return result ;
14+ for ( var key in source ) {
15+ if ( source . hasOwnProperty ( key ) && source [ key ] !== undefined ) {
16+ result [ key ] = source [ key ] ;
1817 }
19- return result ;
18+ }
19+ return result ;
2020}
2121
2222/**
@@ -26,8 +26,8 @@ function extend(target, source, modify) {
2626 * @returns {Boolean } whether there is a match
2727 */
2828function matches ( str , match ) {
29- if ( Array . isArray ( match ) ) return match . indexOf ( str ) > - 1 ;
30- return match . test ( str ) ;
29+ if ( Array . isArray ( match ) ) return match . indexOf ( str ) > - 1 ;
30+ return match . test ( str ) ;
3131}
3232
3333/**
@@ -39,84 +39,96 @@ function matches(str, match) {
3939 * @param {Function(error) } complete fn to call when finished
4040 */
4141function readFiles ( dir , options , callback , complete ) {
42- if ( typeof options === 'function' ) {
43- complete = callback ;
44- callback = options ;
45- options = { } ;
46- }
47- if ( typeof options === 'string' ) options = {
48- encoding : options
49- } ;
50- options = extend ( {
51- recursive : true ,
52- encoding : 'utf8' ,
53- doneOnErr : true
54- } , options ) ;
55- var files = [ ] ;
42+ if ( typeof options === 'function' ) {
43+ complete = callback ;
44+ callback = options ;
45+ options = { } ;
46+ }
47+ if ( typeof options === 'string' ) options = {
48+ encoding : options
49+ } ;
50+ options = extend ( {
51+ recursive : true ,
52+ encoding : 'utf8' ,
53+ doneOnErr : true
54+ } , options ) ;
55+ var files = [ ] ;
5656
57- var done = function ( err ) {
58- if ( typeof complete === 'function' ) {
59- if ( err ) return complete ( err ) ;
60- complete ( null , files ) ;
61- }
62- } ;
57+ var done = function ( err ) {
58+ if ( typeof complete === 'function' ) {
59+ if ( err ) return complete ( err ) ;
60+ complete ( null , files ) ;
61+ }
62+ } ;
6363
64- fs . readdir ( dir , function ( err , list ) {
65- if ( err ) {
66- if ( options . doneOnErr === true ) {
67- if ( err . code === 'EACCES' ) return done ( ) ;
68- return done ( err ) ;
69- }
70- }
71- var i = 0 ;
64+ fs . readdir ( dir , function ( err , list ) {
65+ if ( err ) {
66+ if ( options . doneOnErr === true ) {
67+ if ( err . code === 'EACCES' ) return done ( ) ;
68+ return done ( err ) ;
69+ }
70+ }
71+ var i = 0 ;
7272
73- if ( options . reverse === true ||
74- ( typeof options . sort == 'string' &&
75- ( / r e v e r s e | d e s c / i) . test ( options . sort ) ) ) {
76- list = list . reverse ( ) ;
77- } else if ( options . sort !== false ) list = list . sort ( ) ;
73+ if ( options . reverse === true ||
74+ ( typeof options . sort == 'string' &&
75+ ( / r e v e r s e | d e s c / i) . test ( options . sort ) ) ) {
76+ list = list . reverse ( ) ;
77+ } else if ( options . sort !== false ) list = list . sort ( ) ;
7878
79- ( function next ( ) {
80- var filename = list [ i ++ ] ;
81- if ( ! filename ) return done ( null , files ) ;
82- var file = path . join ( dir , filename ) ;
83- fs . stat ( file , function ( err , stat ) {
84- if ( err && options . doneOnErr === true ) return done ( err ) ;
85- if ( stat && stat . isDirectory ( ) ) {
86- if ( options . recursive ) {
87- if ( options . matchDir && ! matches ( filename , options . matchDir ) ) return next ( ) ;
88- if ( options . excludeDir && matches ( filename , options . excludeDir ) ) return next ( ) ;
89- readFiles ( file , options , callback , function ( err , sfiles ) {
90- if ( err && options . doneOnErr === true ) return done ( err ) ;
91- files = files . concat ( sfiles ) ;
92- next ( ) ;
93- } ) ;
94- } else next ( ) ;
95- } else if ( stat && stat . isFile ( ) ) {
96- if ( options . match && ! matches ( filename , options . match ) ) return next ( ) ;
97- if ( options . exclude && matches ( filename , options . exclude ) ) return next ( ) ;
98- if ( options . filter && ! options . filter ( filename ) ) return next ( ) ;
99- if ( options . shortName ) files . push ( filename ) ;
100- else files . push ( file ) ;
101- fs . readFile ( file , options . encoding , function ( err , data ) {
102- if ( err ) {
103- if ( err . code === 'EACCES' ) return next ( ) ;
104- if ( options . doneOnErr === true ) {
105- return done ( err ) ;
106- }
107- }
108- if ( callback . length > 3 )
109- if ( options . shortName ) callback ( null , data , filename , next ) ;
110- else callback ( null , data , file , next ) ;
111- else callback ( null , data , next ) ;
112- } ) ;
113- }
114- else {
115- next ( ) ;
116- }
79+ ( function next ( ) {
80+ var filename = list [ i ++ ] ;
81+ if ( ! filename ) return done ( null , files ) ;
82+
83+ var file = path . join ( dir , filename ) ;
84+
85+ fs . stat ( file , function ( err , stat ) {
86+ if ( err && options . doneOnErr === true ) return done ( err ) ;
87+ if ( stat && stat . isDirectory ( ) ) {
88+ if ( options . recursive ) {
89+ if ( options . matchDir && ! matches ( filename , options . matchDir ) ) return next ( ) ;
90+ if ( options . excludeDir && matches ( filename , options . excludeDir ) ) return next ( ) ;
91+ readFiles ( file , options , callback , function ( err , sfiles ) {
92+ if ( err && options . doneOnErr === true ) return done ( err ) ;
93+ files = files . concat ( sfiles ) ;
94+ next ( ) ;
11795 } ) ;
118- } ) ( ) ;
96+ } else next ( ) ;
97+ } else if ( stat && stat . isFile ( ) ) {
98+ if ( options . match && ! matches ( filename , options . match ) ) return next ( ) ;
99+ if ( options . exclude && matches ( filename , options . exclude ) ) return next ( ) ;
100+ if ( options . filter && ! options . filter ( filename ) ) return next ( ) ;
101+
102+ if ( options . shortName ) {
103+ files . push ( filename ) ;
104+ } else {
105+ files . push ( file ) ;
106+ }
107+
108+ fs . readFile ( file , options . encoding , function ( err , data ) {
109+ if ( err ) {
110+ if ( err . code === 'EACCES' ) return next ( ) ;
111+ if ( options . doneOnErr === true ) {
112+ return done ( err ) ;
113+ }
114+ }
115+ if ( callback . length > 3 ) {
116+ if ( options . shortName ) {
117+ callback ( null , data , filename , next ) ;
118+ } else {
119+ callback ( null , data , file , next ) ;
120+ }
121+ } else {
122+ callback ( null , data , next ) ;
123+ }
124+ } ) ;
125+ }
126+ else {
127+ next ( ) ;
128+ }
129+ } ) ;
130+ } ) ( ) ;
119131
120- } ) ;
132+ } ) ;
121133}
122134module . exports = readFiles ;
0 commit comments