@@ -4,12 +4,14 @@ const fs = require('fs')
4
4
const spawn = require ( 'cross-spawn' )
5
5
const parseArgs = require ( '../utils/parseArgs' )
6
6
const logger = require ( '../utils/logger' )
7
+ const matcher = require ( 'multimatch' )
7
8
8
9
module . exports = class Core {
9
10
constructor ( ) {
10
11
this . cwd = process . cwd ( )
11
12
this . rawArgs = process . argv
12
- this . args = parseArgs ( this . rawArgs . slice ( 2 ) )
13
+ this . args = parseArgs ( this . rawArgs )
14
+ this . isRemotes = this . args . has ( 'r' ) || this . args . has ( 'remotes' )
13
15
14
16
this . isGitProject ( )
15
17
@@ -18,9 +20,19 @@ module.exports = class Core {
18
20
19
21
initCli ( ) {
20
22
const cli = ( this . cli = cac ( ) )
21
- this . command = cli . command ( '[...branches]' ) . action ( branches => {
22
- this . deleteBranch ( branches )
23
- } )
23
+ this . command = cli
24
+ . command ( '[...branches]' )
25
+ . usage ( '[...branches] [options]' )
26
+ . option ( '-r, --remotes' , 'Delete remotes branches' )
27
+ . action ( ( branches , options ) => {
28
+ this . deleteBranch ( branches , options )
29
+ } )
30
+
31
+ if ( this . isRemotes ) {
32
+ cli . option ( '--scope' , 'Remote branch scope' , {
33
+ default : 'origin'
34
+ } )
35
+ }
24
36
25
37
cli . version ( require ( '../package.json' ) . version ) . help ( )
26
38
@@ -35,9 +47,14 @@ module.exports = class Core {
35
47
return true
36
48
}
37
49
38
- getBranch ( ) {
39
- const { stdout } = spawn . sync ( 'git' , [ 'branch' ] )
40
- const branch = stdout
50
+ getBranch ( options ) {
51
+ const { stdout } = spawn . sync (
52
+ 'git' ,
53
+ [ 'branch' ] . concat ( this . isRemotes ? [ '-r' ] : [ ] )
54
+ )
55
+ let branch = [ ]
56
+
57
+ branch = stdout
41
58
. toString ( )
42
59
. trimRight ( )
43
60
. split ( '\n' )
@@ -46,19 +63,33 @@ module.exports = class Core {
46
63
return b . trim ( )
47
64
}
48
65
} )
49
- . filter ( Boolean )
66
+
67
+ if ( options . scope ) {
68
+ branch = matcher ( branch , [ `${ options . scope } /**` ] ) . map ( b => {
69
+ if ( ! b . includes ( '->' ) ) {
70
+ return b . replace ( `${ options . scope } /` , '' )
71
+ }
72
+ } )
73
+ }
74
+
75
+ branch = branch . filter ( Boolean )
50
76
51
77
return branch
52
78
}
53
79
54
- deleteBranch ( branches ) {
55
- const match = require ( 'multimatch' )
56
- const matched = match ( this . getBranch ( ) , branches )
80
+ deleteBranch ( branches , options ) {
81
+ const matched = matcher ( this . getBranch ( options ) , branches )
57
82
58
83
matched . forEach ( branch => {
59
- const ps = spawn . sync ( 'git' , [ 'branch' , branch , '-D' ] )
84
+ const args = options . remotes
85
+ ? [ 'push' , options . scope , `:${ branch } ` ]
86
+ : [ 'branch' , branch , '-D' ]
87
+ const ps = spawn . sync ( 'git' , args )
60
88
if ( ps . status === 0 ) {
61
- logger . success ( 'Deleted branch' , `\`${ branch } \`` )
89
+ logger . success (
90
+ `Deleted${ options . remotes ? ' remote' : '' } branch` ,
91
+ `\`${ branch } \``
92
+ )
62
93
}
63
94
} )
64
95
}
0 commit comments