fix: use for loop in fs operations

This commit is contained in:
Mohamed Marrouchi 2025-01-08 17:32:58 +01:00
parent 282b69f4a3
commit 30202bac79

View File

@ -48,22 +48,19 @@ export async function moveFiles(
const files = await fs.promises.readdir(sourceFolder); const files = await fs.promises.readdir(sourceFolder);
// Filter only files (skip directories) // Filter only files (skip directories)
const filePaths = await Promise.all( const filePaths = [];
files.map(async (file) => { for (const file of files) {
const filePath = join(sourceFolder, file); const filePath = join(sourceFolder, file);
const stat = await fs.promises.stat(filePath); const stat = await fs.promises.stat(filePath);
return stat.isFile() ? filePath : null; if (stat.isFile()) {
}), filePaths.push(filePath);
); }
}
// Move each file to the destination folder // Move each file to the destination folder
const movePromises = filePaths for (const filePath of filePaths) {
.filter((filePath): filePath is string => filePath !== null) const fileName = basename(filePath);
.map((filePath) => { const destination = resolve(join(destinationFolder, fileName));
const fileName = basename(filePath); await moveFile(filePath, destination, overwrite);
const destination = resolve(join(destinationFolder, fileName)); }
return moveFile(filePath, destination, overwrite);
});
await Promise.all(movePromises);
} }