-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
54 lines (45 loc) · 1.17 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
#! /usr/bin/env node
import { exec, execSync } from "child_process";
import enquirer from "enquirer";
const { Select } = enquirer;
function parseBranchChoices(branches) {
const choices = branches.split("\n").filter((branch) => branch.trim() !== "");
const indexOfCurrentBranch = choices.findIndex((choice) =>
choice.includes("*"),
);
choices.splice(indexOfCurrentBranch, 1);
return choices;
}
function handleBranchSwitch(choices) {
const prompt = new Select({
message: "Switch to ..🏃♀️",
choices,
});
prompt
.run()
.then((answer) => {
try {
execSync(`git switch ${answer}`);
} catch {
console.log("Oops! Please check the Error ..🤔");
}
})
.catch((promptError) => {
console.error("Error with prompt...Please Select branch 🪵", promptError);
});
}
function main() {
exec("git branch --sort=-committerdate", (err, stdout) => {
if (err) {
console.log(err);
return;
}
const choices = parseBranchChoices(stdout);
if (choices.length === 0) {
console.log("No available branches to switch.");
return;
}
handleBranchSwitch(choices);
});
}
main();