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