Skip to content

Instantly share code, notes, and snippets.

@marekrozmus
Created December 18, 2019 14:07
Show Gist options
  • Save marekrozmus/b28e911114f4aa5d171ea19a14343ef8 to your computer and use it in GitHub Desktop.
Save marekrozmus/b28e911114f4aa5d171ea19a14343ef8 to your computer and use it in GitHub Desktop.
Reading directory content with javascript generators
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;
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