-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
57 lines (50 loc) · 1.14 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
#!/usr/bin/env node
const { Select } = require("enquirer");
const { exec } = require("child_process");
const { stderr, stdout } = require("process");
// Get list of recent branches
exec(
"git for-each-ref --sort=-committerdate --count=10 --format='%(refname:short)' refs/heads/",
(error, stdout, stderr) => {
if (error) {
console.log(`Recent branches error: ${error.message}`);
return;
}
if (stderr) {
console.log(`Recent branches stderr: ${stderr}`);
return;
}
outputList(stdout);
}
);
// Output select
function outputList(stdout) {
let branches = stdout.split("\n");
branches = branches.filter(branch => {
return branch.trim();
});
const prompt = new Select({
name: "branch",
message: "Check out a branch",
choices: branches
});
prompt
.run()
.then(checkOutBranch)
.catch(console.error);
}
// Check out selected branch
function checkOutBranch(answer) {
exec("git checkout " + answer),
(error, stdout, stderr) => {
if (error) {
console.log(`Checkout error: ${error.message}`);
return;
}
if (stderr) {
console.log(`Checkout stderr: ${stderr}`);
return;
}
}
process.stdout.write('\x1Bc');
}