11#!/usr/bin/env node
2+ /**
3+ * 一键发版:打印摘要 → 二次确认 → bump 版本 → commit → (tag 已存在时确认清理) → 打 tag → 推送。
4+ * 推送 tag 后触发 "Release App" workflow,由 GitHub Actions 完成构建与发布。
5+ *
6+ * 确认规则(参考 wmdebugger/scripts/release.js):
7+ * - tag 已存在(本地或远程):y/N 确认后删除旧 tag 再重打并推送
8+ * - tag 不存在:回车确认发版,Ctrl+C 取消
9+ *
10+ * 用法:
11+ * npm run release # 用当前版本,tag & push
12+ * npm run release patch # bump patch
13+ * npm run release minor|major
14+ * npm run release 0.2.0 # 指定版本号
15+ */
216const { execSync } = require ( 'child_process' ) ;
17+ const readline = require ( 'readline' ) ;
318const path = require ( 'path' ) ;
419const fs = require ( 'fs' ) ;
520
@@ -24,7 +39,11 @@ function run(cmd, opts = {}) {
2439 return execSync ( cmd , { stdio : 'inherit' , ...opts } ) ;
2540}
2641
27- function commandSucceeds ( cmd ) {
42+ function sh ( cmd ) {
43+ return execSync ( cmd , { encoding : 'utf8' } ) . trim ( ) ;
44+ }
45+
46+ function ok ( cmd ) {
2847 try {
2948 execSync ( cmd , { stdio : 'ignore' } ) ;
3049 return true ;
@@ -49,15 +68,22 @@ function bumpVersion(version, type) {
4968}
5069
5170function isWorkingTreeClean ( ) {
52- return execSync ( 'git status --porcelain' , { encoding : 'utf8' } ) . trim ( ) . length === 0 ;
71+ return sh ( 'git status --porcelain' ) . length === 0 ;
5372}
5473
5574function tagExists ( tag ) {
56- return commandSucceeds ( `git rev-parse -q --verify "refs/tags/${ tag } "` ) ;
75+ return ok ( `git rev-parse -q --verify "refs/tags/${ tag } "` ) ;
5776}
5877
5978function remoteTagExists ( tag ) {
60- return commandSucceeds ( `git ls-remote --exit-code origin "refs/tags/${ tag } "` ) ;
79+ try {
80+ execSync ( `git ls-remote --exit-code origin "refs/tags/${ tag } "` , { stdio : 'ignore' } ) ;
81+ return true ;
82+ } catch ( error ) {
83+ // git ls-remote 在远程可达但 ref 不存在时返回 2;其他错误(网络/权限)必须中断发版
84+ if ( error && typeof error === 'object' && error . status === 2 ) return false ;
85+ throw new Error ( `无法检查 origin 上的 tag ${ tag } ,请确认网络和 Git 权限` ) ;
86+ }
6187}
6288
6389function deleteExistingTag ( tag ) {
@@ -72,7 +98,7 @@ function deleteExistingTag(tag) {
7298function main ( ) {
7399 const arg = process . argv [ 2 ] ;
74100 const root = path . resolve ( __dirname , '..' ) ;
75- // 版本以根 package.json 为准(tauri.conf.json 的 version 指向 ../../../package.json)
101+ // 版本以根 package.json 为准
76102 const pkgPath = path . join ( root , PKG_JSON ) ;
77103 const pkg = readJson ( pkgPath ) ;
78104
@@ -99,32 +125,94 @@ function main() {
99125
100126 const tag = `${ TAG_PREFIX } ${ nextVersion } ` ;
101127
102- console . log ( `current : ${ currentVersion } ` ) ;
103- console . log ( `next : ${ nextVersion } ` ) ;
104- console . log ( `tag : ${ tag } ` ) ;
105- console . log ( '' ) ;
128+ // ---------- 前置检查(任何破坏性操作之前) ----------
129+ let status ;
130+ try {
131+ status = sh ( 'git status --porcelain' ) ;
132+ } catch {
133+ console . error ( '✗ 不在 git 仓库中' ) ;
134+ process . exit ( 1 ) ;
135+ }
136+ if ( versionChanged && status ) {
137+ console . error ( '✗ 工作区有未提交改动,请先 commit 或 stash:' ) ;
138+ console . error ( status ) ;
139+ process . exit ( 1 ) ;
140+ }
141+
142+ const branch = sh ( 'git rev-parse --abbrev-ref HEAD' ) ;
143+ if ( branch === 'HEAD' ) {
144+ console . error ( '✗ 当前处于 detached HEAD,请先 checkout 到发布分支' ) ;
145+ process . exit ( 1 ) ;
146+ }
106147
107- if ( versionChanged ) {
108- if ( ! isWorkingTreeClean ( ) ) {
109- console . error ( 'error: working tree not clean — commit or stash before releasing' ) ;
110- process . exit ( 1 ) ;
148+ const originUrl = sh ( 'git remote get-url origin' ) ;
149+
150+ // ---------- 打印发版摘要 ----------
151+ console . log ( '────────────────────────────────────────' ) ;
152+ console . log ( ` 当前版本: ${ currentVersion } ` ) ;
153+ console . log ( ` 发布版本: ${ tag } ` ) ;
154+ console . log ( ` 分支: ${ branch } ` ) ;
155+ console . log ( ` 远程: ${ originUrl } ` ) ;
156+ console . log ( '────────────────────────────────────────' ) ;
157+
158+ // ---------- tag 状态检测:确认前说明将删除已有还是新建 ----------
159+ const tagLocal = tagExists ( tag ) ;
160+ const tagRemote = remoteTagExists ( tag ) ;
161+ const tagWhere = [
162+ tagLocal ? '本地' : null ,
163+ tagRemote ? 'origin 远程' : null ,
164+ ] . filter ( Boolean ) . join ( '、' ) ;
165+
166+ const rl = readline . createInterface ( { input : process . stdin , output : process . stdout } ) ;
167+ const ask = ( prompt ) => new Promise ( ( resolve ) => rl . question ( prompt , resolve ) ) ;
168+
169+ ( async ( ) => {
170+ let needClean = false ;
171+ if ( tagLocal || tagRemote ) {
172+ console . log ( ` tag 状态: 已存在(${ tagWhere } )→ 将删除后重打` ) ;
173+ const ans = await ask ( `⚠ tag ${ tag } 已存在(${ tagWhere } )。删除后重新打 tag 并推送?[y/N] ` ) ;
174+ if ( ans . trim ( ) . toLowerCase ( ) !== 'y' ) {
175+ console . log ( '✗ 已取消,未删除任何 tag,发版中止' ) ;
176+ rl . close ( ) ;
177+ process . exit ( 1 ) ;
178+ }
179+ needClean = true ;
180+ } else {
181+ console . log ( ' tag 状态: 新建' ) ;
182+ await ask ( '回车确认发版,Ctrl+C 取消...' ) ;
111183 }
112184
113- pkg . version = nextVersion ;
114- writeJson ( pkgPath , pkg ) ;
185+ // ---------- 确认通过后才执行写操作 ----------
186+ if ( versionChanged ) {
187+ if ( ! isWorkingTreeClean ( ) ) {
188+ console . error ( '✗ 工作区有未提交改动,请先 commit 或 stash 后再发版' ) ;
189+ process . exit ( 1 ) ;
190+ }
115191
116- run ( `git add "${ PKG_JSON } "` ) ;
117- run ( `git commit -m "chore: release v${ nextVersion } "` ) ;
118- }
192+ pkg . version = nextVersion ;
193+ writeJson ( pkgPath , pkg ) ;
119194
120- deleteExistingTag ( tag ) ;
195+ run ( `git add "${ PKG_JSON } "` ) ;
196+ run ( `git commit -m "chore: release v${ nextVersion } "` ) ;
197+ }
121198
122- run ( `git tag -a "${ tag } " -m "nes-emulator v${ nextVersion } "` ) ;
123- run ( 'git push' ) ;
124- run ( `git push origin "${ tag } "` ) ;
199+ if ( needClean ) {
200+ deleteExistingTag ( tag ) ;
201+ console . log ( `✓ 已清理旧 tag ${ tag } ` ) ;
202+ }
125203
126- console . log ( '' ) ;
127- console . log ( `✓ pushed tag ${ tag } — workflow "${ WORKFLOW_NAME } " triggered` ) ;
204+ run ( `git tag -a "${ tag } " -m "nes-emulator v${ nextVersion } "` ) ;
205+ run ( 'git push' ) ;
206+ run ( `git push origin "${ tag } "` ) ;
207+
208+ console . log ( '' ) ;
209+ console . log ( `✓ 发版完成!tag ${ tag } 已推送 — workflow "${ WORKFLOW_NAME } " 已触发` ) ;
210+ rl . close ( ) ;
211+ process . exit ( 0 ) ;
212+ } ) ( ) . catch ( ( error ) => {
213+ console . error ( `✗ ${ error instanceof Error ? error . message : String ( error ) } ` ) ;
214+ process . exit ( 1 ) ;
215+ } ) ;
128216}
129217
130218main ( ) ;
0 commit comments