1+ import { command , CommandArguments , CommandConfig , Dispatcher , inject , InputHelper , Log , option , OutputHelper } from "@radic/console" ;
2+ import { RConfig } from "../../" ;
3+ import { ConnectHelper } from "../../helpers/helper.connect" ;
4+ import { IGitService } from "../../services/service.git" ;
5+ import { execSync } from "child_process" ;
6+ import { BaseCommand } from "../../core/commands" ;
7+
8+ @command ( `tag
9+ {version/bump-type:string@which version}
10+ [message:string[]@message]` , 'Git tag with bumping' , < CommandConfig > {
11+ onMissingArgument : 'help' ,
12+ example : `{bold}tag by passing the name{/bold}
13+ r git tag 1.2.3
14+ r git tag v1.44.0-alpha.5
15+ r git tag foobar
16+
17+ {bold}tag using version bump. valid bump-types: major, minor, patch, build{/bold}
18+ r git tag <bump-type>
19+ r git tag minor
20+
21+ {bold}tag using a bump-type as name{/bold}
22+ r git tag minor -n
23+ r git tag minor --no-bump
24+ `
25+ } )
26+ export class GitTagCmd extends BaseCommand {
27+ showHelp : ( ) => void
28+
29+ @inject ( 'cli.helpers.output' )
30+ out : OutputHelper ;
31+
32+ @inject ( 'cli.helpers.input' )
33+ ask : InputHelper ;
34+
35+ @inject ( 'cli.helpers.connect' )
36+ connect : ConnectHelper
37+
38+ @inject ( 'r.log' )
39+ log : Log ;
40+
41+ @inject ( 'r.config' )
42+ config : RConfig
43+
44+ @inject ( 'cli.events' )
45+ events : Dispatcher ;
46+
47+ @option ( 'n' , 'Use this if you want to tag `bump type` names like "major", "minor", etc' )
48+ noBump : boolean
49+
50+ @option ( 'p' , 'Disables pushing' )
51+ noPush : boolean
52+
53+ bumpTypes :string [ ] = [ 'major' , 'minor' , 'patch' , 'build' ]
54+
55+ async handle ( args : CommandArguments , ...argv : any [ ] ) {
56+ this . out . dump ( { args} )
57+
58+ if ( ! args . version ) {
59+ return this . returnError ( 'No version or bump-type specified.' )
60+ }
61+
62+ if ( false == this . bumpTypes . includes ( args . version ) || this . noBump ) {
63+ let msg = args . message && args . message . length > 0 ? args . message . join ( ' ' ) : 'tagged ' + args . version ;
64+ msg = `-m "${ msg } "`
65+ this . log . verbose ( execSync ( `git tag -a ${ args . version } ${ msg } ` ) . toString ( 'utf8' ) )
66+ if ( this . noPush === false ) {
67+ this . log . verbose ( execSync ( `git push -u origin ${ args . version } ` ) . toString ( ) )
68+ }
69+ return this . returnOk ( `Tagged ${ args . version } and pushed it` )
70+ }
71+
72+ let tags = execSync ( 'git tag' ) . toString ( ) ;
73+ let lastTag = process . argv [ 2 ] || tags . split ( '\n' ) . reverse ( ) . filter ( ( val ) => val !== '' ) [ 0 ] ;
74+ let seg = this . config ( 'commands.git.tag.regexp' ) . exec ( lastTag ) ;
75+ if ( seg === null ) throw new Error ( 'Could not parse last tag' )
76+ let prefixed = seg [ 2 ] === undefined // prefixed with "v"
77+ let suffixed = seg [ 4 ] === undefined // suffixed with "-<suffix>.<number>"
78+ let parts :any = {
79+ major : prefixed ? seg [ 1 ] . substr ( 1 ) : seg [ 2 ] ,
80+ minor : prefixed ? seg [ 3 ] : seg [ 3 ] ,
81+ patch : suffixed ? seg [ 5 ] : seg [ 4 ] ,
82+ suffix : seg [ 6 ] || null ,
83+ build : seg [ 7 ] || null
84+ }
85+ let partsNumKeys = Object . keys ( parts ) . filter ( key => key !== 'suffix' ) ;
86+ partsNumKeys . forEach ( key => parts [ key ] = parseInt ( parts [ key ] ) ) ;
87+ console . dir ( parts ) ;
88+ console . dir ( { prefixed, suffixed, seg} ) ;
89+ parts [ args [ 'bump-type' ] ] ++ ;
90+ partsNumKeys . slice ( partsNumKeys . indexOf ( args [ 'bump-type' ] ) + 1 ) . forEach ( key => parts [ key ] = 0 )
91+ parts = Object . values ( parts ) ;
92+ let version = [ ( prefixed ? seg [ 1 ] [ 0 ] : '' ) + parts [ 0 ] , parts [ 1 ] , parts [ 2 ] ] . join ( '.' ) . concat ( suffixed ? '-' + [ parts [ 3 ] , parts [ 4 ] ] . join ( '.' ) : '' )
93+ console . log ( { parts, version} ) ;
94+
95+ }
96+
97+
98+ }
99+ export default GitTagCmd
0 commit comments