-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
86 lines (77 loc) · 2.44 KB
/
index.ts
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
75
76
77
78
79
80
81
82
83
84
85
86
#!/usr/bin/env node
import program from "commander";
import inquirer from "inquirer";
import shell from "shelljs";
import fs from "fs";
// Types
type AnswersType = { consent: string };
// Message if no path is passed
const NO_PATH_MESSAGE: string =
"Please pass the path of the folder/file you want to delete as an argument.";
// CLI version
program.version(
"0.0.1",
"-v, --version",
"outputs the current version of burn"
);
// CLI Options
program
.option("-r, --recursive", "recursively delete a folder and its contents")
.option("-p, --prompt", "Set 'Are you sure' prompt to on or off");
/*
Parses the passed arguments and stores them in program.args (regular arguments passed) and program.opts() (options passed)
*/
program.parse(process.argv);
// Main function
(function () {
// Path constant
const MAIN_ARG: string = program.args[0];
// Path to the config file; .meltrc.json
const pathToConfig: string =
__dirname.slice(0, __dirname.length - 6) + "/.meltrc.json";
// Checks if path argument is passed or not
if (program.args.length === 0) {
return console.log(NO_PATH_MESSAGE);
}
if (program.prompt) {
let temp: boolean;
if (MAIN_ARG.toLowerCase() === "off") {
temp = false;
} else if (MAIN_ARG.toLowerCase() === "on") {
temp = true;
} else {
return console.log("Incorrect argument passed.");
}
fs.writeFileSync(pathToConfig, JSON.stringify({ prompt: temp }));
return console.log(`Prompt set to ${MAIN_ARG.toLowerCase()}.`);
}
if (program.recursive) {
let promptFlag: boolean = true;
if (fs.existsSync(pathToConfig)) {
const data = fs.readFileSync(pathToConfig);
const { prompt } = JSON.parse(data.toString());
promptFlag = prompt;
}
if (promptFlag) {
inquirer
.prompt([
{
name: "consent",
message:
"This will delete the folder and all its contents. Are you sure you want to proceed(Y/n)?\n",
},
])
.then((answers: AnswersType) => {
if (answers.consent[0].toLowerCase() === "y") {
return shell.rm("-rf", MAIN_ARG);
} else if (answers.consent[0].toLowerCase() === "n") {
return console.log("Folder not deleted.");
}
});
} else {
return shell.rm("-rf", MAIN_ARG);
}
} else {
return shell.rm(MAIN_ARG);
}
})();