-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcli.js
executable file
·47 lines (38 loc) · 992 Bytes
/
cli.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
#!/usr/bin/env node
'use strict';
const inquirer = require('inquirer')
const git = require('simple-git/promise')()
function validate(summary) {
const { all, current } = summary
return (!all || all === 0)
? Promise.reject('[branch-checkout] No branches found')
: { branches: all, current }
}
function parse(summary) {
const { branches, current } = summary
return branches.filter(branch => branch !== current)
}
function format(branches) {
return branches.reduce((list, name) => [...list, { name }], [])
}
function ask(choices) {
return inquirer.prompt([{
type: 'list',
name: 'branch',
message: '[branch-checkout] Select the branch you want to checkout:',
choices,
}])
}
function checkout(answer) {
const { branch } = answer
console.log('[branch-checkout] The current branch is now: ' + branch)
git.checkout(branch)
}
git
.branchLocal()
.then(validate)
.then(parse)
.then(format)
.then(ask)
.then(checkout)
.catch(console.log)