Skip to content

Instantly share code, notes, and snippets.

@tksst
Created August 26, 2022 10:21
Show Gist options
  • Save tksst/4d9afa5cadd262ecf7ba758706fddacf to your computer and use it in GitHub Desktop.
Save tksst/4d9afa5cadd262ecf7ba758706fddacf to your computer and use it in GitHub Desktop.
recursiveReaddir.ts
import fs from "node:fs/promises";
import path from "node:path";
async function* recursiveReaddir(dir: string): AsyncGenerator<string> {
const dirents = await fs.readdir(dir, { withFileTypes: true });
for (const dirent of dirents) {
const joined = path.join(dir, dirent.name);
if (dirent.isDirectory()) {
// eslint-disable-next-line no-await-in-loop
for await (const name of recursiveReaddir(joined)) {
yield name;
}
} else {
yield joined;
}
}
return Promise.resolve();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment