@@ -4,12 +4,14 @@ const fs = require('fs')
44const spawn = require ( 'cross-spawn' )
55const parseArgs = require ( '../utils/parseArgs' )
66const logger = require ( '../utils/logger' )
7+ const matcher = require ( 'multimatch' )
78
89module . exports = class Core {
910 constructor ( ) {
1011 this . cwd = process . cwd ( )
1112 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' )
1315
1416 this . isGitProject ( )
1517
@@ -18,9 +20,19 @@ module.exports = class Core {
1820
1921 initCli ( ) {
2022 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+ }
2436
2537 cli . version ( require ( '../package.json' ) . version ) . help ( )
2638
@@ -35,9 +47,14 @@ module.exports = class Core {
3547 return true
3648 }
3749
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
4158 . toString ( )
4259 . trimRight ( )
4360 . split ( '\n' )
@@ -46,19 +63,33 @@ module.exports = class Core {
4663 return b . trim ( )
4764 }
4865 } )
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 )
5076
5177 return branch
5278 }
5379
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 )
5782
5883 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 )
6088 if ( ps . status === 0 ) {
61- logger . success ( 'Deleted branch' , `\`${ branch } \`` )
89+ logger . success (
90+ `Deleted${ options . remotes ? ' remote' : '' } branch` ,
91+ `\`${ branch } \``
92+ )
6293 }
6394 } )
6495 }
0 commit comments