1+ const chalk = require ( 'chalk' ) ;
2+ const simpleGit = require ( 'simple-git/promise' ) ;
3+ const { execSync } = require ( 'child_process' ) ;
4+ const { readFileSync } = require ( 'fs' ) ;
5+ const path = require ( 'path' ) ;
6+
7+ const CHANGELOG_NAME = 'CHANGELOG.md' ;
8+ const CHANGELOG_PATH = path . join ( __dirname , '..' , CHANGELOG_NAME ) ;
9+ const CHANGELOG = readFileSync ( CHANGELOG_PATH , 'utf-8' ) ;
10+
11+ const cwd = process . cwd ( ) ;
12+ const git = simpleGit ( cwd ) ;
13+
14+ async function run ( ) {
15+ execSync ( `git pull` ) ;
16+
17+ const data = await git . tags ( ) ;
18+ const tags = data . all ;
19+ let tag = tags . reverse ( ) [ 0 ] ;
20+ console . log ( chalk . green ( `[Git Query] tag: ${ tag } ` ) ) ;
21+
22+ const tagChangelog = getChangelogTag ( CHANGELOG ) ;
23+ if ( tagChangelog && tag != tagChangelog ) {
24+ console . log ( chalk . yellow ( `[Git Action] Push new ${ tagChangelog } tag!` ) ) ;
25+ execSync ( `git tag ${ tagChangelog } ` ) ;
26+ execSync ( `git push origin ${ tagChangelog } :${ tagChangelog } ` ) ;
27+ execSync ( `git pull` ) ;
28+ tag = tagChangelog ;
29+ } else {
30+ console . log ( chalk . yellow ( '🙄 Please add new release changelog first.' ) ) ;
31+ console . log ( '' ) ;
32+ process . exit ( 1 ) ;
33+ }
34+
35+ const tagSimple = tag . startsWith ( 'v' ) ? tag . substring ( 0 , 2 ) : tag . substring ( 0 , 1 ) ;
36+ console . log ( chalk . green ( `[Git Query] tagSimple: ${ tagSimple } ` ) ) ;
37+
38+ if ( tags . includes ( tagSimple ) ) {
39+ console . log ( chalk . yellow ( `[Git Action] Delete ${ tagSimple } tag` ) ) ;
40+ execSync ( `git push origin :refs/tags/${ tagSimple } ` ) ;
41+ console . log ( chalk . green ( `[Git Action] Delete ${ tagSimple } tag success` ) ) ;
42+ }
43+
44+ console . log ( chalk . yellow ( `[Git Action] Add new simple ${ tagSimple } tag` ) ) ;
45+ execSync ( `git push origin ${ tag } :${ tagSimple } ` ) ;
46+ console . log ( chalk . green ( '🎉 Done!' ) ) ;
47+ }
48+
49+ function getChangelogTag ( content ) {
50+ const lines = content . split ( '\n' ) ;
51+ const pin = / ^ # # / ;
52+ let begin = false ;
53+ let tag = '' ;
54+
55+ for ( let i = 0 ; i < lines . length ; i += 1 ) {
56+ const line = lines [ i ] ;
57+ if ( begin && pin . test ( line ) ) {
58+ break ;
59+ }
60+ if ( ! begin ) {
61+ begin = pin . test ( line ) ;
62+ if ( begin ) {
63+ tag = line . substring ( 3 , line . length ) ;
64+ }
65+ }
66+ }
67+
68+ return tag . trim ( ) ;
69+ }
70+
71+ run ( ) ;
0 commit comments