Forked from ashblue/nodejs-recursive-directory.js
Last active
August 29, 2015 14:12
-
-
Save CaptainJojo/89ed2ea30bc94058e4b1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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