diff --git a/scripts/__tests__/version-utils-test.js b/scripts/__tests__/version-utils-test.js index 19450483399d06..3dab56b51333b3 100644 --- a/scripts/__tests__/version-utils-test.js +++ b/scripts/__tests__/version-utils-test.js @@ -7,7 +7,11 @@ * @format */ -const {parseVersion, isReleaseBranch} = require('../version-utils'); +const { + parseVersion, + isReleaseBranch, + validateBuildType, +} = require('../version-utils'); let execResult = null; jest.mock('shelljs', () => ({ @@ -38,18 +42,53 @@ describe('version-utils', () => { }); describe('parseVersion', () => { - it('should throw error if invalid match', () => { + it('should throw error if buildType is undefined', () => { function testInvalidVersion() { - parseVersion(''); + parseVersion('v0.10.5'); + } + expect(testInvalidVersion).toThrowErrorMatchingInlineSnapshot( + `"Unsupported build type: undefined"`, + ); + }); + + it('should throw error if buildType is not `release`, `dry-run` or `nightly`', () => { + function testInvalidVersion() { + parseVersion('v0.10.5', 'invalid_build_type'); + } + expect(testInvalidVersion).toThrowErrorMatchingInlineSnapshot( + `"Unsupported build type: invalid_build_type"`, + ); + }); + it('should throw error if invalid match with release', () => { + function testInvalidVersion() { + parseVersion('', 'release'); + } + expect(testInvalidVersion).toThrowErrorMatchingInlineSnapshot( + `"You must pass a correctly formatted version; couldn't parse "`, + ); + }); + it('should throw error if invalid match with dry-run', () => { + function testInvalidVersion() { + parseVersion('', 'dry-run'); + } + expect(testInvalidVersion).toThrowErrorMatchingInlineSnapshot( + `"You must pass a correctly formatted version; couldn't parse "`, + ); + }); + it('should throw error if invalid match with nightly', () => { + function testInvalidVersion() { + parseVersion('', 'nightly'); } expect(testInvalidVersion).toThrowErrorMatchingInlineSnapshot( `"You must pass a correctly formatted version; couldn't parse "`, ); }); - it('should parse pre-release version with .', () => { - const {version, major, minor, patch, prerelease} = - parseVersion('0.66.0-rc.4'); + it('should parse pre-release version with release and `.`', () => { + const {version, major, minor, patch, prerelease} = parseVersion( + '0.66.0-rc.4', + 'release', + ); expect(version).toBe('0.66.0-rc.4'); expect(major).toBe('0'); expect(minor).toBe('66'); @@ -57,9 +96,11 @@ describe('version-utils', () => { expect(prerelease).toBe('rc.4'); }); - it('should parse pre-release version with -', () => { - const {version, major, minor, patch, prerelease} = - parseVersion('0.66.0-rc-4'); + it('should parse pre-release version with release and `-`', () => { + const {version, major, minor, patch, prerelease} = parseVersion( + '0.66.0-rc-4', + 'release', + ); expect(version).toBe('0.66.0-rc-4'); expect(major).toBe('0'); expect(minor).toBe('66'); @@ -67,8 +108,20 @@ describe('version-utils', () => { expect(prerelease).toBe('rc-4'); }); + it('should reject pre-release version with random prerelease pattern', () => { + function testInvalidVersion() { + parseVersion('0.66.0-something_invalid', 'release'); + } + expect(testInvalidVersion).toThrowErrorMatchingInlineSnapshot( + `"Version 0.66.0-something_invalid is not valid for Release"`, + ); + }); + it('should parse stable version', () => { - const {version, major, minor, patch, prerelease} = parseVersion('0.66.0'); + const {version, major, minor, patch, prerelease} = parseVersion( + '0.66.0', + 'release', + ); expect(version).toBe('0.66.0'); expect(major).toBe('0'); expect(minor).toBe('66'); @@ -77,8 +130,10 @@ describe('version-utils', () => { }); it('should parse pre-release version from tag', () => { - const {version, major, minor, patch, prerelease} = - parseVersion('v0.66.1-rc.4'); + const {version, major, minor, patch, prerelease} = parseVersion( + 'v0.66.1-rc.4', + 'release', + ); expect(version).toBe('0.66.1-rc.4'); expect(major).toBe('0'); expect(minor).toBe('66'); @@ -86,9 +141,20 @@ describe('version-utils', () => { expect(prerelease).toBe('rc.4'); }); + it('should reject pre-release version from tag with random prerelease pattern', () => { + function testInvalidVersion() { + parseVersion('v0.66.0-something_invalid', 'release'); + } + expect(testInvalidVersion).toThrowErrorMatchingInlineSnapshot( + `"Version 0.66.0-something_invalid is not valid for Release"`, + ); + }); + it('should parse stable version from tag', () => { - const {version, major, minor, patch, prerelease} = - parseVersion('v0.66.0'); + const {version, major, minor, patch, prerelease} = parseVersion( + 'v0.66.0', + 'release', + ); expect(version).toBe('0.66.0'); expect(major).toBe('0'); expect(minor).toBe('66'); @@ -96,23 +162,152 @@ describe('version-utils', () => { expect(prerelease).toBeUndefined(); }); - it('should parse nightly fake version', () => { - const {version, major, minor, patch, prerelease} = parseVersion('0.0.0'); - expect(version).toBe('0.0.0'); + it('should reject nightly with no prerelease', () => { + // this should fail + function testInvalidFunction() { + parseVersion('0.0.0', 'nightly'); + } + expect(testInvalidFunction).toThrowErrorMatchingInlineSnapshot( + `"Version 0.0.0 is not valid for nightlies"`, + ); + }); + + it('should reject nightly with prerelease but wrong version numbers', () => { + // this should fail + function testInvalidFunction() { + parseVersion('1.2.3-pre-release', 'nightly'); + } + expect(testInvalidFunction).toThrowErrorMatchingInlineSnapshot( + `"Version 1.2.3-pre-release is not valid for nightlies"`, + ); + }); + + it('should parse nightly with 0.0.0 and a prerelease part', () => { + // this should fail + const {version, major, minor, patch, prerelease} = parseVersion( + '0.0.0-pre-release', + 'nightly', + ); + + expect(version).toBe('0.0.0-pre-release'); expect(major).toBe('0'); expect(minor).toBe('0'); expect(patch).toBe('0'); + expect(prerelease).toBe('pre-release'); + }); + it('should parse dryrun with release version', () => { + const {version, major, minor, patch, prerelease} = parseVersion( + '0.7.3', + 'dry-run', + ); + expect(version).toBe('0.7.3'); + expect(major).toBe('0'); + expect(minor).toBe('7'); + expect(patch).toBe('3'); expect(prerelease).toBeUndefined(); }); - it('should parse dryrun fake version', () => { - const {version, major, minor, patch, prerelease} = - parseVersion('1000.0.0'); + it('should parse dryrun with prerelease . version', () => { + const {version, major, minor, patch, prerelease} = parseVersion( + '0.20.3-rc.0', + 'dry-run', + ); + expect(version).toBe('0.20.3-rc.0'); + expect(major).toBe('0'); + expect(minor).toBe('20'); + expect(patch).toBe('3'); + expect(prerelease).toBe('rc.0'); + }); + + it('should parse dryrun with prerelease - version', () => { + const {version, major, minor, patch, prerelease} = parseVersion( + '0.20.3-rc-0', + 'dry-run', + ); + expect(version).toBe('0.20.3-rc-0'); + expect(major).toBe('0'); + expect(minor).toBe('20'); + expect(patch).toBe('3'); + expect(prerelease).toBe('rc-0'); + }); + + it('should parse dryrun with main version', () => { + const {version, major, minor, patch, prerelease} = parseVersion( + '1000.0.0', + 'dry-run', + ); expect(version).toBe('1000.0.0'); expect(major).toBe('1000'); expect(minor).toBe('0'); expect(patch).toBe('0'); expect(prerelease).toBeUndefined(); }); + it('should parse dryrun with nightly version', () => { + const {version, major, minor, patch, prerelease} = parseVersion( + '0.0.0-something-else', + 'dry-run', + ); + expect(version).toBe('0.0.0-something-else'); + expect(major).toBe('0'); + expect(minor).toBe('0'); + expect(patch).toBe('0'); + expect(prerelease).toBe('something-else'); + }); + + it('should reject dryrun invalid values', () => { + function testInvalidFunction() { + parseVersion('1000.0.4', 'dry-run'); + } + expect(testInvalidFunction).toThrowErrorMatchingInlineSnapshot( + `"Version 1000.0.4 is not valid for dry-runs"`, + ); + }); + + it('should reject dryrun for invalid prerelease', () => { + function testInvalidFunction() { + parseVersion('0.6.4-something-else', 'dry-run'); + } + expect(testInvalidFunction).toThrowErrorMatchingInlineSnapshot( + `"Version 0.6.4-something-else is not valid for dry-runs"`, + ); + }); + + it('should reject dryrun for nightlies with invalid prerelease', () => { + function testInvalidFunction() { + parseVersion('0.0.0', 'dry-run'); + } + expect(testInvalidFunction).toThrowErrorMatchingInlineSnapshot( + `"Version 0.0.0 is not valid for dry-runs"`, + ); + }); + }); + + describe('Validate version', () => { + it('Throw error if the buildType is unknown', () => { + function testInvalidFunction() { + validateBuildType('wrong_build'); + } + expect(testInvalidFunction).toThrowErrorMatchingInlineSnapshot( + `"Unsupported build type: wrong_build"`, + ); + }); + it('Does not throw if the buildType is release', () => { + function testValidCall() { + validateBuildType('release'); + } + expect(testValidCall).not.toThrowError(); + }); + it('Does not throw if the buildType is nightly', () => { + function testValidCall() { + validateBuildType('nightly'); + } + expect(testValidCall).not.toThrowError(); + }); + it('Does not throw if the buildType is dry-run', () => { + function testValidCall() { + validateBuildType('dry-run'); + } + expect(testValidCall).not.toThrowError(); + }); }); }); diff --git a/scripts/bump-oss-version.js b/scripts/bump-oss-version.js index ed14b1015270a0..6a5f1a8e50539b 100755 --- a/scripts/bump-oss-version.js +++ b/scripts/bump-oss-version.js @@ -89,7 +89,7 @@ async function main() { } let latest = false; - const {version, prerelease} = parseVersion(releaseVersion); + const {version, prerelease} = parseVersion(releaseVersion, 'release'); if (!prerelease) { const {setLatest} = await inquirer.prompt({ type: 'confirm', diff --git a/scripts/prepare-package-for-release.js b/scripts/prepare-package-for-release.js index 4f89743c618827..78f96b899bb7f1 100755 --- a/scripts/prepare-package-for-release.js +++ b/scripts/prepare-package-for-release.js @@ -57,13 +57,26 @@ if (branch && !isReleaseBranch(branch) && !isDryRun) { exit(1); } -const {version} = parseVersion(releaseVersion); +const buildType = isDryRun + ? 'dry-run' + : isReleaseBranch(branch) + ? 'release' + : 'nightly'; + +const {version} = parseVersion( + releaseVersion, + isDryRun ? 'dry-run' : buildType, +); if (version == null) { console.error(`Invalid version provided: ${releaseVersion}`); exit(1); } -if (exec(`node scripts/set-rn-version.js --to-version ${version}`).code) { +if ( + exec( + `node scripts/set-rn-version.js --to-version ${version} --build-type ${buildType}`, + ).code +) { echo(`Failed to set React Native version to ${version}`); exit(1); } diff --git a/scripts/publish-npm.js b/scripts/publish-npm.js index 4f2939b47928fd..4a5e13f9ed810c 100755 --- a/scripts/publish-npm.js +++ b/scripts/publish-npm.js @@ -72,8 +72,15 @@ const argv = yargs .strict().argv; const nightlyBuild = argv.nightly; const dryRunBuild = argv.dryRun; +const releaseBuild = argv.release; const isCommitly = nightlyBuild || dryRunBuild; +const buildType = releaseBuild + ? 'release' + : nightlyBuild + ? 'nightly' + : 'dry-run'; + if (!argv.help) { echo(`The temp publishing folder is ${tmpPublishingFolder}`); } @@ -97,7 +104,7 @@ let version, minor, prerelease = null; try { - ({version, major, minor, prerelease} = parseVersion(rawVersion)); + ({version, major, minor, prerelease} = parseVersion(rawVersion, buildType)); } catch (e) { echo(e.message); exit(1); @@ -122,7 +129,9 @@ if (dryRunBuild) { // For stable, pre-release releases, we rely on CircleCI job `prepare_package_for_release` to handle this if (isCommitly) { if ( - exec(`node scripts/set-rn-version.js --to-version ${releaseVersion}`).code + exec( + `node scripts/set-rn-version.js --to-version ${releaseVersion} --build-type ${buildType}`, + ).code ) { echo(`Failed to set version number to ${releaseVersion}`); exit(1); diff --git a/scripts/run-ci-e2e-tests.js b/scripts/run-ci-e2e-tests.js index 71cdff980a3020..96ffdd6d830abd 100644 --- a/scripts/run-ci-e2e-tests.js +++ b/scripts/run-ci-e2e-tests.js @@ -58,7 +58,11 @@ try { } describe('Create react-native package'); - if (exec('node ./scripts/set-rn-version.js --version 1000.0.0').code) { + if ( + exec( + 'node ./scripts/set-rn-version.js --to-version 1000.0.0 --build-type dry-run', + ).code + ) { echo('Failed to set version and update package.json ready for release'); exitCode = 1; throw Error(exitCode); diff --git a/scripts/set-rn-version.js b/scripts/set-rn-version.js index 23705ed53fc194..0ac1a749f1089a 100755 --- a/scripts/set-rn-version.js +++ b/scripts/set-rn-version.js @@ -21,37 +21,42 @@ const os = require('os'); const path = require('path'); const {cat, echo, exec, exit, sed} = require('shelljs'); const yargs = require('yargs'); -const {parseVersion} = require('./version-utils'); +const {parseVersion, validateBuildType} = require('./version-utils'); const {saveFiles} = require('./scm-utils'); -const tmpVersioningFolder = fs.mkdtempSync( - path.join(os.tmpdir(), 'rn-set-version'), -); -echo(`The temp versioning folder is ${tmpVersioningFolder}`); - -let argv = yargs.option('v', { - alias: 'to-version', - type: 'string', -}).argv; - +// TODO: add the build type here +let argv = yargs + .option('v', { + alias: 'to-version', + type: 'string', + required: true, + }) + .option('b', { + alias: 'build-type', + type: 'string', + required: true, + }).argv; + +const buildType = argv.buildType; const version = argv.toVersion; - -if (!version) { - echo('You must specify a version using -v'); - exit(1); -} +validateBuildType(buildType); let major, minor, patch, prerelease = -1; try { - ({major, minor, patch, prerelease} = parseVersion(version)); + ({major, minor, patch, prerelease} = parseVersion(version, buildType)); } catch (e) { echo(e.message); exit(1); } +const tmpVersioningFolder = fs.mkdtempSync( + path.join(os.tmpdir(), 'rn-set-version'), +); +echo(`The temp versioning folder is ${tmpVersioningFolder}`); + saveFiles(['package.json', 'template/package.json'], tmpVersioningFolder); fs.writeFileSync( diff --git a/scripts/test-e2e-local.js b/scripts/test-e2e-local.js index b4c2175d4afea0..120ce09d10208a 100644 --- a/scripts/test-e2e-local.js +++ b/scripts/test-e2e-local.js @@ -164,7 +164,9 @@ if (argv.target === 'RNTester') { const releaseVersion = `${baseVersion}-${dateIdentifier}`; // this is needed to generate the Android artifacts correctly - exec(`node scripts/set-rn-version.js --to-version ${releaseVersion}`).code; + exec( + `node scripts/set-rn-version.js --to-version ${releaseVersion} --build-type dry-run`, + ).code; // Generate native files for Android generateAndroidArtifacts(releaseVersion, tmpPublishingFolder); diff --git a/scripts/version-utils.js b/scripts/version-utils.js index daae4e7d60c923..38bbb8a731534c 100644 --- a/scripts/version-utils.js +++ b/scripts/version-utils.js @@ -9,21 +9,132 @@ const VERSION_REGEX = /^v?((\d+)\.(\d+)\.(\d+)(?:-(.+))?)$/; -function parseVersion(versionStr) { - const match = versionStr.match(VERSION_REGEX); - if (!match) { - throw new Error( - `You must pass a correctly formatted version; couldn't parse ${versionStr}`, - ); - } +/** + * Parses a version string and performs some checks to verify its validity. + * A valid version is in the format vX.Y.Z[-KKK] where X, Y, Z are numbers and KKK can be something else. + * The `builtType` is used to enforce that the major version can assume only specific + * values. + * + * Parameters: + * - @versionStr the string representing a version + * - @buildType the build type. It can be of values: `dry-run`, `release`, `nightly` + * + * Returns: an object with the shape: + * ``` + * { + * version: string, + * major: number, + * minor: number, + * patch: number, + * prerelease: string + * } + * ``` + */ +function parseVersion(versionStr, buildType) { + validateBuildType(buildType); + + const match = _extractMatchIfValid(versionStr); const [, version, major, minor, patch, prerelease] = match; - return { + + const versionObject = { version, major, minor, patch, prerelease, }; + + _validateVersion(versionObject, buildType); + + return versionObject; +} + +function validateBuildType(buildType) { + if (['release', 'dry-run', 'nightly'].indexOf(buildType) < 0) { + throw new Error(`Unsupported build type: ${buildType}`); + } +} + +function _extractMatchIfValid(versionStr) { + const match = versionStr.match(VERSION_REGEX); + if (!match) { + throw new Error( + `You must pass a correctly formatted version; couldn't parse ${versionStr}`, + ); + } + return match; +} + +function _validateVersion(versionObject, buildType) { + const map = { + release: _validateRelease, + 'dry-run': _validateDryRun, + nightly: _validateNightly, + }; + map[buildType](versionObject); +} + +/** + * Releases are in the form of 0.Y.Z[-RC.0] + */ +function _validateRelease(version) { + const validRelease = + _isStableRelease(version) || _isStablePrerelease(version); + if (!validRelease) { + throw new Error(`Version ${version.version} is not valid for Release`); + } +} + +function _validateDryRun(version) { + const isMain = _isMain(version); + const isNightly = _is000(version) && version.prerelease != null; + const isRelease = _isStableRelease(version); + const isPrerelease = _isStablePrerelease(version); + + console.log( + `isMain ${isMain}, isNightly ${isNightly} isRelease ${isRelease} isPrerelease ${isPrerelease}`, + ); + + if (!isMain && !isNightly && !isRelease && !isPrerelease) { + throw new Error(`Version ${version.version} is not valid for dry-runs`); + } +} + +function _validateNightly(version) { + // a valid nightly is a prerelease + const isPrerelease = version.prerelease != null; + const isValidNightly = _is000(version) && isPrerelease; + if (!isValidNightly) { + throw new Error(`Version ${version.version} is not valid for nightlies`); + } +} + +function _isStableRelease(version) { + return ( + version.major === '0' && version.minor !== '0' && version.prerelease == null + ); +} + +function _isStablePrerelease(version) { + return ( + version.major === '0' && + version.minor !== '0' && + version.prerelease != null && + (version.prerelease.startsWith('rc.') || + version.prerelease.startsWith('rc-')) + ); +} + +function _is000(version) { + return ( + version.major === '0' && version.minor === '0' && version.patch === '0' + ); +} + +function _isMain(version) { + return ( + version.major === '1000' && version.minor === '0' && version.patch === '0' + ); } function isReleaseBranch(branch) { @@ -31,6 +142,7 @@ function isReleaseBranch(branch) { } module.exports = { + validateBuildType, parseVersion, isReleaseBranch, };