Skip to content

Instantly share code, notes, and snippets.

@JayMGurav
Last active February 9, 2023 04:12
Show Gist options
  • Save JayMGurav/816e7c184d6cebcda3d51ae1f684d5eb to your computer and use it in GitHub Desktop.
Save JayMGurav/816e7c184d6cebcda3d51ae1f684d5eb to your computer and use it in GitHub Desktop.
Recursively traverse through the directory and list out all the files asynchronously
"use strict";
const { readdir } = require("fs");
const { promisify } = require("util");
const readdirP = promisify(readdir);
async function getAllFilesRecursively(folder) {
return await (
await readdirP(folder, { withFileTypes: true })
).reduce(async (acc, curr) => {
if (curr.isDirectory()) {
return (await acc).concat(
await getAllCsvGzipFilesRecursively(`${folder}/${curr.name}`)
);
} else {
(await acc).push(`${folder}/${curr.name}`);
return acc;
}
}, Promise.resolve([]));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment