1
0
mirror of https://github.com/sendevia/website.git synced 2026-03-05 23:32:45 +08:00

feat(version): add bash script to update package version and create tags

This commit is contained in:
2026-01-09 15:26:57 +08:00
parent 38bbc797b2
commit 03c217fb0e
2 changed files with 28 additions and 33 deletions

View File

@@ -1,33 +0,0 @@
import fs from "node:fs";
import { execSync } from "node:child_process";
const pkg = JSON.parse(fs.readFileSync("./package.json", "utf-8"));
// 获取提交数
const commitCount = parseInt(execSync("git rev-list --count HEAD").toString().trim(), 10);
// 检查是否有暂存的更改
const hasStagedChanges = execSync("git diff --cached --quiet || echo 'changed'").toString().trim() === "changed";
// 版本号中的提交数 = 当前提交数 + 1 (即将进行的提交) + (如果有暂存更改则额外+1)
const nextCommitCount = commitCount + 1 + (hasStagedChanges ? 1 : 0);
// 获取当前日期
const now = new Date();
const year = now.getFullYear().toString().slice(-2);
const month = now.getMonth() + 1;
const date = now.getDate();
const newVersion = `${year}.${month}.${date}(${nextCommitCount})`;
pkg.version = newVersion;
fs.writeFileSync("./package.json", JSON.stringify(pkg, null, 2) + "\n");
console.log(`Version updated to: ${newVersion}`);
// 自动执行 git add 和 git commit
try {
execSync("git add package.json");
execSync(`git commit -m "chore(package): update to version ${newVersion}"`);
console.log(`Committed: chore(package): update to version ${newVersion}`);
} catch (error) {
console.error("Failed to commit version update:", error.message);
}

28
scripts/update-version.sh Normal file
View File

@@ -0,0 +1,28 @@
#!/bin/bash
# 获取当前提交数
COMMIT_COUNT=$(git rev-list --count HEAD)
# 计算最终提交数
NEXT_COMMIT_COUNT=$((COMMIT_COUNT + 1))
# 获取当前日期 YY.M.D
YEAR=$(date +%y)
MONTH=$(date +%-m)
DAY=$(date +%-d)
NEW_VERSION="${YEAR}.${MONTH}.${DAY}(${NEXT_COMMIT_COUNT})"
# 使用 sed 更新 package.json 中的版本号
# 匹配 "version": "..." 模式
sed -i "s/\"version\": \".*\"/\"version\": \"${NEW_VERSION}\"/" package.json
echo "Version updated to: ${NEW_VERSION}"
# Git 操作
git add package.json
git commit -m "chore(package): update to version ${NEW_VERSION}"
echo "Committed: chore(package): update to version ${NEW_VERSION}"
git tag "${NEW_VERSION}"
echo "Tagged: ${NEW_VERSION}"