-
Notifications
You must be signed in to change notification settings - Fork 0
/
gcb.js
executable file
·45 lines (40 loc) · 1.17 KB
/
gcb.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
#!/usr/bin/env node
const { exec } = require("child_process");
const inquirer = require("inquirer");
exec("git branch", (error, stdout, stderr) => {
if (error) {
console.error(error);
return;
}
if (stderr) {
console.error("stderr:", stderr);
}
const replaceStars = stdout.replace("*", ""); // replace * with an empty string
const branchesArray = replaceStars.split(" "); // this will finally display /n
const newBranch = branchesArray.filter((t) => t !== "*" && t !== ""); // here we remove * and ""
const filteredNewBranch = newBranch.map((value) => {
return value.replace(/(\n$)/gm, "");
});
inquirer
.prompt([
{
type: "list",
name: "branches",
message: "which branch do you want to checkout",
choices: filteredNewBranch,
},
])
.then((answers) =>
exec(`git checkout ${answers.branches}`, (error, stdout, stderr) => {
if (error) {
console.log("sorry, there was an error");
}
if (stderr) {
console.error(stderr);
}
if (stdout.length > 0) {
console.log("Successful - you are on ", answers.branches);
}
})
);
});