-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcli.js
executable file
·82 lines (72 loc) · 2.07 KB
/
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#!/usr/bin/env node
const chalk = require('chalk')
const inquirer = require('inquirer')
const git = require('simple-git/promise')
function startSpinner(text) {
const interval = 80
const frames = ['⠋', '⠙', '⠹', '⠸', '⠼', '⠴', '⠦', '⠧', '⠇', '⠏']
let frame = 0
const showCursor = () => process.stderr.write('\u001b[?25h')
const hideCursor = () => process.stderr.write('\u001b[?25l')
const clearSpinner = () => {
process.stderr.clearLine()
process.stderr.cursorTo(0)
}
hideCursor()
const spinnerId = setInterval(() => {
clearSpinner()
process.stderr.write(`${frames[frame]} ${text}`)
if (++frame === frames.length) frame = 0
}, interval)
return () => {
clearInterval(spinnerId)
clearSpinner()
showCursor()
}
}
;(async () => {
const repo = git()
const stopSpinner = startSpinner('fetching...')
await repo.fetch(['--all', '-p'])
const { branches } = await repo.branch()
stopSpinner()
const choices = Object.values(branches)
.map(b => b.name)
.map(name => ({ name, value: name }))
const { willDeleteBranches } = await inquirer.prompt([
{
type: 'checkbox',
name: 'willDeleteBranches',
message: 'Which branches you want to delete?',
choices
}
])
if (!willDeleteBranches.length) return
const { didConfirm } = await inquirer.prompt([
{
type: 'confirm',
name: 'didConfirm',
message: 'Are you sure?',
default: false
}
])
if (!didConfirm) return
for (let branch of willDeleteBranches) {
const stopSpinner = startSpinner('deleting...')
try {
const matches = /^remotes\/([^/]+)\/(.*)/.exec(branch)
if (!matches) {
await repo.branch(['-D', branch])
} else {
const [, remoteName, localName] = matches
await repo.push(remoteName, localName, { '--delete': null })
}
stopSpinner()
console.log(`${branch} ${chalk.green.bold('Success')}`)
} catch (error) {
stopSpinner()
console.log(`${branch} ${chalk.red.bold(`${error}`)}`)
}
}
console.log(chalk.cyan('Done!'))
})()