-
Notifications
You must be signed in to change notification settings - Fork 0
/
execute.js
49 lines (36 loc) · 1.11 KB
/
execute.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
const exec = require("child_process").exec;
const { question } = require("readline-sync");
function _execute(commands) {
const command = commands.join(" && ");
exec(command, (error, stdout, stderr) => {
if (error) {
console.error(`There was an error: ${error.toString()}\n`);
}
console.log("\n " + stdout.replace(/\n/g, "\n "));
if (stderr.length > 0) {
console.error("\n " + stderr.replace(/\n/g, "\n "));
}
});
}
function execute(commands, confirm = true) {
console.log("\nThese are the commands we've generated: \n");
for (let command of commands) {
console.log(` ${command}`);
}
console.log("");
const answer = commands ? question("Should we execute these commands? [Y/n] ").toLowerCase() : 'y';
const choice = answer.length == 0 ? 'y' : answer[0];
switch(choice) {
case 'y':
console.log("Executing...");
_execute(commands);
return;
case 'n':
console.log("Cancelling...");
break;
default:
console.log(`We couldn't understand "${answer}". Cancelling...`);
break;
}
}
module.exports = { execute };