-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
44 lines (35 loc) · 1.18 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
import {spawnSync, execSync} from 'child_process'
import cliSelect from 'cli-select-2'
import chalk from 'chalk';
const recent_branches_cmd = `
git reflog | grep -o "checkout: moving from .* to " | sed -e 's/checkout: moving from //' -e 's/ to $//' | head -40
`
const runCmd = (cmd) => {
const result = spawnSync(cmd, [], {shell: true, encoding: 'utf8'})
if (result.stderr) {
console.error(result.stderr)
process.exit(1)
}
if (result.status !== 0) {
console.error(`Command FAILED with status ${result.status}, no stderr: ${cmd}`)
process.exit(1)
}
return result.stdout
}
const run = async () => {
const branches_cmd_output = runCmd(recent_branches_cmd, {shell: true, encoding: 'utf8'})
const branches = [...new Set(
branches_cmd_output.split('\n').filter(branch => branch.trim() !== '')
)]
const selected = await cliSelect({
selected: '',
unselected: '',
values: branches,
valueRenderer: (value, selected) => selected ? chalk.inverse(value) : value
})
try {
execSync(`git checkout ${selected.value}`, {stdio: 'inherit'})
} catch(e) {
}
}
export { run }