@@ -201,19 +201,46 @@ export function copyFileContent(srcPath: string, dstPath: string): Promise<void>
201201 return writeFromStream ( dstPath , srcStream )
202202}
203203
204+ async function walkDir (
205+ rPath : string ,
206+ onEntry : ( entry : Dirent , entryPath : string ) => Promise < void > | void ,
207+ errors ?: Record < string , string >
208+ ) : Promise < void > {
209+ let entries : Dirent [ ]
210+
211+ try {
212+ entries = await fs . readdir ( rPath , { withFileTypes : true } )
213+ } catch ( e : any ) {
214+ if ( ! errors ) throw e
215+ errors [ rPath ] = e . message
216+ return
217+ }
218+
219+ for ( const entry of entries ) {
220+ const entryPath = path . join ( rPath , entry . name )
221+ await onEntry ( entry , entryPath )
222+ if ( entry . isDirectory ( ) ) {
223+ await walkDir ( entryPath , onEntry , errors )
224+ }
225+ }
226+ }
227+
204228export async function dirSize ( rPath : string ) : Promise < [ number , any ] > {
205229 let size = 0
206230 const errors : Record < string , string > = { }
207- for ( const f of await fs . readdir ( rPath , { withFileTypes : true , recursive : true } ) ) {
208- if ( f . isFile ( ) ) {
209- const p = path . join ( f . parentPath , f . name )
231+
232+ await walkDir (
233+ rPath ,
234+ async ( entry , entryPath ) => {
235+ if ( ! entry . isFile ( ) ) return
210236 try {
211- size += ( await fs . stat ( p ) ) . size
237+ size += ( await fs . stat ( entryPath ) ) . size
212238 } catch ( e : any ) {
213- errors [ p ] = e . message
239+ errors [ entryPath ] = e . message
214240 }
215- }
216- }
241+ } ,
242+ errors
243+ )
217244 return [ size , errors ]
218245}
219246
@@ -222,17 +249,22 @@ export async function dirListFileNames(rPath: string): Promise<string[]> {
222249}
223250
224251export async function countDirEntries ( rPath : string ) : Promise < { files : number ; directories : number } > {
225- return ( await fs . readdir ( rPath , { withFileTypes : true , recursive : true } ) ) . reduce (
226- ( acc , f : Dirent ) => {
227- if ( f . isDirectory ( ) ) {
228- acc . directories ++
252+ const entriesCount = { files : 0 , directories : 0 }
253+ const ignoredErrors : Record < string , string > = { }
254+
255+ await walkDir (
256+ rPath ,
257+ ( entry : Dirent ) => {
258+ if ( entry . isDirectory ( ) ) {
259+ entriesCount . directories ++
229260 } else {
230- acc . files ++
261+ entriesCount . files ++
231262 }
232- return acc
233263 } ,
234- { files : 0 , directories : 0 }
264+ ignoredErrors
235265 )
266+
267+ return entriesCount
236268}
237269
238270export async function dirHasChildren ( rPath : string , mustContainsDirs = true ) : Promise < boolean > {
0 commit comments