Last active
March 15, 2018 14:59
-
-
Save mariuslazar93/ccb6513a7c7ffb76f537e2b6d04a80ac to your computer and use it in GitHub Desktop.
Get the list of files inside a directory tree
This file contains hidden or 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
const fs = require('fs'); | |
const path = require('path'); | |
/** | |
* Traversing a directory tree and get a list of the files inside it. | |
* | |
* @param {string} dir | |
* Directory path | |
* | |
* @return {string[]} | |
* Returns an array of file paths | |
*/ | |
function getDirectoryFilesList(dir) { | |
return fs.readdirSync(dir) | |
.reduce((files, file) => { | |
const filePath = path.join(dir, file); | |
const stats = fs.statSync(filePath); | |
return stats.isDirectory() ? | |
files.concat(getDirectoryFilesList(filePath)) : files.concat(filePath); | |
}, []); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment