-
Notifications
You must be signed in to change notification settings - Fork 0
/
clean.js
74 lines (48 loc) · 1.58 KB
/
clean.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
"use strict";
const { exec } = require("child_process");
let PATH;
PATH = "./dist";
// PATH = process.argv[2]; // tbh WAY too dangerous! just update THIS file if other places ever "look" somewhere else!
const ERROR_PREFIX = `ERROR during \`${PATH}\` clean`;
const actionsPipe = (fnErr, fnDone, ...actions) => {
actions = actions.flat().filter(Boolean);
if(!actions.length)return;
const action = actions.shift();
if("xyzDEBUG_LOG_EACH_ACTION" === "DEBUG_LOG_EACH_ACTION")console.log({action});
exec( action, (error, stdout, stderr) => {
if(error)return fnErr(
ERROR_PREFIX, "[error]\n", error.code || 1, error
);
if(stderr)return fnErr(
ERROR_PREFIX, "[stderr]\n", 1, stderr
);
if(stdout.length)console.log(stdout);
return (
actions.length
? actionsPipe(fnErr, fnDone, actions)
: fnDone(stdout)
);
} );
};
const bashTerminalCheck = () => {
return new Promise( (resolve, reject) => {
exec( "ls", (error, stdout, stderr) => resolve(!error) );
} );
};
bashTerminalCheck().then( isBash => {
if("xyzDEBUG_LOG_IS_BASH" === "DEBUG_LOG_IS_BASH")console.log({isBash}, "\n");
actionsPipe(
(errPrefix = "", errSuffix = "", errCode = 1, errMessage = "") => {
console.error(errPrefix + errSuffix, errMessage);
process.exit(errCode);
},
(stdout) => console.log(`Root of \`${PATH}\` is now clean`),
[
`IF NOT EXIST "${PATH}" ( mkdir "${PATH}" )`,
isBash
? `find "${PATH}" -maxdepth 1 -type f -delete`
: `DEL /Q "${PATH}/"`,
// "force a failure exit code",
]
);
} );