UNPKG

918 BJavaScriptView Raw
1const { readdir } = require("../compat/fs");
2const { init, walkSingleDir, readdirOpts } = require("./shared");
3
4function promise(dir, options) {
5 return new Promise((resolve, reject) => {
6 callback(dir, options, (err, output) => {
7 if (err) return reject(err);
8 resolve(output);
9 });
10 });
11}
12
13function callback(dirPath, options, callback) {
14 const { state, callbackInvoker, dir } = init(dirPath, options, callback);
15 walk(state, dir, options.maxDepth, callbackInvoker);
16}
17
18function walk(state, dir, currentDepth, callback) {
19 if (currentDepth < 0) {
20 --state.queue;
21 return;
22 }
23 readdir(dir, readdirOpts, function(error, dirents) {
24 if (error) {
25 --state.queue;
26 callback(error, state);
27 return;
28 }
29
30 walkSingleDir(walk, state, dir, dirents, currentDepth, callback);
31 if (--state.queue < 0) callback(null, state);
32 });
33}
34
35module.exports = { promise, callback };