-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathindex.js
48 lines (40 loc) · 1.09 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
#!/usr/bin/env node
const { promisify } = require('util')
const exec = promisify(require('child_process').exec)
const prompts = require('prompts')
async function run () {
const { stdout: branches } = await exec('git branch -v --sort=-committerdate')
const choices = branches
.split(/\n/)
.filter(branch => !!branch.trim())
.map(branch => {
const [, flag, value, hint] = branch.match(/([* ]) +([^ ]+) +(.+)/)
return { value, hint, disabled: flag === '*' }
})
const { branch } = await prompts({
type: 'select',
name: 'branch',
message: 'Switch branch',
choices,
hint: choices[0].hint,
warn: 'current branch',
onState ({ value }) {
this.hint = choices.find(c => c.value === value).hint
}
})
await checkout(branch)
}
async function checkout (branch) {
if (!branch) return
const { stdout, stderr } = await exec(`git checkout ${branch}`)
process.stdout.write(stdout)
process.stderr.write(stderr)
}
function onError (e) {
if (e.stderr) {
process.stderr.write(e.stderr)
} else {
console.error(e)
}
}
run().catch(onError)