Skip to content

Instantly share code, notes, and snippets.

@mariuslazar93
Last active March 15, 2018 14:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mariuslazar93/ccb6513a7c7ffb76f537e2b6d04a80ac to your computer and use it in GitHub Desktop.
Save mariuslazar93/ccb6513a7c7ffb76f537e2b6d04a80ac to your computer and use it in GitHub Desktop.
Get the list of files inside a directory tree
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