mirror of
https://github.com/xtrll/MusicMetaFinder.git
synced 2026-04-18 00:03:28 +02:00
feat(utils): add directory file fetching functionality
This commit is contained in:
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user