Skip to content

Instantly share code, notes, and snippets.

@vKxni
Created October 16, 2022 17:46
Show Gist options
  • Save vKxni/d9ee917ae0a7c014ea8b93f4ec1e3790 to your computer and use it in GitHub Desktop.
Save vKxni/d9ee917ae0a7c014ea8b93f4ec1e3790 to your computer and use it in GitHub Desktop.
Recursively read all files in a directory
import fs from "fs";
import path from "path";
/**
* Recursively read all files in a directory.
* @param {fs.PathLike} dirPath The path to the directory that will be recursively traversed.
* @param {Array} arrayOfFiles The array that all files will be recursively pushed to.
* @returns Returns an array of files.
*/
export function getAllFiles(dirPath: string): string[] {
const files = fs.readdirSync(dirPath);
let arrayOfFiles: string[] = [];
files.forEach(function (file: string) {
if (fs.statSync(dirPath + "/" + file).isDirectory()) {
arrayOfFiles.push(...getAllFiles(dirPath + "/" + file));
} else {
arrayOfFiles.push(path.join(dirPath, "/", file));
}
});
return arrayOfFiles;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment