-
Notifications
You must be signed in to change notification settings - Fork 0
/
calculate-version.js
90 lines (75 loc) · 3.18 KB
/
calculate-version.js
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#!/usr/bin/env node
const util = require('util');
const exec = require('child_process').exec;
const execAsync = util.promisify(exec);
async function executeCommandAsync(command) {
const { stdout, stderr } = await execAsync(command);
if (stderr) {
throw new Error(stderr);
}
return stdout.trimEnd();
}
module.exports = async function() {
const versionTagRegexp = new RegExp(/^v(\d+)\.(\d+)\.(\d+)$/);
const version = {
major: 0,
minor: 0,
patch: 0,
version: "0.0.0",
sha: "",
branch: "",
commitCount: 0,
isDirty: false,
buildTime: "",
buildSafeTime: "",
};
try {
const tagName = await executeCommandAsync('git describe --tags --abbrev=0');
const commitsString = await executeCommandAsync('git rev-list ' + tagName + '..HEAD');
const commits = commitsString.trim().split('\n').map(line => line.trim()).filter(line => line);
const messages = await Promise.all(commits.map(async (commit) => await executeCommandAsync('git show -s --format=%s ' + commit)));
const versionTagMatch = tagName.match(versionTagRegexp);
if (!versionTagMatch) {
throw new Error("Could not extract version information from most recently applied tag: '" + tagName + "'.");
}
version.major = parseInt(versionTagMatch[1]);
version.minor = parseInt(versionTagMatch[2]);
version.patch = parseInt(versionTagMatch[3]);
if (messages.some(m => m.startsWith("Breaking:"))) {
version.major = version.major + 1;
version.minor = 0;
version.patch = 0;
}
else if (messages.some(m => m.startsWith("Feature:"))) {
version.minor = version.minor + 1;
version.patch = 0;
}
else if (messages.length) {
version.patch = version.patch + 1;
}
version.version = version.major + "." + version.minor + "." + version.patch;
version.sha = await executeCommandAsync('git rev-parse HEAD');
version.branch = await executeCommandAsync('git rev-parse --abbrev-ref HEAD');
version.commitCount = commits.length;
const dateTime = new Date();
const year = dateTime.getUTCFullYear();
const month = `${dateTime.getUTCMonth() + 1}`.padStart(2, '0');
const day =`${dateTime.getUTCDate()}`.padStart(2, '0');
const hours =`${dateTime.getUTCHours()}`.padStart(2, '0');
const minutes =`${dateTime.getUTCMinutes()}`.padStart(2, '0');
const seconds =`${dateTime.getUTCSeconds()}`.padStart(2, '0');
const milliseconds =`${dateTime.getUTCMilliseconds()}`.padStart(2, '0');
try {
await executeCommandAsync('git diff-index --quiet HEAD --');
}
catch {
version.isDirty = true;
}
version.buildTime = dateTime.toISOString();
version.buildSafeTime = `${year}${month}${day}-${hours}${minutes}${seconds}.${milliseconds}`;
}
catch (e) {
console.error("Could not get version from git: " + e);
}
return version;
}