Skip to content

Instantly share code, notes, and snippets.

@wojtekmaj
Last active April 11, 2024 13:06
Show Gist options
  • Save wojtekmaj/e151400af87d355eebf0fd84936a07de to your computer and use it in GitHub Desktop.
Save wojtekmaj/e151400af87d355eebf0fd84936a07de to your computer and use it in GitHub Desktop.
Recursive fs.readdirSync
import fs from 'node:fs';
import path from 'node:path';
function readdirSyncDeep(dir: string, rootDir: string = dir) {
const files = fs.readdirSync(dir);
const filelist: string[] = [];
files.forEach((file) => {
const filePath = path.join(dir, file);
if (fs.statSync(filePath).isDirectory()) {
filelist.push(...readdirSyncDeep(filePath, rootDir));
} else {
const relativeFilePath = path.relative(rootDir, filePath);
filelist.push(relativeFilePath);
}
});
return filelist;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment