Last active
December 2, 2020 12:46
-
-
Save MakotoUwaya/78ad25b2264975388aebfed3b388e344 to your computer and use it in GitHub Desktop.
Retrieving files recursively for nodejs with flatMap(ES2019)
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'; | |
| const targetDirectoryPath = 'directoryPath'; | |
| const listFiles = (dir: string): string[] => { | |
| return fs.readdirSync(dir, { withFileTypes: true }).flatMap(dirent => { | |
| const path = `${dir}/${dirent.name}`; | |
| return dirent.isFile() ? [path] : listFiles(path); | |
| }); | |
| }; | |
| console.log(listFiles(targetDirectoryPath)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment