@@ -119,48 +119,83 @@ export async function getChangedPackageNames(opts: ReleaseOptions) {
119119 * @returns
120120 */
121121export function clearTagVersion ( tag : string ) {
122- return tag . replace ( / ^ v | ^ @ . + ? @ | \w . + ? @ / , '' ) ;
122+ return tag . replace ( / ( . + ( _ | - | @ ) ) / , '' ) . replace ( 'v' , '' ) ;
123123}
124124
125- export function getGitTagVersion ( name : string , version : string , opts : ReleaseOptions ) {
125+ function getGitTagPrefixLegacy ( name : string , opts : ReleaseOptions ) {
126126 const { isMonorepo, scopedTag } = opts ;
127-
128127 if ( isMonorepo ) {
129128 const names = name . split ( '/' ) ;
130129 const pre = scopedTag ? name : names [ names . length - 1 ] ;
131- return `${ pre } @${ version } ` ;
130+ return `${ pre } @` ;
131+ }
132+ return 'v' ;
133+ }
134+
135+ export function getGitTagPrefix ( name : string , opts : ReleaseOptions ) {
136+ const { isMonorepo, scopedTag } = opts ;
137+ if ( isMonorepo ) {
138+ const names = name . split ( '/' ) ;
139+ const pre = scopedTag ? name . replace ( '@' , '' ) . replace ( '/' , '-' ) : names [ names . length - 1 ] ;
140+ return `${ pre } -v` ;
132141 }
133- return `v${ version } ` ;
142+ return 'v' ;
143+ }
144+
145+ export function getGitTagVersion ( name : string , version : string , opts : ReleaseOptions ) {
146+ return getGitTagPrefix ( name , opts ) + version ;
134147}
135148
136149/**
137150 * Get the latest tag of the packages.
138151 * @param pkgNames only include package names
139152 * @returns
140153 */
141- export async function getGitTags ( pkgNames ?: string [ ] ) {
142- pkgNames ||= [ ] ;
154+ export async function getGitTags ( opts : ReleaseOptions ) {
155+ const { isMonorepo, pkgs } = opts ;
156+ const pkgNames = isMonorepo ? pkgs . map ( s => s . name ) : [ ] ;
157+
158+ // compatible with old version
159+ const prefixMap : Record < string , string > = { } ;
160+ if ( isMonorepo ) {
161+ pkgNames . forEach ( name => {
162+ prefixMap [ getGitTagPrefixLegacy ( name , { isMonorepo, scopedTag : true } as ReleaseOptions ) ] =
163+ name ;
164+ prefixMap [ getGitTagPrefixLegacy ( name , { isMonorepo, scopedTag : false } as ReleaseOptions ) ] =
165+ name ;
166+ prefixMap [ getGitTagPrefix ( name , { isMonorepo, scopedTag : true } as ReleaseOptions ) ] = name ;
167+ prefixMap [ getGitTagPrefix ( name , { isMonorepo, scopedTag : false } as ReleaseOptions ) ] = name ;
168+ } ) ;
169+ }
170+ const prefixKeys = Object . keys ( prefixMap ) ;
143171
144172 const records = await run (
145173 `git for-each-ref --format="%(refname:short) %(creatordate)" refs/tags` ,
146174 ) ;
147175 const map : Record < string , GitTagInfo [ ] > = { } ;
148- const add = ( name : string , record : string ) => {
176+ const add = ( record : string ) => {
149177 const [ tag , ...times ] = record . split ( ' ' ) ;
150- let pkgName = name ;
151- if ( name !== '_' ) {
152- if ( ! pkgNames . includes ( name ) ) {
153- const n = pkgNames . find ( s => s . endsWith ( `/${ name } ` ) ) ;
154- if ( n ) {
155- pkgName = n ;
156- }
178+ const version = clearTagVersion ( tag ) ;
179+
180+ let pkgName = '' ;
181+
182+ // single package
183+ if ( tag === `v${ version } ` || tag === version ) {
184+ pkgName = '_' ;
185+ } else {
186+ const prefix = prefixKeys . find ( pre => tag === `${ pre } ${ version } ` ) ;
187+ if ( prefix ) {
188+ pkgName = prefixMap [ prefix ] ;
157189 }
158190 }
191+ if ( ! pkgName ) {
192+ return ;
193+ }
159194
160195 map [ pkgName ] = ( map [ pkgName ] || [ ] ) . concat ( [
161196 {
162197 name : tag ,
163- version : clearTagVersion ( tag ) ,
198+ version,
164199 time : dayjs ( times . join ( ' ' ) ) . format ( 'YYYY-MM-DD' ) ,
165200 } ,
166201 ] ) ;
@@ -170,12 +205,7 @@ export async function getGitTags(pkgNames?: string[]) {
170205 if ( ! record ) {
171206 return ;
172207 }
173- if ( record . includes ( '@' ) ) {
174- const i = record . lastIndexOf ( '@' ) ;
175- add ( record . substring ( 0 , i ) , record ) ;
176- } else {
177- add ( '_' , record ) ;
178- }
208+ add ( record ) ;
179209 } ) ;
180210
181211 Object . keys ( map ) . forEach ( name => {
0 commit comments