Files
hose-use/scripts/release/bump-version.ts
T
gitea c71b20c25b chore: 按用途整理工程脚本与验证入口
将内部脚本归入 build、release、generate、dev 和 checks,保留公开安装脚本路径。
同步命令、CI、发布流程、脚本间调用及根目录定位,补齐脚本说明与完成报告。

验证:929 项测试、类型与版本检查通过;二进制及插件构建、冒烟与打包通过。
OpenAPI 产物不变;工作流 Schema 导出存在基线即可复现的 Zod transform 错误,单独记录。
2026-10-30 14:15:32 +08:00

122 lines
6.3 KiB
TypeScript

#!/usr/bin/env bun
import { readFileSync, writeFileSync, existsSync } from "node:fs";
import { join } from "node:path";
import { execFileSync } from "node:child_process";
export function bumpVersion(ROOT: string, input: string, rebuild = () => {
execFileSync(process.execPath, ["run", "openapi:all"], { cwd: ROOT, stdio: "inherit" });
execFileSync(process.execPath, ["run", "build:plugin"], { cwd: ROOT, stdio: "inherit" });
execFileSync(process.execPath, ["run", "check:version"], { cwd: ROOT, stdio: "inherit" });
}): string {
const pkgPath = join(ROOT, "package.json");
const pkg = JSON.parse(readFileSync(pkgPath, "utf-8"));
const currentVersion: string = pkg.version;
if (!input) {
console.error("Usage: bun run bump <version | patch | minor | major>");
console.error(`Current version: ${currentVersion}`);
throw new Error("A release version is required");
}
let nextVersion = input.replace(/^v/, "");
if (input === "patch" || input === "minor" || input === "major") {
const [major, minor, patch] = currentVersion.split("-")[0].split(".").map(Number);
if (input === "patch") nextVersion = `${major}.${minor}.${patch + 1}`;
if (input === "minor") nextVersion = `${major}.${minor + 1}.0`;
if (input === "major") nextVersion = `${major + 1}.0.0`;
}
if (!isReleaseVersion(nextVersion)) throw new Error(`Invalid SemVer format: '${nextVersion}'`);
if (Bun.semver.order(nextVersion, currentVersion) <= 0) throw new Error(`Version must increase: ${currentVersion} -> ${nextVersion}`);
const git = (args: string[]) => execFileSync("git", args, { cwd: ROOT, encoding: "utf8", stdio: ["ignore", "pipe", "pipe"] }).trim();
if (git(["status", "--porcelain"])) throw new Error("Release preparation requires a clean working tree");
const branch = git(["branch", "--show-current"]);
if (!["codex/release-v", "release/v"].some(prefix => branch === `${prefix}${nextVersion}`)) {
throw new Error(`Prepare the release on codex/release-v${nextVersion} or release/v${nextVersion}; current branch: ${branch || "detached HEAD"}`);
}
if (git(["tag", "--list", `v${nextVersion}`])) throw new Error(`Tag v${nextVersion} already exists`);
// Read all versioned sources before writing any of them.
for (const file of ["README.md", "skills/hose-use/SKILL.md"]) readFileSync(join(ROOT, file), "utf8");
for (const file of ["specs/hose-open-api/openapi.yaml", ".zcode-plugin/plugin.json"]) {
if (existsSync(join(ROOT, file))) {
const content = readFileSync(join(ROOT, file), "utf8");
if (file.endsWith(".json")) JSON.parse(content);
}
}
console.log(`==> Bumping version: ${currentVersion} -> ${nextVersion}`);
// 1. package.json
pkg.version = nextVersion;
writeFileSync(pkgPath, JSON.stringify(pkg, null, 2) + "\n");
console.log("✓ Updated package.json");
// 2. README.md
const readmePath = join(ROOT, "README.md");
let readmeContent = readFileSync(readmePath, "utf-8");
readmeContent = readmeContent.replace(/# hose-use \(v\d+\.\d+\.\d+(?:-[A-Za-z0-9.-]+)?\)/g, `# hose-use (v${nextVersion})`);
writeFileSync(readmePath, readmeContent);
console.log("✓ Updated README.md");
// 3. skills/hose-use/SKILL.md
const skillPath = join(ROOT, "skills", "hose-use", "SKILL.md");
let skillContent = readFileSync(skillPath, "utf-8");
skillContent = skillContent.replace(/# hose-use \(v\d+\.\d+\.\d+(?:-[A-Za-z0-9.-]+)?\)/g, `# hose-use (v${nextVersion})`);
skillContent = skillContent.replace(/v\d+\.\d+\.\d+(?:-[A-Za-z0-9.-]+)?: 合思/g, `v${nextVersion}: 合思`);
skillContent = skillContent.replace(/version: \d+\.\d+\.\d+(?:-[A-Za-z0-9.-]+)?/g, `version: ${nextVersion}`);
skillContent = skillContent.replace(/\(v\d+\.\d+\.\d+(?:-[A-Za-z0-9.-]+)?\)/g, `(v${nextVersion})`);
writeFileSync(skillPath, skillContent);
console.log("✓ Updated skills/hose-use/SKILL.md");
// 4. specs/hose-open-api/openapi.yaml
const openapiPath = join(ROOT, "specs", "hose-open-api", "openapi.yaml");
if (existsSync(openapiPath)) {
let openapiContent = readFileSync(openapiPath, "utf-8");
openapiContent = openapiContent.replace(/version: \d+\.\d+\.\d+(?:-[A-Za-z0-9.-]+)?/g, `version: ${nextVersion}`);
writeFileSync(openapiPath, openapiContent);
console.log("✓ Updated specs/hose-open-api/openapi.yaml");
}
// 4.5 .zcode-plugin/plugin.json
const pluginJsonPath = join(ROOT, ".zcode-plugin", "plugin.json");
if (existsSync(pluginJsonPath)) {
const pluginJson = JSON.parse(readFileSync(pluginJsonPath, "utf-8"));
pluginJson.version = nextVersion;
writeFileSync(pluginJsonPath, JSON.stringify(pluginJson, null, 2) + "\n");
console.log("✓ Updated .zcode-plugin/plugin.json");
}
// 5. Sync to global ~/.agents/skills/hose-use/ is now an explicit dev:override step.
// A version bump must not overwrite personal Skill files in the user's environment.
// 7. Rebuild OpenAPI and plugin bundles so generated artifacts stay in lockstep.
console.log("==> Rebuilding OpenAPI and plugin bundles...");
rebuild();
console.log("\n🎉 Successfully bumped version to " + nextVersion + "!");
console.log("\nNext steps to publish:");
console.log(` 1. Review the diff, then run bun test && bun run check && bun run check:version`);
console.log(` 2. Commit on ${branch}, push that branch and open a PR to main`);
console.log(` 3. Wait for CI and review, then merge the PR`);
console.log(` 4. Switch to main; git pull --ff-only origin main; git push home main`);
console.log(` 5. Create annotated tag v${nextVersion} on the merged release commit`);
console.log(` 6. git push home refs/tags/v${nextVersion} (first), then git push origin refs/tags/v${nextVersion}`);
console.log(` 7. Verify the Release build, binary archives and checksums on both sources`);
console.log(` Optional: bun run dev:override to explicitly sync your global development binary and Skill`);
return nextVersion;
}
export function isReleaseVersion(value: string): boolean {
const match = value.match(/^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-([A-Za-z0-9-]+(?:\.[A-Za-z0-9-]+)*))?$/);
return !!match && (!match[4] || match[4].split(".").every(part => !/^0\d+$/.test(part)));
}
if (import.meta.main) {
try {
bumpVersion(join(import.meta.dirname, "../.."), process.argv[2]?.trim() || "");
} catch (error) {
console.error(error instanceof Error ? error.message : String(error));
process.exitCode = 1;
}
}