1 | const pro = require("util").promisify;
|
2 | const fs = require("fs");
|
3 |
|
4 | async function rmDir(dir, contentOnly = false) {
|
5 | try {
|
6 |
|
7 | let files = await pro(fs.readdir)(dir);
|
8 | for (let file of files) {
|
9 | let filePath = dir + "/" + file;
|
10 | let stat = await pro(fs.stat)(filePath);
|
11 | if (stat.isDirectory()) {
|
12 | await rmDir(filePath);
|
13 | } else {
|
14 | await pro(fs.unlink)(filePath);
|
15 | }
|
16 | }
|
17 |
|
18 | if (!contentOnly) {
|
19 | await pro(fs.rmdir)(dir);
|
20 | }
|
21 |
|
22 | } catch (e) {
|
23 | if (e.code !== "ENOENT") {
|
24 | throw e;
|
25 | }
|
26 | }
|
27 | }
|
28 |
|
29 | module.exports = rmDir; |
\ | No newline at end of file |