-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
executable file
·48 lines (44 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
45
46
47
48
#!/usr/bin/env node
const inquirer = require('inquirer')
const shell = require('shelljs')
const gitBranchOutput = shell.exec('git branch', {silent: true})
const branchStrings = gitBranchOutput.stdout.split('\n').map(branch => branch.slice(2))
const branches = branchStrings.filter(branch => branch !== '')
function checkoutBranch (branchSelection) {
if (shell.exec(`git checkout ${branchSelection}`).code !== 0) {
shell.exit(1)
}
}
if (gitBranchOutput.stderr !== '') {
console.log(gitBranchOutput.stderr)
process.exit(0)
} else if (branches.length === 0) {
console.log('No branches\n')
process.exit(0)
} else if (branches.length === 1) {
console.log('Only one branch\n')
process.exit(0)
}
inquirer.prompt([
{
type: 'list',
name: 'selection',
message: 'Which branch do you want to checkout?',
choices: branches
}
]).then(choice => {
let branchSelection = choice.selection
inquirer.prompt([
{
type: 'confirm',
name: 'confirm',
message: `Checkout ${branchSelection}?`,
default: true
}
]).then(selection => {
if (!selection.confirm) {
return process.exit(0)
}
module.exports = checkoutBranch(branchSelection)
})
})