chore: fix release-alpha-version

This commit is contained in:
Tunglies 2025-03-26 01:54:45 +08:00
parent 1baa840160
commit 5be1d604ee

View File

@ -1,48 +1,38 @@
import fs from "fs/promises"; import fs from "fs/promises";
import path from "path"; import path from "path";
import { program } from "commander";
/** /**
* 更新 package.json 文件中的版本号 * 更新 package.json 文件中的版本号
* @param {string} versionTag 版本标签 ( "alpha", "beta", "rc")
*/ */
async function updatePackageVersion(versionTag) { async function updatePackageVersion() {
const _dirname = process.cwd(); const _dirname = process.cwd();
const packageJsonPath = path.join(_dirname, "package.json"); const packageJsonPath = path.join(_dirname, "package.json");
try { try {
const data = await fs.readFile(packageJsonPath, "utf8"); const data = await fs.readFile(packageJsonPath, "utf8");
const packageJson = JSON.parse(data); const packageJson = JSON.parse(data);
// 获取当前版本并清理可能存在的旧标签 let result = packageJson.version;
let currentVersion = packageJson.version.replace( if (!result.includes("alpha")) {
/-(alpha|beta|rc)\.?\d*$/i, result = `${result}-alpha`;
"", }
);
let newVersion = `${currentVersion}-${versionTag}`;
console.log( console.log("[INFO]: Current package.json version is: ", result);
"[INFO]: Current package.json version is: ", packageJson.version = result;
packageJson.version,
);
packageJson.version = newVersion;
await fs.writeFile( await fs.writeFile(
packageJsonPath, packageJsonPath,
JSON.stringify(packageJson, null, 2), JSON.stringify(packageJson, null, 2),
"utf8", "utf8",
); );
console.log(`[INFO]: package.json version updated to: ${newVersion}`); console.log(`[INFO]: package.json version updated to: ${result}`);
return newVersion;
} catch (error) { } catch (error) {
console.error("Error updating package.json version:", error); console.error("Error updating package.json version:", error);
throw error;
} }
} }
/** /**
* 更新 Cargo.toml 文件中的版本号 * 更新 Cargo.toml 文件中的版本号
* @param {string} versionTag 版本标签
*/ */
async function updateCargoVersion(versionTag) { async function updateCargoVersion() {
const _dirname = process.cwd(); const _dirname = process.cwd();
const cargoTomlPath = path.join(_dirname, "src-tauri", "Cargo.toml"); const cargoTomlPath = path.join(_dirname, "src-tauri", "Cargo.toml");
try { try {
@ -50,12 +40,10 @@ async function updateCargoVersion(versionTag) {
const lines = data.split("\n"); const lines = data.split("\n");
const updatedLines = lines.map((line) => { const updatedLines = lines.map((line) => {
if (line.trim().startsWith("version =")) { if (line.startsWith("version =")) {
// 清理可能存在的旧标签 const versionMatch = line.match(/version\s*=\s*"([^"]+)"/);
const cleanedVersion = line.replace(/-(alpha|beta|rc)\.?\d*"/i, '"'); if (versionMatch && !versionMatch[1].includes("alpha")) {
const versionMatch = cleanedVersion.match(/version\s*=\s*"([^"]+)"/); const newVersion = `${versionMatch[1]}-alpha`;
if (versionMatch) {
const newVersion = `${versionMatch[1]}-${versionTag}`;
return line.replace(versionMatch[1], newVersion); return line.replace(versionMatch[1], newVersion);
} }
} }
@ -63,83 +51,46 @@ async function updateCargoVersion(versionTag) {
}); });
await fs.writeFile(cargoTomlPath, updatedLines.join("\n"), "utf8"); await fs.writeFile(cargoTomlPath, updatedLines.join("\n"), "utf8");
console.log(`[INFO]: Cargo.toml version updated with ${versionTag} tag`);
} catch (error) { } catch (error) {
console.error("Error updating Cargo.toml version:", error); console.error("Error updating Cargo.toml version:", error);
throw error;
} }
} }
/** /**
* 更新 tauri.conf.json 文件中的版本号 * 更新 tauri.conf.json 文件中的版本号
* @param {string} versionTag 版本标签
*/ */
async function updateTauriConfigVersion(versionTag) { async function updateTauriConfigVersion() {
const _dirname = process.cwd(); const _dirname = process.cwd();
const tauriConfigPath = path.join(_dirname, "src-tauri", "tauri.conf.json"); const tauriConfigPath = path.join(_dirname, "src-tauri", "tauri.conf.json");
try { try {
const data = await fs.readFile(tauriConfigPath, "utf8"); const data = await fs.readFile(tauriConfigPath, "utf8");
const tauriConfig = JSON.parse(data); const tauriConfig = JSON.parse(data);
// 清理可能存在的旧标签 let version = tauriConfig.version;
let currentVersion = tauriConfig.version.replace( if (!version.includes("alpha")) {
/-(alpha|beta|rc)\.?\d*$/i, version = `${version}-alpha`;
"", }
);
let newVersion = `${currentVersion}-${versionTag}`;
console.log( console.log("[INFO]: Current tauri.conf.json version is: ", version);
"[INFO]: Current tauri.conf.json version is: ", tauriConfig.version = version;
tauriConfig.version,
);
tauriConfig.version = newVersion;
await fs.writeFile( await fs.writeFile(
tauriConfigPath, tauriConfigPath,
JSON.stringify(tauriConfig, null, 2), JSON.stringify(tauriConfig, null, 2),
"utf8", "utf8",
); );
console.log(`[INFO]: tauri.conf.json version updated to: ${newVersion}`); console.log(`[INFO]: tauri.conf.json version updated to: ${version}`);
} catch (error) { } catch (error) {
console.error("Error updating tauri.conf.json version:", error); console.error("Error updating tauri.conf.json version:", error);
throw error;
} }
} }
/** /**
* 主函数依次更新所有文件的版本号 * 主函数依次更新所有文件的版本号
* @param {string} versionTag 版本标签
*/ */
async function main(versionTag) { async function main() {
if (!versionTag) { await updatePackageVersion();
console.error("Error: Version tag is required"); await updateCargoVersion();
process.exit(1); await updateTauriConfigVersion();
} }
// 验证版本标签是否有效 main().catch(console.error);
const validTags = ["alpha", "beta", "rc"];
if (!validTags.includes(versionTag.toLowerCase())) {
console.error(
`Error: Invalid version tag. Must be one of: ${validTags.join(", ")}`,
);
process.exit(1);
}
try {
console.log(`[INFO]: Updating versions with ${versionTag} tag...`);
await updatePackageVersion(versionTag);
await updateCargoVersion(versionTag);
await updateTauriConfigVersion(versionTag);
console.log("[SUCCESS]: All version updates completed successfully!");
} catch (error) {
console.error("[ERROR]: Failed to update versions:", error);
process.exit(1);
}
}
// 设置命令行界面
program
.name("pnpm release-version")
.description("Add version tag (alpha/beta/rc) to project version numbers")
.argument("<version-tag>", "version tag to add (alpha, beta, or rc)")
.action(main)
.parse(process.argv);