Created
December 18, 2019 14:07
-
-
Save marekrozmus/b28e911114f4aa5d171ea19a14343ef8 to your computer and use it in GitHub Desktop.
Reading directory content with javascript generators
This file contains 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 { readdirSync } from "fs"; | |
function* filesPaths(directoryIn) { | |
const entries = readdirSync(directoryIn, { | |
withFileTypes: true | |
}); | |
const directories = entries.filter(x => x.isDirectory()).map(x => x.name); | |
const filesList = entries.filter(x => x.isFile()).map(x => x.name); | |
for (const file of filesList) { | |
yield path.join(directoryIn, file); | |
} | |
for (const directory of directories) { | |
for (const filePath of filesPaths(path.join(directoryIn, directory))) { | |
yield filePath; | |
} | |
} | |
} | |
export default filesPaths; |
This file contains 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 filePaths from "filePaths.js" | |
for (const filePath of filesPaths(process.cwd())) { | |
console.info(filePath); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment