Skip to content

Instantly share code, notes, and snippets.

@dmtrmrv
Created June 29, 2018 01:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dmtrmrv/0c96839f693eb93aff413ae418df51e7 to your computer and use it in GitHub Desktop.
Save dmtrmrv/0c96839f693eb93aff413ae418df51e7 to your computer and use it in GitHub Desktop.
Recursively return folders in Node.js
const getFolders = (dir) => {
const paths = [dir];
const scan = (dir) => {
fs.readdirSync(dir).forEach((item) => {
if (fs.statSync(path.join(dir, item)).isDirectory()) {
const subdir = `${dir + item}/`;
paths.push(subdir);
scan(subdir);
}
});
};
scan(dir);
return paths;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment