Skip to content

Commit cc1e3ab

Browse files
Luna Weifacebook-github-bot
authored andcommitted
Extract version parsing from release script
Summary: Changelog: [Internal] - extract logic for parsing version in bump-oss-version and add tests Reviewed By: cortinico Differential Revision: D32196238 fbshipit-source-id: 6ea7af3d282eea1d876118f056bca94a151e6182
1 parent 636f614 commit cc1e3ab

File tree

3 files changed

+77
-8
lines changed

3 files changed

+77
-8
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/**
2+
* Copyright (c) Facebook, Inc. and its affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*
7+
* @format
8+
*/
9+
10+
const {parseVersion} = require('../version-utils');
11+
12+
describe('version-utils', () => {
13+
describe('parseVersion', () => {
14+
it('should throw error if invalid match', () => {
15+
function testInvalidVersion() {
16+
parseVersion('<invalid version>');
17+
}
18+
expect(testInvalidVersion).toThrowErrorMatchingInlineSnapshot(
19+
`"You must pass a correctly formatted version; couldn't parse <invalid version>"`,
20+
);
21+
});
22+
23+
it('should parse pre-release version with .', () => {
24+
const {major, minor, patch, prerelease} = parseVersion('0.66.0-rc.4');
25+
expect(major).toBe('0');
26+
expect(minor).toBe('66');
27+
expect(patch).toBe('0');
28+
expect(prerelease).toBe('rc.4');
29+
});
30+
31+
it('should parse stable version', () => {
32+
const {major, minor, patch, prerelease} = parseVersion('0.66.0');
33+
expect(major).toBe('0');
34+
expect(minor).toBe('66');
35+
expect(patch).toBe('0');
36+
expect(prerelease).toBeUndefined();
37+
});
38+
});
39+
});

scripts/bump-oss-version.js

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
const fs = require('fs');
2020
const {cat, echo, exec, exit, sed} = require('shelljs');
2121
const yargs = require('yargs');
22+
const {parseVersion} = require('./version-utils');
2223

2324
let argv = yargs
2425
.option('r', {
@@ -68,15 +69,16 @@ if (!nightlyBuild) {
6869
}
6970
}
7071

71-
// Generate version files to detect mismatches between JS and native.
72-
let match = version.match(/^(\d+)\.(\d+)\.(\d+)(?:-(.+))?$/);
73-
if (!match) {
74-
echo(
75-
`You must pass a correctly formatted version; couldn't parse ${version}`,
76-
);
72+
let major,
73+
minor,
74+
patch,
75+
prerelease = -1;
76+
try {
77+
({major, minor, patch, prerelease} = parseVersion(version));
78+
} catch (e) {
79+
echo(e.message);
7780
exit(1);
7881
}
79-
let [, major, minor, patch, prerelease] = match;
8082

8183
fs.writeFileSync(
8284
'ReactAndroid/src/main/java/com/facebook/react/modules/systeminfo/ReactNativeVersion.java',
@@ -228,7 +230,7 @@ if (!nightlyBuild) {
228230
exec(`git push ${remote} v${version}`);
229231

230232
// Tag latest if doing stable release
231-
if (version.indexOf('rc') === -1) {
233+
if (prerelease == null) {
232234
exec('git tag -d latest');
233235
exec(`git push ${remote} :latest`);
234236
exec('git tag latest');

scripts/version-utils.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* Copyright (c) Facebook, Inc. and its affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*
7+
* @format
8+
*/
9+
10+
function parseVersion(version) {
11+
const match = version.match(/^(\d+)\.(\d+)\.(\d+)(?:-(.+))?$/);
12+
if (!match) {
13+
throw new Error(
14+
`You must pass a correctly formatted version; couldn't parse ${version}`,
15+
);
16+
}
17+
const [, major, minor, patch, prerelease] = match;
18+
return {
19+
major,
20+
minor,
21+
patch,
22+
prerelease,
23+
};
24+
}
25+
26+
module.exports = {
27+
parseVersion,
28+
};

0 commit comments

Comments
 (0)