@@ -18,29 +18,12 @@ export async function staticFileFinder(
1818 const rules = new FileFinderRules ( options ) ;
1919 const precomputed = new StaticFileFinder ( rules , internalTryReturn ) ;
2020
21- const queue = new Queue < string [ ] > ( [ ] ) ;
22- for ( const path of queue ) {
23- const dirEntries = await readdir ( join ( root , ...path ) , {
24- withFileTypes : true ,
25- encoding : 'utf-8' ,
26- } ) ;
27- const siblings = new Map (
28- dirEntries . map ( ( v ) => [ rules . _normalise ( v . name ) , join ( root , ...path , v . name ) ] ) ,
29- ) ;
30- for ( const file of dirEntries ) {
31- if ( rules . _checkPermitted ( file . name ) ) {
32- if ( file . isDirectory ( ) ) {
33- const dirPath = [ ...path , file . name ] ;
34- precomputed . _addDir ( dirPath ) ;
35- if ( path . length < rules . _subDirectories ) {
36- queue . push ( dirPath ) ;
37- }
38- } else if ( file . isFile ( ) ) {
39- precomputed . _addFile ( path , file . name , join ( root , ...path , file . name ) , siblings ) ;
40- }
41- }
42- }
43- }
21+ await internalDiscoverFiles (
22+ root ,
23+ rules ,
24+ ( path , name , siblings ) => precomputed . _addFile ( path , name , join ( root , ...path , name ) , siblings ) ,
25+ ( path ) => precomputed . _addDir ( path ) ,
26+ ) ;
4427 return precomputed ;
4528}
4629
@@ -142,6 +125,37 @@ export class StaticFileFinder<T> implements FileFinder {
142125 }
143126}
144127
128+ export async function internalDiscoverFiles (
129+ root : string ,
130+ rules : FileFinderRules ,
131+ fileCallback : ( path : string [ ] , name : string , siblings : Map < string , string > ) => void ,
132+ dirCallback ?: ( path : string [ ] ) => void ,
133+ ) {
134+ const queue = new Queue < string [ ] > ( [ ] ) ;
135+ for ( const path of queue ) {
136+ const dirEntries = await readdir ( join ( root , ...path ) , {
137+ withFileTypes : true ,
138+ encoding : 'utf-8' ,
139+ } ) ;
140+ const siblings = new Map (
141+ dirEntries . map ( ( v ) => [ rules . _normalise ( v . name ) , join ( root , ...path , v . name ) ] ) ,
142+ ) ;
143+ for ( const file of dirEntries ) {
144+ if ( rules . _checkPermitted ( file . name ) ) {
145+ if ( file . isDirectory ( ) ) {
146+ const dirPath = [ ...path , file . name ] ;
147+ dirCallback ?.( dirPath ) ;
148+ if ( path . length < rules . _subDirectories ) {
149+ queue . push ( dirPath ) ;
150+ }
151+ } else if ( file . isFile ( ) ) {
152+ fileCallback ( path , file . name , siblings ) ;
153+ }
154+ }
155+ }
156+ }
157+ }
158+
145159interface StaticFileInfo < T > {
146160 data : T | undefined ;
147161 basename : string ;
0 commit comments