11#!/usr/bin/env node
22const { execSync } = require ( 'child_process' ) ;
3+ const readline = require ( 'readline' ) ;
34const path = require ( 'path' ) ;
45const fs = require ( 'fs' ) ;
56
@@ -81,16 +82,37 @@ function remoteTagExists(tag) {
8182 return commandSucceeds ( `git ls-remote --exit-code origin "refs/tags/${ tag } "` ) ;
8283}
8384
84- function deleteExistingTag ( tag ) {
85- if ( remoteTagExists ( tag ) ) {
85+ function confirmTagOverwrite ( tag , existing ) {
86+ const locations = [
87+ existing . local ? '本地' : '' ,
88+ existing . remote ? 'origin 远程' : '' ,
89+ ] . filter ( Boolean ) . join ( '、' ) ;
90+ const prompt = `⚠ tag ${ tag } 已存在(${ locations } )。删除后重新打 tag 并推送?[y/N] ` ;
91+ const input = readline . createInterface ( { input : process . stdin , output : process . stdout } ) ;
92+
93+ return new Promise ( ( resolve ) => {
94+ let resolved = false ;
95+ const finish = ( answer ) => {
96+ if ( resolved ) return ;
97+ resolved = true ;
98+ input . close ( ) ;
99+ resolve ( answer ) ;
100+ } ;
101+ input . once ( 'close' , ( ) => finish ( false ) ) ;
102+ input . question ( prompt , ( answer ) => finish ( answer . trim ( ) . toLowerCase ( ) === 'y' ) ) ;
103+ } ) ;
104+ }
105+
106+ function deleteExistingTag ( tag , existing ) {
107+ if ( existing . remote ) {
86108 run ( `git push origin --delete "${ tag } "` ) ;
87109 }
88- if ( tagExists ( tag ) ) {
110+ if ( existing . local ) {
89111 run ( `git tag -d "${ tag } "` ) ;
90112 }
91113}
92114
93- function main ( ) {
115+ async function main ( ) {
94116 const target = process . argv [ 2 ] ;
95117 const arg = process . argv [ 3 ] ;
96118
@@ -165,9 +187,20 @@ function main() {
165187 if ( notesChanged ) console . log ( 'notes : updated from ReleaseNote files' ) ;
166188 console . log ( '' ) ;
167189
168- if ( tagExists ( tag ) && ! cfg . overwriteExistingRelease ) {
169- console . error ( `error: tag "${ tag } " already exists` ) ;
170- process . exit ( 1 ) ;
190+ const existingTag = {
191+ local : tagExists ( tag ) ,
192+ remote : remoteTagExists ( tag ) ,
193+ } ;
194+ if ( existingTag . local || existingTag . remote ) {
195+ if ( ! cfg . overwriteExistingRelease ) {
196+ console . error ( `error: tag "${ tag } " already exists` ) ;
197+ process . exit ( 1 ) ;
198+ }
199+ if ( ! ( await confirmTagOverwrite ( tag , existingTag ) ) ) {
200+ console . log ( '✗ 已取消,未删除任何 tag,发版中止' ) ;
201+ process . exitCode = 1 ;
202+ return ;
203+ }
171204 }
172205
173206 if ( mutate ) {
@@ -212,8 +245,8 @@ function main() {
212245 run ( `git commit -m "${ commitMsg } "` ) ;
213246 }
214247
215- if ( cfg . overwriteExistingRelease ) {
216- deleteExistingTag ( tag ) ;
248+ if ( existingTag . local || existingTag . remote ) {
249+ deleteExistingTag ( tag , existingTag ) ;
217250 }
218251
219252 run ( `git tag -a "${ tag } " -m "${ target } v${ nextVersion } "` ) ;
@@ -225,4 +258,7 @@ function main() {
225258 console . log ( `✓ pushed tag ${ tag } — workflow "${ workflowName } " triggered` ) ;
226259}
227260
228- main ( ) ;
261+ main ( ) . catch ( ( error ) => {
262+ console . error ( error instanceof Error ? error . message : error ) ;
263+ process . exit ( 1 ) ;
264+ } ) ;
0 commit comments