feat: 实现版本递增与交互选择流程

This commit is contained in:
2026-06-09 17:56:55 +08:00
parent 1871d0b665
commit da7770f76a

View File

@@ -1,3 +1,7 @@
import { readFileSync, writeFileSync } from "node:fs";
import { createInterface } from "node:readline";
import { join } from "node:path";
interface Semver { interface Semver {
major: number; major: number;
minor: number; minor: number;
@@ -33,9 +37,55 @@ export function bumpVersion(current: string, type: BumpType): string {
} }
} }
async function ask(query: string): Promise<string> {
const rl = createInterface({ input: process.stdin, output: process.stdout });
return new Promise((resolve) => {
rl.question(query, (answer) => {
rl.close();
resolve(answer.trim());
});
});
}
async function selectBumpType(): Promise<BumpType> {
console.log("选择版本递增类型:");
console.log(" 1) major - 不兼容的 API 变更");
console.log(" 2) minor - 向下兼容的功能新增");
console.log(" 3) patch - 向下兼容的问题修正");
while (true) {
const answer = await ask("请输入 1/2/3 [3]: ");
const choice = answer || "3";
if (choice === "1") return "major";
if (choice === "2") return "minor";
if (choice === "3") return "patch";
console.log("无效选择,请输入 1、2 或 3");
}
}
async function stepBumpVersion(): Promise<string> {
const pkgPath = join(import.meta.dir, "..", "package.json");
const pkg = JSON.parse(readFileSync(pkgPath, "utf-8")) as { version: string };
const currentVersion = pkg.version;
const bumpType = await selectBumpType();
const newVersion = bumpVersion(currentVersion, bumpType);
const answer = await ask(`确认版本号 ${currentVersion}${newVersion}? [y/N]: `);
if (answer.toLowerCase() !== "y") {
console.log("已取消");
process.exit(0);
}
pkg.version = newVersion;
writeFileSync(pkgPath, JSON.stringify(pkg, null, 2) + "\n");
console.log(`版本号已更新: ${currentVersion}${newVersion}`);
return newVersion;
}
async function main(): Promise<void> { async function main(): Promise<void> {
// 后续 task 中实现流程编排 const newVersion = await stepBumpVersion();
console.log("release 脚本骨架就绪"); console.log(`[1/4] 版本号递增完成: ${newVersion}`);
} }
main().catch((err: unknown) => { main().catch((err: unknown) => {