feat: add cleanup function to delete images directory after execution

This commit is contained in:
xtrullor73
2024-05-27 17:20:42 -07:00
parent 56dbb5df31
commit 0e28042d84
3 changed files with 66 additions and 23 deletions

View File

@@ -0,0 +1,24 @@
import { promises as fs } from 'fs';
import path from 'path';
/**
* Recursively deletes a directory and its contents.
* @param {string} dirPath - The path to the directory to delete.
*/
async function deleteDirectoryRecursively(dirPath) {
const entries = await fs.readdir(dirPath, { withFileTypes: true });
const promises = entries.map((entry) => {
const fullPath = path.join(dirPath, entry.name);
return entry.isDirectory()
? deleteDirectoryRecursively(fullPath)
: fs.unlink(fullPath);
});
await Promise.all(promises);
await fs.rmdir(dirPath);
}
export default deleteDirectoryRecursively;