From 824969ea2537d5c0653d79733036add80d111891 Mon Sep 17 00:00:00 2001 From: lanyuanxiaoyao Date: Thu, 11 Jun 2026 15:16:29 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E4=BC=98=E5=8C=96=E5=8F=91?= =?UTF-8?q?=E5=B8=83=E6=B5=81=E7=A8=8B=EF=BC=8Cnpm=20=E5=8F=91=E5=B8=83?= =?UTF-8?q?=E5=A4=B1=E8=B4=A5=E6=88=96=E5=8F=96=E6=B6=88=E6=97=B6=E8=87=AA?= =?UTF-8?q?=E5=8A=A8=E5=9B=9E=E9=80=80=E7=89=88=E6=9C=AC=E5=8F=B7=E4=BF=AE?= =?UTF-8?q?=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- scripts/release.ts | 36 +++++++++++++++++++++++++++++++++--- 1 file changed, 33 insertions(+), 3 deletions(-) diff --git a/scripts/release.ts b/scripts/release.ts index b664bf1..9b347cd 100644 --- a/scripts/release.ts +++ b/scripts/release.ts @@ -162,8 +162,7 @@ async function stepNpmPublish(): Promise { const answer = await ask("确认发布到 npm? [y/N]: "); if (answer.toLowerCase() !== "y") { - console.log("已取消发布"); - process.exit(0); + throw new CancelledError("已取消发布"); } const proc = Bun.spawn(["bun", "publish", "--access", "public"], { @@ -184,6 +183,24 @@ function writeVersion(version: string): void { writeFileSync(pkgPath, JSON.stringify(pkg, null, 2) + "\n"); } +async function revertVersion(): Promise { + console.log("\n回退 package.json 到原始状态..."); + const proc = Bun.spawn(["git", "checkout", "package.json"], { + stdio: ["inherit", "inherit", "inherit"], + }); + const exitCode = await proc.exited; + if (exitCode !== 0) { + throw new Error("git checkout package.json 失败,请手动检查工作区状态"); + } +} + +class CancelledError extends Error { + constructor(message: string) { + super(message); + this.name = "CancelledError"; + } +} + async function main(): Promise { await checkWorkingTree(); @@ -196,10 +213,23 @@ async function main(): Promise { writeVersion(newVersion); console.log(`[2/3] 版本号已更新: ${newVersion}`); + try { + await stepNpmPublish(); + } catch (err) { + if (err instanceof CancelledError) { + console.log(err.message); + await revertVersion(); + console.log("工作区已恢复,无事发生"); + process.exit(0); + } + console.error("npm 发布失败,正在回退..."); + await revertVersion(); + throw err; + } + await stepGitCommitTag(newVersion); console.log(`[3/3] git commit 和 tag v${newVersion} 完成`); - await stepNpmPublish(); console.log("npm 发布完成"); }