-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathget-release-version.js
More file actions
170 lines (145 loc) · 5.08 KB
/
get-release-version.js
File metadata and controls
170 lines (145 loc) · 5.08 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
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/**
* @license
* Copyright 2025 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
import { execSync } from 'node:child_process';
import path from 'node:path';
import fs from 'node:fs';
function getPackageVersion() {
const packageJsonPath = path.resolve(process.cwd(), 'package.json');
const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf8'));
return packageJson.version;
}
function getLatestStableTag() {
// Fetches all tags, then filters for the latest stable (non-prerelease) tag.
const tags = execSync('git tag --list "v*.*.*" --sort=-v:refname')
.toString()
.split('\n');
const latestStableTag = tags.find((tag) =>
tag.match(/^v[0-9]+\.[0-9]+\.[0-9]+$/),
);
if (!latestStableTag) {
throw new Error('Could not find a stable tag.');
}
return latestStableTag;
}
function incrementPatchVersion(version) {
const parts = version.split('.');
const major = parseInt(parts[0]);
const minor = parseInt(parts[1]);
const patch = parseInt(parts[2].split('-')[0]); // Handle pre-release versions
return `${major}.${minor}.${patch + 1}`;
}
function getLatestNightlyCount() {
try {
// Try to get the latest nightly tag from git to determine the counter
const currentVersion = getPackageVersion();
const nextVersion = incrementPatchVersion(currentVersion);
const tags = execSync(`git tag -l "v${nextVersion}-nightly.*"`)
.toString()
.trim();
if (!tags) return 0;
const nightlyTags = tags.split('\n').filter(Boolean);
const counts = nightlyTags.map((tag) => {
const match = tag.match(/nightly\.(\d+)$/);
return match ? parseInt(match[1]) : 0;
});
return Math.max(...counts, -1) + 1;
} catch (_error) {
// If we can't get tags, start from 0
return 0;
}
}
function getNextVersionString(stableVersion, minorIncrement) {
const [major, minor] = stableVersion.substring(1).split('.');
const nextMinorVersion = parseInt(minor, 10) + minorIncrement;
return `${major}.${nextMinorVersion}.0`;
}
export function getNightlyTagName() {
const version = getPackageVersion();
const nextVersion = incrementPatchVersion(version);
const nightlyCount = getLatestNightlyCount();
return `v${nextVersion}-nightly.${nightlyCount}`;
}
export function getPreviewTagName(stableVersion) {
const version = getNextVersionString(stableVersion, 1);
return `v${version}-preview`;
}
function getPreviousReleaseTag(isNightly) {
if (isNightly) {
console.error('Finding latest nightly release...');
return execSync(
`gh release list --limit 100 --json tagName | jq -r '[.[] | select(.tagName | contains("nightly"))] | .[0].tagName'`,
)
.toString()
.trim();
} else {
console.error('Finding latest STABLE release (excluding pre-releases)...');
return execSync(
`gh release list --limit 100 --json tagName | jq -r '[.[] | select(.tagName | (contains("nightly") or contains("preview")) | not)] | .[0].tagName'`,
)
.toString()
.trim();
}
}
export function getReleaseVersion() {
const isNightly = process.env.IS_NIGHTLY === 'true';
const isPreview = process.env.IS_PREVIEW === 'true';
const manualVersion = process.env.MANUAL_VERSION;
let releaseTag;
if (isNightly) {
console.error('Calculating next nightly version...');
const stableVersion = getLatestStableTag();
releaseTag = getNightlyTagName(stableVersion);
} else if (isPreview) {
console.error('Calculating next preview version...');
const stableVersion = getLatestStableTag();
releaseTag = getPreviewTagName(stableVersion);
} else if (manualVersion) {
console.error(`Using manual version: ${manualVersion}`);
releaseTag = manualVersion;
} else {
throw new Error(
'Error: No version specified and this is not a nightly or preview release.',
);
}
if (!releaseTag) {
throw new Error('Error: Version could not be determined.');
}
if (!releaseTag.startsWith('v')) {
console.error("Version is missing 'v' prefix. Prepending it.");
releaseTag = `v${releaseTag}`;
}
if (releaseTag.includes('+')) {
throw new Error(
'Error: Versions with build metadata (+) are not supported for releases. Please use a pre-release version (e.g., v1.2.3-alpha.4) instead.',
);
}
if (!releaseTag.match(/^v[0-9]+\.[0-9]+\.[0-9]+(-[a-zA-Z0-9.-]+)?$/)) {
throw new Error(
'Error: Version must be in the format vX.Y.Z or vX.Y.Z-prerelease',
);
}
const releaseVersion = releaseTag.substring(1);
let npmTag = 'latest';
if (releaseVersion.includes('-')) {
const prereleasePart = releaseVersion.split('-')[1];
npmTag = prereleasePart.split('.')[0];
// Ensure nightly releases use 'nightly' tag, not 'latest'
if (npmTag === 'nightly') {
npmTag = 'nightly';
}
}
const previousReleaseTag = getPreviousReleaseTag(isNightly);
return { releaseTag, releaseVersion, npmTag, previousReleaseTag };
}
if (process.argv[1] === new URL(import.meta.url).pathname) {
try {
const versions = getReleaseVersion();
console.log(JSON.stringify(versions));
} catch (error) {
console.error(error.message);
process.exit(1);
}
}