2023-02-03 14:05:02 +01:00
|
|
|
import { readdir } from "fs/promises";
|
|
|
|
import { resolve } from "path";
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Asynchronously and recursively walk a directory tree, yielding every file and directory
|
|
|
|
* found
|
|
|
|
*
|
|
|
|
* @param root the starting directory
|
|
|
|
* @returns AsyncGenerator
|
|
|
|
*/
|
2023-04-02 22:47:42 +02:00
|
|
|
export default async function* walk(root: string): AsyncGenerator<string, void, unknown> {
|
2023-02-03 14:05:02 +01:00
|
|
|
for (const entry of await readdir(root, { withFileTypes: true })) {
|
|
|
|
const absolutePath = resolve(root, entry.name);
|
|
|
|
if (entry.isDirectory()) {
|
|
|
|
yield absolutePath;
|
|
|
|
yield* walk(absolutePath);
|
|
|
|
} else yield absolutePath;
|
|
|
|
}
|
|
|
|
}
|