feat(utils): add directory file fetching functionality

This commit is contained in:
xtrullor73
2024-04-21 22:42:41 -07:00
parent 25e2dcc143
commit 6e932c3759

View File

@@ -1,18 +1,25 @@
import fs from 'fs/promises'; import fs from 'fs/promises';
import path from 'path'; import path from 'path';
export const enumerateFilesInDirectory = async (folderPath, callback) => { export default async function directoryFileFetcher(folderPath) {
try { async function enumerateFilesInDirectory(dirPath) {
const entries = await fs.readdir(folderPath, { withFileTypes: true }); let fileList = [];
const entries = await fs.readdir(dirPath, { withFileTypes: true });
for (const entry of entries) { for (const entry of entries) {
const fullPath = path.join(folderPath, entry.name); const fullPath = path.join(dirPath, entry.name);
if (entry.isDirectory()) { if (entry.isDirectory()) {
await enumerateFilesInDirectory(fullPath, callback); fileList = fileList.concat(await enumerateFilesInDirectory(fullPath));
} else { } else {
callback(fullPath); fileList.push(fullPath);
} }
} }
return fileList;
}
try {
return await enumerateFilesInDirectory(folderPath); // return array of files
} catch (e) { } catch (e) {
console.error(`Error enumerating files in directory ${folderPath}: ${e}`); console.error(`Error enumerating files in directory ${folderPath}:`, e);
throw e;
} }
}; }