-
Notifications
You must be signed in to change notification settings - Fork 1
/
DeleteFiles.js
44 lines (40 loc) · 1.21 KB
/
DeleteFiles.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import fs from "fs/promises";
/**
* A utility class for deleting files and directories.
*/
export default class DeleteFiles {
/**
* Array to store paths of files and directories to be deleted.
* @type {string[]}
*/
filesToDelete = [];
/**
* Deletes a directory at the specified path.
* @param {string} path - The path of the directory to be deleted.
* @return {Promise<void>} - A Promise that resolves when the directory is deleted.
*/
async deleteDirectory(path) {
try {
await fs.rm(path, { recursive: true });
} catch (err) {
console.error(`Error deleting "${path}" : ${err.message}`);
}
}
/**
* Deletes all files and directories stored in the filesToDelete array.
* @return {Promise<void>} - A Promise that resolves when all files and directories are deleted.
*/
async deleteAllFiles() {
const deletePromises = this.filesToDelete.map((path) =>
this.deleteDirectory(path),
);
await Promise.all(deletePromises);
}
/**
* Adds a file or directory path to the list of items to be deleted.
* @param {string} path - The path of the file or directory to be deleted.
*/
addFileToDelete(path) {
this.filesToDelete.push(path);
}
}