Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save CaptainJojo/89ed2ea30bc94058e4b1 to your computer and use it in GitHub Desktop.
Save CaptainJojo/89ed2ea30bc94058e4b1 to your computer and use it in GitHub Desktop.
/**
* Goes through the given directory to return all files and folders recursively
* @author Ash Blue ash@blueashes.com
* @example getFilesRecursive('./folder/sub-folder');
* @requires Must include the file system module native to NodeJS, ex. var fs = require('fs');
* @param {string} folder Folder location to search through
* @returns {object} Nested tree of the found files
*/
// var fs = require('fs');
function getFilesRecursive (folder) {
var fileContents = fs.readdirSync(folder),
fileTree = [],
stats;
fileContents.forEach(function (fileName) {
stats = fs.lstatSync(folder + '/' + fileName);
if (stats.isDirectory()) {
fileTree.push({
name: fileName,
children: getFilesRecursive(folder + '/' + fileName)
});
} else {
fileTree.push({
name: fileName
});
}
});
return fileTree;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment