const { execSync } = require("child_process");
const fs = require("fs");
const path = require("path");
/**
* 读取 package.json 中的版本号
* @returns {string} 版本号
* AI 生成 2026-01-08 09:45:58 coder_yx
*/
function getVersion() {
const packagePath = path.join(__dirname, "package.json");
const packageJSON = JSON.parse(fs.readFileSync(packagePath, "utf8"));
return packageJSON.version;
}
/**
* 检查是否为 Git 仓库
* @returns {boolean} 是否为 Git 仓库
* AI 生成 2026-01-08 09:45:58 coder_yx
*/
function isGitRepository() {
try {
execSync("git rev-parse --git-dir", { stdio: "ignore" });
return true;
} catch (error) {
return false;
}
}
/**
* 检查 tag 是否已存在
* @param {string} tagName - Tag 名称
* @returns {boolean} Tag 是否已存在
* AI 生成 2026-01-08 09:45:58 coder_yx
*/
function tagExists(tagName) {
try {
execSync(`git rev-parse ${tagName}`, { stdio: "ignore" });
return true;
} catch (error) {
return false;
}
}
/**
* 创建并推送 Git Tag
* @param {string} version - 版本号
* AI 生成 2026-01-08 09:45:58 coder_yx
*/
function createAndPushTag(version) {
const tagName = `v${version}`;
const tagMessage = `Release version ${version}`;
try {
// 检查是否为 Git 仓库
if (!isGitRepository()) {
console.error("❌ 错误: 当前目录不是 Git 仓库");
process.exit(1);
}
// 检查 tag 是否已存在
if (tagExists(tagName)) {
console.log(`⚠️ 警告: Tag ${tagName} 已存在,跳过创建`);
return;
}
// 创建带注释的 tag
console.log(`📌 正在创建 Tag: ${tagName}...`);
execSync(`git tag -a ${tagName} -m "${tagMessage}"`, { stdio: "inherit" });
console.log(`✅ Tag ${tagName} 创建成功`);
// 推送 tag 到远程仓库
console.log(`🚀 正在推送 Tag ${tagName} 到远程仓库...`);
execSync(`git push origin ${tagName}`, { stdio: "inherit" });
console.log(`✅ Tag ${tagName} 推送成功`);
} catch (error) {
console.error(`❌ 创建或推送 Tag 失败:`, error.message);
process.exit(1);
}
}
// 主流程
function main() {
const version = getVersion();
console.log(`📦 当前版本: ${version}`);
createAndPushTag(version);
}
// 执行主流程
main();
打包后自动创建并推送 Git Tag 脚本
最新推荐文章于 2026-06-14 10:56:45 发布

2708

被折叠的 条评论
为什么被折叠?



