mirror of
https://github.com/Tencent/BrowserSkill.git
synced 2026-09-10 13:01:23 +08:00
25 lines
686 B
JavaScript
25 lines
686 B
JavaScript
import { readdir } from "node:fs/promises";
|
|
import { join } from "node:path";
|
|
|
|
export async function discoverFiles(root, predicate) {
|
|
const files = [];
|
|
|
|
async function visit(directory) {
|
|
let entries;
|
|
try {
|
|
entries = await readdir(directory, { withFileTypes: true });
|
|
} catch (error) {
|
|
if (error?.code === "ENOENT") return;
|
|
throw error;
|
|
}
|
|
for (const entry of entries.sort((left, right) => left.name.localeCompare(right.name))) {
|
|
const path = join(directory, entry.name);
|
|
if (entry.isDirectory()) await visit(path);
|
|
else if (entry.isFile() && predicate(path)) files.push(path);
|
|
}
|
|
}
|
|
|
|
await visit(root);
|
|
return files;
|
|
}
|