-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgetGitVersion.mjs
74 lines (59 loc) · 1.82 KB
/
getGitVersion.mjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import { execSync } from 'child_process';
import { resolve, dirname } from 'path';
import { writeFileSync } from 'fs';
import { fileURLToPath } from 'url';
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
const edition = process.argv[2] ?? 'ce';
let version = '';
try {
// 获取当前 commit hash
const currentCommit = execSync('git rev-parse HEAD', {
encoding: 'utf8'
}).trim();
// 获取所有 tag 并按创建时间倒序排序
const tags = execSync(
'git tag --sort=-creatordate -l --format="%(objectname) %(refname:short)"',
{
encoding: 'utf8'
}
)
.trim()
.split('\n')
.filter(Boolean); // 过滤空行
// 找到所有与当前 commit 关联的 tag
const matchingTags = tags.filter((t) =>
t.split(' ')[0].startsWith(currentCommit)
);
if (matchingTags.length === 0) {
throw new Error('No tag found for current commit');
}
const selectedTag = matchingTags[matchingTags.length - 1] || matchingTags[0];
const formattedTag = selectedTag.split(' ')[1].replace(/^v/i, '');
const commitId = execSync('git rev-parse --short HEAD', {
encoding: 'utf8'
}).trim();
// 如果成功获取到 tag,使用 tag + commitId
version = `${formattedTag}-${edition} ${commitId}`;
} catch (error) {
// 如果没有 tag,回退到使用分支名 + commitId
const branch = execSync('git rev-parse --abbrev-ref HEAD', {
encoding: 'utf8'
}).trim();
const commitId = execSync('git rev-parse --short HEAD', {
encoding: 'utf8'
}).trim();
version = `${branch} ${commitId}`;
}
const filePath = resolve(
// import.meta.dirname 需要 node v20 +
__dirname,
'..',
'packages',
'base',
'src',
'scripts',
'version.ts'
);
const command = `export const UI_VERSION = '${version}';\n`;
writeFileSync(filePath, command);