Skip to content

Commit

Permalink
Update nightly versioning of react-native (#37028)
Browse files Browse the repository at this point in the history
Summary:
Pull Request resolved: #37028

Changelog: [Internal] - Update nightly versions of react-native

Reviewed By: hoxyq

Differential Revision: D45192008

fbshipit-source-id: d3626c676906f996bc38f8cb156261b7ae202c2a
  • Loading branch information
lunaleaps authored and facebook-github-bot committed May 5, 2023
1 parent 9b69263 commit d24f568
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 76 deletions.
35 changes: 22 additions & 13 deletions scripts/__tests__/publish-npm-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,53 +75,62 @@ describe('publish-npm', () => {

describe('nightly', () => {
it('should publish', () => {
execMock.mockReturnValueOnce({code: 0}).mockReturnValueOnce({code: 0});
execMock
.mockReturnValueOnce({stdout: '0.81.0-rc.1\n', code: 0})
.mockReturnValueOnce({code: 0})
.mockReturnValueOnce({code: 0});
const expectedVersion = '0.82.0-nightly-20230420-currentco';

publishNpm('nightly');

const expectedVersion = '0.0.0-20230420-2352-currentco';
expect(publishAndroidArtifactsToMavenMock).toHaveBeenCalledWith(
expectedVersion,
true,
);
expect(execMock.mock.calls[0][0]).toBe(
`npm view react-native dist-tags.next`,
);
expect(execMock.mock.calls[1][0]).toBe(
`node scripts/set-rn-version.js --to-version ${expectedVersion} --build-type nightly`,
);
expect(execMock.mock.calls[1][0]).toBe('npm publish --tag nightly');
expect(execMock.mock.calls[2][0]).toBe('npm publish --tag nightly');
expect(echoMock).toHaveBeenCalledWith(
`Published to npm ${expectedVersion}`,
);
expect(exitMock).toHaveBeenCalledWith(0);
expect(execMock.mock.calls).toHaveLength(2);
expect(execMock.mock.calls).toHaveLength(3);
});

it('should fail to set version', () => {
execMock.mockReturnValueOnce({code: 1});
execMock
.mockReturnValueOnce({stdout: '0.81.0-rc.1\n', code: 0})
.mockReturnValueOnce({code: 1});
const expectedVersion = '0.82.0-nightly-20230420-currentco';

publishNpm('nightly');

const expectedVersion = '0.0.0-20230420-2352-currentco';
expect(publishAndroidArtifactsToMavenMock).not.toBeCalled();
expect(execMock.mock.calls[0][0]).toBe(
`npm view react-native dist-tags.next`,
);
expect(execMock.mock.calls[1][0]).toBe(
`node scripts/set-rn-version.js --to-version ${expectedVersion} --build-type nightly`,
);
expect(echoMock).toHaveBeenCalledWith(
`Failed to set version number to ${expectedVersion}`,
);
expect(exitMock).toHaveBeenCalledWith(1);
expect(execMock.mock.calls).toHaveLength(1);
expect(execMock.mock.calls).toHaveLength(2);
});
});

describe('release', () => {
it('should fail with invalid release version', () => {
process.env.CIRCLE_TAG = '1.0.1';
publishNpm('release');
expect(echoMock).toHaveBeenCalledWith(
'Version 1.0.1 is not valid for Release',
);
expect(() => {
publishNpm('release');
}).toThrow('Version 1.0.1 is not valid for Release');
expect(publishAndroidArtifactsToMavenMock).not.toBeCalled();
expect(exitMock).toHaveBeenCalledWith(1);
expect(execMock).not.toBeCalled();
});

it('should publish non-latest', () => {
Expand Down
27 changes: 27 additions & 0 deletions scripts/__tests__/version-utils-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const {
parseVersion,
isReleaseBranch,
validateBuildType,
isNightly,
} = require('../version-utils');

let execResult = null;
Expand Down Expand Up @@ -300,6 +301,32 @@ describe('version-utils', () => {
});
});

describe('isNightly', () => {
it('should match old version of nightlies', () => {
expect(
isNightly({
version: '0.0.0-20230420-2108-f84256a92',
major: '0',
minor: '0',
patch: '0',
prerelease: '20230420-2108-f84256a92',
}),
).toBe(true);
});

it('should match nightlies', () => {
expect(
isNightly({
version: '0.81.0-nightly-20230420-f84256a92',
major: '0',
minor: '81',
patch: '0',
prerelease: 'nightly-20230420-f84256a92',
}),
).toBe(true);
});
});

describe('Validate version', () => {
it('Throw error if the buildType is unknown', () => {
function testInvalidFunction() {
Expand Down
116 changes: 60 additions & 56 deletions scripts/publish-npm.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,21 @@

'use strict';

const {exec, echo, exit} = require('shelljs');
const {parseVersion} = require('./version-utils');
const {
exitIfNotOnGit,
getCurrentCommit,
isTaggedLatest,
} = require('./scm-utils');
const {
generateAndroidArtifacts,
publishAndroidArtifactsToMaven,
} = require('./release-utils');
const fs = require('fs');
const path = require('path');
const yargs = require('yargs');

/**
* This script prepares a release version of react-native and may publish to NPM.
* It is supposed to run in CI environment, not on a developer's machine.
Expand All @@ -31,22 +46,45 @@
* * or otherwise `{major}.{minor}-stable`
*/

const {exec, echo, exit} = require('shelljs');
const {parseVersion} = require('./version-utils');
const {
exitIfNotOnGit,
getCurrentCommit,
isTaggedLatest,
} = require('./scm-utils');
const {
generateAndroidArtifacts,
publishAndroidArtifactsToMaven,
} = require('./release-utils');
const fs = require('fs');
const path = require('path');
const yargs = require('yargs');
if (require.main === module) {
const argv = yargs
.option('n', {
alias: 'nightly',
type: 'boolean',
default: false,
})
.option('d', {
alias: 'dry-run',
type: 'boolean',
default: false,
})
.option('r', {
alias: 'release',
type: 'boolean',
default: false,
})
.strict().argv;

const buildType = argv.release
? 'release'
: argv.nightly
? 'nightly'
: 'dry-run';

publishNpm(buildType);
}

const RN_PACKAGE_DIR = path.join(__dirname, '..', 'packages', 'react-native');
// Get `next` version from npm and +1 on the minor for `main` version
function getMainVersion() {
const cmd = 'npm view react-native dist-tags.next';
echo(cmd);
const result = exec(cmd);
if (result.code) {
throw 'Failed to get next version from npm';
}
const {major, minor} = parseVersion(result.stdout.trim(), 'release');
return `${major}.${parseInt(minor, 10) + 1}.0`;
}

function getNpmInfo(buildType) {
const currentCommit = getCurrentCommit();
Expand All @@ -60,13 +98,13 @@ function getNpmInfo(buildType) {
}

if (buildType === 'nightly') {
const mainVersion = getMainVersion();
const dateIdentifier = new Date()
.toISOString()
.slice(0, -8)
.replace(/[-:]/g, '')
.replace(/[T]/g, '-');
.slice(0, -14)
.replace(/[-]/g, '');
return {
version: `0.0.0-${dateIdentifier}-${shortCommit}`,
version: `${mainVersion}-nightly-${dateIdentifier}-${shortCommit}`,
tag: 'nightly',
};
}
Expand Down Expand Up @@ -97,14 +135,7 @@ function getNpmInfo(buildType) {
}

function publishNpm(buildType) {
let version,
tag = null;
try {
({version, tag} = getNpmInfo(buildType));
} catch (e) {
echo(e.message);
return exit(1);
}
const {version, tag} = getNpmInfo(buildType);

// Set version number in various files (package.json, gradle.properties etc)
// For non-nightly, non-dry-run, CircleCI job `prepare_package_for_release` does this
Expand Down Expand Up @@ -138,7 +169,8 @@ function publishNpm(buildType) {
const otp = process.env.NPM_CONFIG_OTP;
const otpFlag = otp ? ` --otp ${otp}` : '';

if (exec(`npm publish ${tagFlag}${otpFlag}`, {cwd: RN_PACKAGE_DIR}).code) {
const packageDirPath = path.join(__dirname, '..', 'packages', 'react-native');
if (exec(`npm publish ${tagFlag}${otpFlag}`, {cwd: packageDirPath}).code) {
echo('Failed to publish package to npm');
return exit(1);
} else {
Expand All @@ -147,32 +179,4 @@ function publishNpm(buildType) {
}
}

if (require.main === module) {
const argv = yargs
.option('n', {
alias: 'nightly',
type: 'boolean',
default: false,
})
.option('d', {
alias: 'dry-run',
type: 'boolean',
default: false,
})
.option('r', {
alias: 'release',
type: 'boolean',
default: false,
})
.strict().argv;

const buildType = argv.release
? 'release'
: argv.nightly
? 'nightly'
: 'dry-run';

publishNpm(buildType);
}

module.exports = publishNpm;
18 changes: 11 additions & 7 deletions scripts/version-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const VERSION_REGEX = /^v?((\d+)\.(\d+)\.(\d+)(?:-(.+))?)$/;
* - stable: 0.68.1
* - stable prerelease: 0.70.0-rc.0
* - e2e-test: X.Y.Z-20221116-2018
* - nightly: 0.0.0-20221116-2018-0bc4547fc
* - nightly: X.Y.Z-20221116-0bc4547fc
* - dryrun: 1000.0.0
*
* Parameters:
Expand Down Expand Up @@ -106,7 +106,7 @@ function validateRelease(version) {
function validateDryRun(version) {
if (
!isMain(version) &&
!isNightlyBuild(version) &&
!isNightly(version) &&
!isStableRelease(version) &&
!isStablePrerelease(version)
) {
Expand All @@ -116,7 +116,7 @@ function validateDryRun(version) {

function validateNightly(version) {
// a valid nightly is a prerelease
if (!isNightlyBuild(version)) {
if (!isNightly(version)) {
throw new Error(`Version ${version.version} is not valid for nightlies`);
}
}
Expand All @@ -139,10 +139,13 @@ function isStablePrerelease(version) {
);
}

function isNightlyBuild(version) {
return (
version.major === '0' && version.minor === '0' && version.patch === '0'
);
function isNightly(version) {
// Check if older nightly version
if (version.major === '0' && version.minor === '0' && version.patch === '0') {
return true;
}

return version.version.includes('nightly');
}

function isMain(version) {
Expand All @@ -158,6 +161,7 @@ function isReleaseBranch(branch) {
module.exports = {
validateBuildType,
parseVersion,
isNightly,
isReleaseBranch,
isMain,
isStableRelease,
Expand Down

0 comments on commit d24f568

Please sign in to comment.