227 lines
8.3 KiB
TypeScript
227 lines
8.3 KiB
TypeScript
#!/usr/bin/env bun
|
|
import { execSync, spawnSync } from "node:child_process";
|
|
import { readFileSync, writeFileSync } from "node:fs";
|
|
import { join } from "node:path";
|
|
|
|
const ROOT = join(import.meta.dirname, "..");
|
|
const pkgPath = join(ROOT, "package.json");
|
|
const pkg = JSON.parse(readFileSync(pkgPath, "utf-8"));
|
|
const currentVersion: string = pkg.version;
|
|
|
|
const args = process.argv.slice(2);
|
|
const isDryRun = args.includes("--dry-run");
|
|
const inputArg = args.find((a) => !a.startsWith("-"))?.trim();
|
|
|
|
if (!inputArg) {
|
|
console.log(`
|
|
Usage: bun run release <patch | minor | major | version> [--dry-run]
|
|
|
|
Current version: v${currentVersion}
|
|
|
|
Examples:
|
|
bun run release patch
|
|
bun run release minor
|
|
bun run release 0.10.34 --dry-run
|
|
`);
|
|
process.exit(1);
|
|
}
|
|
|
|
// 1. Calculate target version
|
|
let nextVersion = inputArg.replace(/^v/, "");
|
|
if (inputArg === "patch" || inputArg === "minor" || inputArg === "major") {
|
|
const [major, minor, patch] = currentVersion.split(".").map(Number);
|
|
if (inputArg === "patch") nextVersion = `${major}.${minor}.${patch + 1}`;
|
|
if (inputArg === "minor") nextVersion = `${major}.${minor + 1}.0`;
|
|
if (inputArg === "major") nextVersion = `${major + 1}.0.0`;
|
|
}
|
|
|
|
if (!/^\d+\.\d+\.\d+(-[a-zA-Z0-9.]+)?$/.test(nextVersion)) {
|
|
console.error(`❌ 非法的 SemVer 格式: '${nextVersion}'`);
|
|
process.exit(1);
|
|
}
|
|
|
|
const releaseBranch = `codex/release-v${nextVersion}`;
|
|
const tag = `v${nextVersion}`;
|
|
|
|
console.log("\n========================================================");
|
|
console.log(`🚀 Seal Use 自动化发布向导: v${currentVersion} -> v${nextVersion}`);
|
|
if (isDryRun) console.log("🔍 [DRY RUN 预检演练模式 - 不会执行真实推送或合并]");
|
|
console.log("========================================================\n");
|
|
|
|
function run(cmd: string, options: { cwd?: string; stdio?: "inherit" | "pipe" | "ignore" } = {}) {
|
|
const stdio = options.stdio ?? "inherit";
|
|
try {
|
|
return execSync(cmd, { cwd: options.cwd ?? ROOT, encoding: "utf-8", stdio }).trim();
|
|
} catch (err) {
|
|
const errorMsg = err instanceof Error ? err.message : String(err);
|
|
throw new Error(`执行命令失败: ${cmd}\n${errorMsg}`);
|
|
}
|
|
}
|
|
|
|
function runCapture(cmd: string) {
|
|
return execSync(cmd, { cwd: ROOT, encoding: "utf-8" }).trim();
|
|
}
|
|
|
|
// 2. Pre-flight checks
|
|
console.log("🔍 步骤 1/8: 执行发布前置条件核验...");
|
|
|
|
// Check git status
|
|
const gitStatus = runCapture("git status --porcelain");
|
|
if (gitStatus) {
|
|
console.error("❌ 工作区存在未提交的修改,请先提交或暂存后再发布:\n");
|
|
console.error(gitStatus);
|
|
process.exit(1);
|
|
}
|
|
|
|
// Check current branch
|
|
const currentBranch = runCapture("git branch --show-current");
|
|
if (currentBranch !== "main" && currentBranch !== "master") {
|
|
console.error(`❌ 发版必须从 main 主干分支发起,当前处于: '${currentBranch}'`);
|
|
process.exit(1);
|
|
}
|
|
|
|
// Check remotes
|
|
const remotes = runCapture("git remote").split(/\r?\n/);
|
|
if (!remotes.includes("origin")) {
|
|
console.error("❌ 未找到 'origin' 远程仓库。");
|
|
process.exit(1);
|
|
}
|
|
const hasHomeRemote = remotes.includes("home");
|
|
if (!hasHomeRemote) {
|
|
console.warn("⚠️ 未找到 'home' 镜像远程仓库,将仅向 'origin' 推送。");
|
|
}
|
|
|
|
// Check GitHub CLI
|
|
try {
|
|
run("gh auth status", { stdio: "ignore" });
|
|
} catch {
|
|
console.error("❌ GitHub CLI (gh) 未登录或不可用。请先运行 'gh auth login'。");
|
|
process.exit(1);
|
|
}
|
|
|
|
console.log("✓ 前置检查通过!\n");
|
|
|
|
if (isDryRun) {
|
|
console.log("🧪 正在模拟运行测试与编译检查...");
|
|
run("bun run check");
|
|
run("bun test");
|
|
console.log("\n✅ [DRY RUN] 预检演练全部通过!真实发布将执行:");
|
|
console.log(` 1. git switch -c ${releaseBranch}`);
|
|
console.log(` 2. bun run bump ${nextVersion}`);
|
|
console.log(` 3. 更新 CHANGELOG.md`);
|
|
console.log(` 4. git commit -m "Release ${tag}: ..."`);
|
|
console.log(` 5. git push origin HEAD:refs/heads/${releaseBranch}`);
|
|
console.log(` 6. gh pr create && gh pr checks --watch && gh pr merge --merge`);
|
|
console.log(` 7. 同步 main,打标 ${tag} 并推送到 origin 与 home`);
|
|
process.exit(0);
|
|
}
|
|
|
|
// 3. Create release branch
|
|
console.log(`🌿 步骤 2/8: 创建并切换至发布分支: ${releaseBranch}...`);
|
|
run(`git switch -c ${releaseBranch}`);
|
|
|
|
let success = false;
|
|
|
|
try {
|
|
// 4. Bump version
|
|
console.log(`\n📦 步骤 3/8: 执行版本 Bump (${nextVersion})...`);
|
|
run(`bun run bump ${nextVersion} --force`);
|
|
|
|
// Ensure CHANGELOG.md has entry
|
|
const changelogPath = join(ROOT, "CHANGELOG.md");
|
|
let changelog = readFileSync(changelogPath, "utf-8");
|
|
if (!changelog.includes(`## [${nextVersion}]`)) {
|
|
console.log("📝 正在根据最近提交记录自动补充 CHANGELOG.md 模板...");
|
|
const latestTag = runCapture("git describe --tags --abbrev=0 2>/dev/null || echo ''");
|
|
const commitLogCmd = latestTag ? `git log ${latestTag}..HEAD --oneline` : "git log -n 10 --oneline";
|
|
const commits = runCapture(commitLogCmd).split("\n").slice(0, 8);
|
|
const dateStr = new Date().toISOString().slice(0, 10);
|
|
const changelogEntry = `## [${nextVersion}] - ${dateStr}\n\n### Added\n` +
|
|
commits.map((c) => `- ${c.replace(/^[a-f0-9]+ /, "")}`).join("\n") + "\n\n";
|
|
|
|
changelog = changelog.replace("## [Unreleased]\n", `## [Unreleased]\n\n${changelogEntry}`);
|
|
writeFileSync(changelogPath, changelog);
|
|
console.log("✓ 已将提交大纲写入 CHANGELOG.md");
|
|
}
|
|
|
|
// 5. Test and build validation
|
|
console.log("\n🧪 步骤 4/8: 执行全量测试与类型安全校验...");
|
|
run("bun run check");
|
|
run("bun test");
|
|
console.log("✓ 本地测试与类型验证通过!\n");
|
|
|
|
// 6. Commit and Push
|
|
console.log("💾 步骤 5/8: 提交发布版本变更并推送到远程分支...");
|
|
const releaseTitle = `Release ${tag}: Automated release bump`;
|
|
run(`git commit -am "${releaseTitle}"`);
|
|
run(`git push origin HEAD:refs/heads/${releaseBranch}`);
|
|
|
|
// 7. Create PR & Wait for CI
|
|
console.log("\n🔀 步骤 6/8: 创建 GitHub Release PR 并等待 CI 验证...");
|
|
const prUrl = runCapture(
|
|
`gh pr create --base main --head ${releaseBranch} --title "${releaseTitle}" --body "Automated release bump for ${tag}."`
|
|
);
|
|
console.log(`✓ 已创建 PR: ${prUrl}`);
|
|
|
|
const prNumber = prUrl.match(/\/pull\/(\d+)/)?.[1];
|
|
if (!prNumber) throw new Error(`无法从 PR URL 中提取 PR 编号: ${prUrl}`);
|
|
|
|
console.log(`⏳ 正在等待 PR #${prNumber} CI 门禁检查跑绿...`);
|
|
run(`gh pr checks ${prNumber} --watch`);
|
|
|
|
console.log(`🚀 自动合并 PR #${prNumber} 到 main...`);
|
|
run(`gh pr merge ${prNumber} --merge`);
|
|
|
|
// 8. Pull main, sync home, and tag
|
|
console.log("\n🏷️ 步骤 7/8: 同步主干分支并创建官方签名 Tag...");
|
|
run("git switch main");
|
|
run("git pull --ff-only origin main");
|
|
|
|
if (hasHomeRemote) {
|
|
console.log("🔄 同步推送 main 到 home (Gitea)...");
|
|
run("git push home main");
|
|
}
|
|
|
|
const tagMessage = `🚀 Release ${tag}: Automated release`;
|
|
run(`git tag -a ${tag} -m "${tagMessage}"`);
|
|
|
|
if (hasHomeRemote) {
|
|
console.log(`📤 推送 Tag ${tag} 到 home (Gitea)...`);
|
|
run(`git push home refs/tags/${tag}`);
|
|
}
|
|
|
|
console.log(`📤 推送 Tag ${tag} 到 origin (GitHub,触发 Release 构建)...`);
|
|
run(`git push origin refs/tags/${tag}`);
|
|
|
|
// 9. Cleanup
|
|
console.log("\n🧹 步骤 8/8: 清理临时发布分支...");
|
|
run(`git branch -d ${releaseBranch}`);
|
|
try {
|
|
run(`git push origin --delete ${releaseBranch}`, { stdio: "ignore" });
|
|
} catch {
|
|
// best-effort
|
|
}
|
|
|
|
success = true;
|
|
} catch (err) {
|
|
console.error(`\n❌ 发布流程中断: ${err instanceof Error ? err.message : String(err)}`);
|
|
console.error("💡 正在安全恢复本地分支...");
|
|
try {
|
|
run("git switch main", { stdio: "ignore" });
|
|
} catch {
|
|
// ignore
|
|
}
|
|
process.exit(1);
|
|
}
|
|
|
|
if (success) {
|
|
console.log("\n🎉 ========================================================");
|
|
console.log(`🎉 恭喜!Seal Use ${tag} 已成功发布并触发多平台自动化编译!`);
|
|
console.log(" • GitHub Actions 构建中心: https://github.com/yunyingk/seal-use/actions");
|
|
console.log(` • GitHub Release 页面: https://github.com/yunyingk/seal-use/releases/tag/${tag}`);
|
|
if (hasHomeRemote) {
|
|
console.log(` • Gitea Release 镜像: https://git.ying-qing.cn/gitea/seal-use/releases/tag/${tag}`);
|
|
}
|
|
console.log("========================================================\n");
|
|
}
|