11import { resolve } from "node:path" ;
2+ import { runtime } from "std-env" ;
23import prettyBytes from "pretty-bytes" ;
34import { lstat , readdir } from "node:fs/promises" ;
45
@@ -39,19 +40,44 @@ export async function getFolderSize(
3940 options ?: Options ,
4041) {
4142 const { loose = false } = options || { } ;
43+ let total = 0 ;
44+ const sumTotal = ( size : number ) => total += size ;
45+ const getFileSize = loose ? looseGetFileSize : strictGetFileSize ;
46+
47+ // bun (use recursive)
48+ if ( runtime === "bun" ) {
49+ const dirents = await readdir ( base , {
50+ recursive : true ,
51+ withFileTypes : true ,
52+ } ) ;
53+
54+ if ( dirents . length === 0 ) {
55+ return mayBeWithPrettyBytes ( ) ;
56+ }
57+
58+ const promises = dirents . map ( async ( dirent ) => {
59+ if ( ! dirent . isFile ( ) ) {
60+ return ;
61+ }
62+ const size = await getFileSize ( resolve ( base , dirent . name ) ) ;
63+ sumTotal ( size ) ;
64+ } ) ;
65+
66+ await Promise . all ( promises ) ;
67+
68+ return mayBeWithPrettyBytes ( ) ;
69+ }
70+
4271 const dirents = await readdir ( base , {
4372 withFileTypes : true ,
4473 } ) ;
74+
4575 if ( dirents . length === 0 ) {
46- return 0 ;
76+ return mayBeWithPrettyBytes ( ) ;
4777 }
4878
4979 const promises : Array < Promise < number > > = [ ] ;
5080
51- let total = 0 ;
52- const sumTotal = ( size : number ) => total += size ;
53- const getFileSize = loose ? looseGetFileSize : strictGetFileSize ;
54-
5581 for ( const dirent of dirents ) {
5682 if ( dirent . isFile ( ) ) {
5783 const path = resolve ( base , dirent . name ) ;
@@ -70,9 +96,9 @@ export async function getFolderSize(
7096
7197 await Promise . all ( promises ) ;
7298
73- if ( ! pretty ) {
74- return total ;
75- }
99+ return mayBeWithPrettyBytes ( ) ;
76100
77- return prettyBytes ( total ) ;
101+ function mayBeWithPrettyBytes ( ) {
102+ return pretty ? prettyBytes ( total ) : total ;
103+ }
78104}
0 commit comments