Skip to content

Instantly share code, notes, and snippets.

@gnitnuj gnitnuj/AccumulateFiles.js
Last active Feb 13, 2018

Embed
What would you like to do?
Module to file paths in a given directory
// this file exists just to name the gist, and because gist ordering is asciibetical, it's name starts with a capital letter.
const fs = require('fs');
const path = require('path');
const util = require('util');
/**
*
*
* @param {string} dir
* @param {array} [list=[]]
* @returns {array}
*/
const accumulateFiles = (dir, list = []) => {
// helper to find all files to upload
return util
.promisify(fs.readdir)(dir)
.then((result) => {
// separate files from directories
const dirs = [];
result.forEach((f) => {
const fPath = path.join(dir, f);
const stat = fs.statSync(fPath);
if (stat.isDirectory()) {
dirs.push(fPath);
} else {
list.push(fPath);
}
});
// recursively search directories for files to accumulate
if (dirs.length) {
return Promise.all(
dirs.map((d) => {
return accumulateFiles(d, list);
}),
);
}
return list;
})
.then((list) => {
return list;
});
};
module.exports = accumulateFiles;
{
"name": "accumulate-files",
"version": "0.1.0"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.