打包后自动创建并推送 Git Tag 脚本

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();


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值