Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ jobs:

- name: Checkout repository
uses: actions/checkout@v4
with:
fetch-depth: 0

- name: Print environment info
run: |
Expand Down
53 changes: 29 additions & 24 deletions scripts/auto-version.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env node
import fs from 'fs';
import { execSync } from 'child_process';
import { spawnSync } from 'child_process';
import { fileURLToPath } from 'url';
import { dirname, join } from 'path';

Expand All @@ -13,15 +13,35 @@ function cleanString(str) {
return (str || '').toString().replace(/^["'\s]+|["'\s]+$/g, '').trim();
}

// 获取Git最新标签(纯Node.js实现,避免平台差异
function getLatestTag() {
// 执行Git命令(跨平台兼容
function execGitCommand(args) {
try {
// 获取所有标签
const tagsOutput = execSync('git tag 2>/dev/null || echo ""', {
const result = spawnSync('git', args, {
encoding: 'utf8',
stdio: ['pipe', 'pipe', 'ignore']
});

if (result.error || result.status !== 0) {
return '';
}

return cleanString(result.stdout);
} catch (error) {
return '';
}
}

// 获取Git最新标签(跨平台实现)
function getLatestTag() {
try {
// 获取所有标签
const tagsOutput = execGitCommand(['tag']);

if (!tagsOutput) {
console.log('📌 未找到版本标签,使用默认 v0.0.0');
return 'v0.0.0';
}

// 过滤出格式为vX.Y.Z的标签
const tags = tagsOutput
.split('\n')
Expand Down Expand Up @@ -62,28 +82,13 @@ function getLatestTag() {

// 获取Git提交次数
function getCommitCount() {
try {
const count = execSync('git rev-list --count HEAD 2>/dev/null || echo "0"', {
encoding: 'utf8',
stdio: ['pipe', 'pipe', 'ignore']
});
return parseInt(cleanString(count)) || 0;
} catch (error) {
return 0;
}
const count = execGitCommand(['rev-list', '--count', 'HEAD']);
return parseInt(count) || 0;
}

// 获取短提交哈希
function getShortCommitHash() {
try {
const hash = execSync('git rev-parse --short HEAD 2>/dev/null || echo ""', {
encoding: 'utf8',
stdio: ['pipe', 'pipe', 'ignore']
});
return cleanString(hash);
} catch (error) {
return '';
}
return execGitCommand(['rev-parse', '--short', 'HEAD']);
}

// 生成版本号
Expand Down Expand Up @@ -240,4 +245,4 @@ function main() {
}

// 执行主函数
main();
main();
Loading