Skip to content
This repository was archived by the owner on Jan 14, 2022. It is now read-only.

Commit 59bea1a

Browse files
committed
async try 1, going to now rework
1 parent abd3982 commit 59bea1a

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

lib/fileTools.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ var log = require('./log'),
1111
utils = require('./utils');
1212

1313
var stat = Q.nfbind(fs.stat);
14+
var PromiseQueue = require('./promiseQueue');
1415

1516
function readFile (source, options, callback) {
1617
return Q.nfcall(fs.readFile, source, options)
@@ -210,6 +211,42 @@ function folderScanSync(output, directory) {
210211
});
211212
}
212213

214+
// utilizes a simple promise queue to
215+
function folderScan(rootDirectory) {
216+
return Q.Promise(function (scanResolve, scanReject) {
217+
let queue = new PromiseQueue();
218+
const folders = [];
219+
const files = [];
220+
221+
queue.enqueue(folderScanPromise(queue, rootDirectory, folders, files))
222+
.then(() => {
223+
scanResolve({
224+
folders: folders.reverse(),
225+
files: files.reverse()
226+
});
227+
});
228+
});
229+
}
230+
231+
function folderScanPromise(promiseQueue, directory, folders, files) {
232+
// readdir promise
233+
return fs.promises.readdir(directory).then(directoryEntries => {
234+
// Q.all on the files in the directory
235+
return Q.all(directoryEntries.map(entry => {
236+
// grab the stats, if directory, then put it in the queue and add it to folders, if a file, add it to files
237+
const filePath = path.join(directory, entry);
238+
return fs.promises.stat().then(stats => {
239+
if (stats.isDirectory()) {
240+
folders.push(filePath);
241+
promiseQueue.enqueue(folderScanPromise(promiseQueue, filePath, folders, files));
242+
} else {
243+
files.push(filePath);
244+
}
245+
});
246+
}));
247+
});
248+
}
249+
213250
// Copies the 'source' file to 'target' if it's missing after creating the
214251
// required directory structure.
215252
function syncFile (source, target, callback) {

0 commit comments

Comments
 (0)