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 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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (e) {
|
return fileList;
|
||||||
console.error(`Error enumerating files in directory ${folderPath}: ${e}`);
|
|
||||||
}
|
}
|
||||||
};
|
|
||||||
|
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