diff --git a/scripts/__tests__/version-utils-test.js b/scripts/__tests__/version-utils-test.js new file mode 100644 index 00000000000..ce7f2372345 --- /dev/null +++ b/scripts/__tests__/version-utils-test.js @@ -0,0 +1,39 @@ +/** + * 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 {parseVersion} = require('../version-utils'); + +describe('version-utils', () => { + describe('parseVersion', () => { + it('should throw error if invalid match', () => { + function testInvalidVersion() { + parseVersion(''); + } + expect(testInvalidVersion).toThrowErrorMatchingInlineSnapshot( + `"You must pass a correctly formatted version; couldn't parse "`, + ); + }); + + it('should parse pre-release version with .', () => { + const {major, minor, patch, prerelease} = parseVersion('0.66.0-rc.4'); + expect(major).toBe('0'); + expect(minor).toBe('66'); + expect(patch).toBe('0'); + expect(prerelease).toBe('rc.4'); + }); + + it('should parse stable version', () => { + const {major, minor, patch, prerelease} = parseVersion('0.66.0'); + expect(major).toBe('0'); + expect(minor).toBe('66'); + expect(patch).toBe('0'); + expect(prerelease).toBeUndefined(); + }); + }); +}); diff --git a/scripts/bump-oss-version.js b/scripts/bump-oss-version.js index 6550726ed89..7109f8b08a7 100755 --- a/scripts/bump-oss-version.js +++ b/scripts/bump-oss-version.js @@ -19,6 +19,7 @@ const fs = require('fs'); const {cat, echo, exec, exit, sed} = require('shelljs'); const yargs = require('yargs'); +const {parseVersion} = require('./version-utils'); let argv = yargs .option('r', { @@ -70,15 +71,16 @@ if (!nightlyBuild) { } } -// Generate version files to detect mismatches between JS and native. -let match = version.match(/^(\d+)\.(\d+)\.(\d+)(?:-(.+))?$/); -if (!match) { - echo( - `You must pass a correctly formatted version; couldn't parse ${version}`, - ); +let major, + minor, + patch, + prerelease = -1; +try { + ({major, minor, patch, prerelease} = parseVersion(version)); +} catch (e) { + echo(e.message); exit(1); } -let [, major, minor, patch, prerelease] = match; fs.writeFileSync( 'ReactAndroid/src/main/java/com/facebook/react/modules/systeminfo/ReactNativeVersion.java', @@ -220,7 +222,7 @@ if (!nightlyBuild) { exec(`git push ${remote} v${version}`); // Tag latest if doing stable release - if (version.indexOf('rc') === -1) { + if (prerelease == null) { exec('git tag -d latest'); exec(`git push ${remote} :latest`); exec('git tag latest'); diff --git a/scripts/version-utils.js b/scripts/version-utils.js new file mode 100644 index 00000000000..a2d0d6342de --- /dev/null +++ b/scripts/version-utils.js @@ -0,0 +1,28 @@ +/** + * 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 + */ + +function parseVersion(version) { + const match = version.match(/^(\d+)\.(\d+)\.(\d+)(?:-(.+))?$/); + if (!match) { + throw new Error( + `You must pass a correctly formatted version; couldn't parse ${version}`, + ); + } + const [, major, minor, patch, prerelease] = match; + return { + major, + minor, + patch, + prerelease, + }; +} + +module.exports = { + parseVersion, +};