23 lines
693 B
JavaScript
23 lines
693 B
JavaScript
import { readdir, stat } from 'node:fs/promises';
|
|
import path from 'node:path';
|
|
|
|
// Recursively lists every file under entryPath. A path that is itself a file
|
|
// resolves to a single-element list, so callers can mix files and directories.
|
|
export async function walk(entryPath) {
|
|
const entryStat = await stat(entryPath);
|
|
if (entryStat.isFile()) return [entryPath];
|
|
|
|
const entries = await readdir(entryPath, { withFileTypes: true });
|
|
const files = [];
|
|
|
|
for (const entry of entries) {
|
|
const fullPath = path.join(entryPath, entry.name);
|
|
if (entry.isDirectory()) {
|
|
files.push(...(await walk(fullPath)));
|
|
} else {
|
|
files.push(fullPath);
|
|
}
|
|
}
|
|
|
|
return files;
|
|
}
|