Created
August 26, 2022 10:21
-
-
Save tksst/4d9afa5cadd262ecf7ba758706fddacf to your computer and use it in GitHub Desktop.
recursiveReaddir.ts
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 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