Skip to content

Commit 11c5928

Browse files
committed
fix: trammel: promisify
1 parent e7775a9 commit 11c5928

File tree

1 file changed

+13
-12
lines changed

1 file changed

+13
-12
lines changed

lib/trammel.js

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
'use strict';
22

3+
const {promisify} = require('node:util');
34
const {lstat, readdir} = require('fs/promises');
45
const path = require('path');
56
const {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

4445
async function processDir(dir, options, emitter) {
4546
const {stopOnError} = options;

0 commit comments

Comments
 (0)