Created
October 16, 2022 17:46
-
-
Save vKxni/d9ee917ae0a7c014ea8b93f4ec1e3790 to your computer and use it in GitHub Desktop.
Recursively read all files in a directory
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
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