Skip to content

Commit 28c90b3

Browse files
j-piaseckiCopilot
andauthored
Update the script responsible for updating the package version (#3934)
## Description Updates the `set-package-version` script to also support beta and release candidate versions. Since those two may not be published from a stable branch, they require explicit version to be set. The version format for those releases would be: ``` {major}.{minor}.{patch}-rc.{rcVersion} {major}.{minor}.{patch}-beta.{betaVersion} ``` ## Test plan Run the script in different configurations --------- Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
1 parent b776a81 commit 28c90b3

3 files changed

Lines changed: 92 additions & 10 deletions

File tree

scripts/release/set-package-version.js

Lines changed: 78 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,17 @@ const fs = require('fs');
22
const { execSync } = require('child_process');
33
const { getPackageVersionByTag } = require('./npm-utils');
44
const { parseVersion, getStableBranchVersion } = require('./version-utils');
5+
const assert = require('assert');
56

67
const PACKAGE_PATH = './packages/react-native-gesture-handler/package.json';
78

9+
const ReleaseType = {
10+
STABLE: 'stable',
11+
BETA: 'beta',
12+
RELEASE_CANDIDATE: 'rc',
13+
COMMITLY: 'commitly',
14+
};
15+
816
function getLatestVersion() {
917
const latestVersion = getPackageVersionByTag('react-native-gesture-handler', 'latest');
1018

@@ -37,9 +45,24 @@ function getNextStableVersion() {
3745
}
3846
}
3947

40-
function getVersion(isCommitly) {
41-
if (isCommitly) {
42-
const [major, minor] = getLatestVersion()
48+
function getNextPreReleaseVersion(releaseType, version) {
49+
let dotIndex = 1;
50+
while (true) {
51+
const targetVersion = `${version}-${releaseType}.${dotIndex}`;
52+
53+
try {
54+
// if the version is already published, increment the pre-release sequence (rc/beta number) and try again
55+
getPackageVersionByTag('react-native-gesture-handler', targetVersion);
56+
dotIndex++;
57+
} catch (error) {
58+
return targetVersion;
59+
}
60+
}
61+
}
62+
63+
function getVersion(releaseType, preReleaseVersion = null) {
64+
if (releaseType === ReleaseType.COMMITLY) {
65+
const [major, minor] = getLatestVersion();
4366

4467
const currentSHA = execSync('git rev-parse HEAD').toString().trim();
4568
const now = new Date();
@@ -50,15 +73,62 @@ function getVersion(isCommitly) {
5073

5174
const commitlyVersion = `${major}.${minor + 1}.${0}-nightly-${currentDate}-${currentSHA.slice(0, 9)}`;
5275
return commitlyVersion;
76+
} else if (releaseType === ReleaseType.BETA || releaseType === ReleaseType.RELEASE_CANDIDATE) {
77+
if (preReleaseVersion == null) {
78+
throw new Error(`Version must be provided for ${releaseType} releases`);
79+
}
80+
81+
return getNextPreReleaseVersion(releaseType, preReleaseVersion);
5382
}
5483

5584
const [major, minor, patch] = getNextStableVersion();
5685
return `${major}.${minor}.${patch}`;
5786
}
5887

5988
function setPackageVersion() {
60-
const isCommitly = process.argv.includes('--commitly');
61-
const version = getVersion(isCommitly);
89+
let version = null;
90+
let isCommitly = false;
91+
let isBeta = false;
92+
let isReleaseCandidate = false;
93+
94+
for (let i = 2; i < process.argv.length; i++) {
95+
const arg = process.argv[i];
96+
if (arg === '--commitly') {
97+
isCommitly = true;
98+
} else if (arg === '--beta') {
99+
isBeta = true;
100+
} else if (arg === '--rc') {
101+
isReleaseCandidate = true;
102+
} else if (arg === '--version') {
103+
if (i + 1 < process.argv.length) {
104+
version = process.argv[i + 1];
105+
i++;
106+
} else {
107+
throw new Error('Expected a version after --version');
108+
}
109+
}
110+
}
111+
112+
assert([isCommitly, isBeta, isReleaseCandidate].filter(Boolean).length <= 1, 'Release flags --commitly, --beta, and --rc are mutually exclusive; specify at most one');
113+
assert(version === null || isBeta || isReleaseCandidate, 'Version should not be provided for stable nor commitly releases');
114+
assert(version !== null || (!isBeta && !isReleaseCandidate), 'Version must be provided for beta and release candidate releases');
115+
116+
const releaseType = isCommitly
117+
? ReleaseType.COMMITLY
118+
: isBeta
119+
? ReleaseType.BETA
120+
: isReleaseCandidate
121+
? ReleaseType.RELEASE_CANDIDATE
122+
: ReleaseType.STABLE;
123+
124+
if (version != null) {
125+
const versionRegex = /^(\d+)\.(\d+)\.(\d+)$/;
126+
if (!versionRegex.test(version)) {
127+
throw new Error(`Provided version "${version}" is not valid. Expected format: x.y.z`);
128+
}
129+
}
130+
131+
version = getVersion(releaseType, version);
62132

63133
const packageJson = JSON.parse(fs.readFileSync(PACKAGE_PATH, 'utf8'));
64134
packageJson.version = version;
@@ -68,4 +138,6 @@ function setPackageVersion() {
68138
console.log(version);
69139
}
70140

71-
setPackageVersion();
141+
if (require.main === module) {
142+
setPackageVersion();
143+
}

scripts/release/should-be-latest.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,15 @@ const { getPackageVersionByTag } = require('./npm-utils');
22
const { parseVersion } = require('./version-utils');
33

44
function shouldBeLatest(version) {
5+
const [newMajor, newMinor, newPatch, newPreRelease] = parseVersion(version);
6+
7+
// Pre-releases should never be latest
8+
if (newPreRelease !== null) {
9+
return false;
10+
}
11+
512
const latestVersion = getPackageVersionByTag('react-native-gesture-handler', 'latest');
613
const [major, minor, patch] = parseVersion(latestVersion);
7-
const [newMajor, newMinor, newPatch] = parseVersion(version);
814

915
// TODO: We'll worry about 3.x.x later :)
1016
if (newMajor !== major) {

scripts/release/version-utils.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
const { execSync } = require('child_process');
22

3-
const VERSION_REGEX = /^(\d+)\.(\d+)\.(\d+)$/;
3+
const VERSION_REGEX = /^(\d+)\.(\d+)\.(\d+)(-.*)?$/;
44
const BRANCH_REGEX = /^(\d+)\.(\d+)-stable$/;
55

66
function parseVersion(version) {
7-
const [, major, minor, patch] = version.match(VERSION_REGEX);
8-
return [Number(major), Number(minor), Number(patch)];
7+
const match = version.match(VERSION_REGEX);
8+
if (!match) {
9+
throw new Error(`Invalid version string: ${version}`);
10+
}
11+
const [, major, minor, patch, preRelease] = match;
12+
return [Number(major), Number(minor), Number(patch), preRelease || null];
913
}
1014

1115
function getStableBranchVersion() {

0 commit comments

Comments
 (0)