Skip to content

Instantly share code, notes, and snippets.

@Bengejd
Created December 31, 2018 18:21
Show Gist options
  • Save Bengejd/fe7f296dae82e66670d6170369dfcf89 to your computer and use it in GitHub Desktop.
Save Bengejd/fe7f296dae82e66670d6170369dfcf89 to your computer and use it in GitHub Desktop.
Node.js recursively grab all files in folder
function getFiles(dir: string, fileList?: []) {
let files = fs.readdirSync(dir);
fileList = fileList || [];
files.forEach(async (file) => {
if(fs.statSync(dir + file).isDirectory()) {
// @ts-ignore
fileList = await getFiles(dir + file + '/', fileList);
} else {
// @ts-ignore
fileList.push( dir + '/' + file);
console.log('Found file at: ', dir + file);
}
});
return new Promise((resolve) => resolve(fileList));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment