-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
117 lines (105 loc) · 2.52 KB
/
index.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
const inquirer = require("inquirer");
const git = require("simple-git")(process.cwd());
console.log("Running in ", process.cwd());
const BOOL_ANSWER = {
TRUE: "Yes",
FALSE: "No"
};
const TASKS = {
UNDO_COMMIT: "Undo a commit",
REVERT_FILE: "Revert file(s)"
};
const QUESTIONS = {
task: {
type: "list",
name: "task",
message: "What do you want to do?",
choices: getValues(TASKS)
},
isPushed: {
type: "list",
name: "is_pushed",
message: "Did you push it?",
choices: getValues(BOOL_ANSWER)
},
chooseFile: {
type: 'input',
name: 'filename',
message: "Which file?"
},
chooseBranch: {
type: 'input',
name: 'branchName',
message: "Which branch?"
},
};
const FUNCTIONS = {
undoLocalCommit: function(args) {
// console.log("undoLocalCommit");
// console.log(JSON.stringify(args, null, " "));
runGitCommand("reset --soft HEAD~")
.catch(e => {
console.log("User canceled, aborting");
})
},
undoPushedCommit: function(args) {
// console.log("undoPushedCommit");
// console.log(JSON.stringify(args, null, " "));
},
reverFileToBranch: function(args) {
// console.log("reverFileToBranch");
// console.log(JSON.stringify(args, null, ' '));
}
}
const CONDITIONS_TREE = {
questions: [QUESTIONS.task],
nextSteps: {
[TASKS.UNDO_COMMIT]: {
questions: [QUESTIONS.isPushed],
nextSteps: {
[BOOL_ANSWER.TRUE]: FUNCTIONS.undoPushedCommit,
[BOOL_ANSWER.FALSE]: FUNCTIONS.undoLocalCommit
}
},
[TASKS.REVERT_FILE]: {
questions: [QUESTIONS.chooseBranch, QUESTIONS.chooseFile]
}
}
}
function askQuestions(questionsTreeNode, results = {}) {
return inquirer.prompt(questionsTreeNode.questions).then(answers => {
results = Object.assign(results, answers);
const answer = results[questionsTreeNode.questions[0].name];
const nextStep = questionsTreeNode.nextSteps[answer];
if(typeof nextStep === "function") {
nextStep(results);
} else {
return askQuestions(nextStep, results);
}
});
}
function runGitCommand(command) {
const commandParts = command.split(" ");
console.log(" ")
console.log("Going to run the following command: ")
console.log(`git ${command}`);
console.log(" ");
return inquirer.prompt([{
type: "list",
name: "confirmation",
message: "Ok?",
choices: getValues(BOOL_ANSWER)
}]).then(answers => {
if(answers["confirmation"] === BOOL_ANSWER.TRUE) {
git.raw(command.split(" "));
} else {
throw "USER_CANCELED"
}
})
}
function getValues(dict) {
return Object.keys(dict).map(function(key) {
return dict[key];
});
}
askQuestions(CONDITIONS_TREE);