Repository navigation
Expand file tree
/
Copy pathversion.ts
More file actions
53 lines (43 loc) · 1.38 KB
/
Copy pathversion.ts
File metadata and controls
53 lines (43 loc) · 1.38 KB
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
import { execFileSync } from 'child_process';
const exactTagPattern = /^v?(\d+\.\d+\.\d+)$/;
const describedTagPattern = /^v?(\d+\.\d+\.\d+)-(\d+)-g([0-9a-f]+)(-dirty)?$/;
const shaPattern = /^([0-9a-f]+)(-dirty)?$/;
function gitDescribe(): string | null {
if (process.env.GITOPS_EXPORT_VERSION) {
return process.env.GITOPS_EXPORT_VERSION;
}
try {
return execFileSync('git', ['describe', '--tags', '--match', 'v[0-9]*', '--dirty', '--always'], {
cwd: __dirname,
encoding: 'utf8',
stdio: ['ignore', 'pipe', 'ignore'],
}).trim();
} catch {
return null;
}
}
export function getBuildVersion(): string {
const described = gitDescribe();
if (!described) {
return '0.0.0';
}
const exactMatch = described.match(exactTagPattern);
if (exactMatch) {
return exactMatch[1];
}
const describedMatch = described.match(describedTagPattern);
if (describedMatch) {
const [, tag, commitCount, sha, dirtySuffix] = describedMatch;
const buildSuffix = dirtySuffix ? `${sha}.dirty` : sha;
return `${tag}-dev.${commitCount}+${buildSuffix}`;
}
const shaMatch = described.match(shaPattern);
if (shaMatch) {
const [, sha, dirtySuffix] = shaMatch;
const buildSuffix = dirtySuffix ? `${sha}.dirty` : sha;
return `0.0.0-dev+${buildSuffix}`;
}
return '0.0.0';
}
const buildVersion = getBuildVersion();
export default buildVersion;