/
version-utils.js
88 lines (75 loc) · 2.25 KB
/
version-utils.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
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @format
*/
const {exec} = require('shelljs');
function parseVersion(versionStr) {
const match = versionStr.match(/^v?((\d+)\.(\d+)\.(\d+)(?:-(.+))?)$/);
if (!match) {
throw new Error(
`You must pass a correctly formatted version; couldn't parse ${versionStr}`,
);
}
const [, version, major, minor, patch, prerelease] = match;
return {
version,
major,
minor,
patch,
prerelease,
};
}
function getLatestVersionTag(branchVersion) {
// Returns list of tags like ["v0.67.2", "v0.67.1", "v0.67.0-rc.3", "v0.67.0-rc.2", ...] in reverse lexical order
const tags = exec(`git tag --list "v${branchVersion}*" --sort=-refname`, {
silent: true,
})
.stdout.trim()
.split('\n')
.filter(tag => tag.length > 0);
// If there are no tags, return null
if (tags.length === 0) {
return null;
}
// Return most recent tag (with the "v" prefix)
return tags[0];
}
function getNextVersionFromTags(branch) {
// Assumption that branch names will follow pattern `{major}.{minor}-stable`
// Ex. "0.67-stable" -> "0.67"
const branchVersion = branch.replace('-stable', '');
// Get the latest version tag of the release branch
const versionTag = getLatestVersionTag(branchVersion);
// If there are no tags , we assume this is the first pre-release
if (versionTag == null) {
return `${branchVersion}.0-rc.0`;
}
const {major, minor, patch, prerelease} = parseVersion(versionTag);
if (prerelease != null) {
// prelease is of the form "rc.X"
const prereleasePatch = parseInt(prerelease.slice(3), 10);
return `${major}.${minor}.${patch}-rc.${prereleasePatch + 1}`;
}
// If not prerelease, increment the patch version
return `${major}.${minor}.${parseInt(patch, 10) + 1}`;
}
function isReleaseBranch(branch) {
return branch.endsWith('-stable');
}
function isTaggedLatest(commitSha) {
return (
exec(`git rev-list -1 latest | grep ${commitSha}`, {
silent: true,
}).stdout.trim() === commitSha
);
}
module.exports = {
isTaggedLatest,
parseVersion,
getNextVersionFromTags,
isReleaseBranch,
};