-
Notifications
You must be signed in to change notification settings - Fork 0
/
clean.mjs
36 lines (31 loc) · 979 Bytes
/
clean.mjs
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
/* eslint-disable no-console */
import fs from "fs";
const { stat, rmdir } = fs.promises;
const dirs = process.argv.slice(2);
const statResults = [];
// Check that each dir exists. An error will be thrown for any dir that doesn't exist, but we need
// all the promises to be resolved for Promise.all, so we must intercept the error.
dirs.forEach((dir) => {
statResults.push(
stat(dir)
.then((dirStat) => ({ dir, stat: dirStat }))
.catch(() => null)
);
});
// Remove the dirs that exist and are directories
Promise.all(statResults)
.then((stats) => stats.filter((dirStat) => dirStat && dirStat.stat.isDirectory()))
.then((existingDirStats) => {
const rmdirResults = [];
existingDirStats.forEach(({ dir }) => {
rmdirResults.push(rmdir(dir, { recursive: true }));
});
return Promise.all(rmdirResults);
})
.then(() => {
process.exit();
})
.catch((reason) => {
console.log(reason.message);
process.exit(1);
});