11'use strict' ;
22
3+ const { promisify} = require ( 'node:util' ) ;
34const { lstat, readdir} = require ( 'fs/promises' ) ;
45const path = require ( 'path' ) ;
56const { EventEmitter} = require ( 'events' ) ;
@@ -12,18 +13,20 @@ const tryToCatch = require('try-to-catch');
1213 shall return information about the link, while stat() shall return
1314 information about the file the link references.
1415*/
15- module . exports = async ( dir , options , callback ) => {
16+ module . exports = async ( dir , options = { } ) => {
1617 const emitter = new EventEmitter ( ) ;
1718
18- let total = 0 ;
19- let type ;
19+ const [ total ] = await Promise . all ( [
20+ subscribe ( emitter , options ) ,
21+ processDir ( dir , options , emitter ) ,
22+ ] ) ;
2023
21- if ( ! callback ) {
22- callback = options ;
23- options = { } ;
24- } else {
25- type = options . type ;
26- }
24+ return total ;
25+ } ;
26+
27+ const subscribe = promisify ( ( emitter , options , callback ) => {
28+ const { type} = options ;
29+ let total = 0 ;
2730
2831 emitter . on ( 'file' , ( file , stat ) => {
2932 total += stat . size ;
@@ -37,9 +40,7 @@ module.exports = async (dir, options, callback) => {
3740
3841 callback ( null , format . size ( total ) ) ;
3942 } ) ;
40-
41- await processDir ( dir , options , emitter ) ;
42- } ;
43+ } ) ;
4344
4445async function processDir ( dir , options , emitter ) {
4546 const { stopOnError} = options ;
0 commit comments