Skip to content

Commit

Permalink
Fix nightly publish
Browse files Browse the repository at this point in the history
Summary:
Changelog: [Internal] - `get-and-update-packages` was deleted in D53487874 also actually published the monorepo packages. 

Update publish-npm to publish the updated nightly monorepo packages

Reviewed By: cipolleschi

Differential Revision: D53697621
  • Loading branch information
lunaleaps authored and facebook-github-bot committed Feb 13, 2024
1 parent 1e49f93 commit 1a5af20
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 10 deletions.
85 changes: 75 additions & 10 deletions scripts/releases-ci/__tests__/publish-npm-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ const publishAndroidArtifactsToMavenMock = jest.fn();
const removeNewArchFlags = jest.fn();
const env = process.env;

const publishPackageMock = jest.fn();
const getNpmInfoMock = jest.fn();

jest
.mock('shelljs', () => ({
exec: execMock,
Expand Down Expand Up @@ -96,10 +99,34 @@ describe('publish-npm', () => {
});

describe('nightly', () => {
let consoleLog;
beforeAll(() => {
consoleLog = console.log;
console.log = jest.fn();
jest.mock('../../npm-utils', () => ({
...jest.requireActual('../../npm-utils'),
publishPackage: publishPackageMock,
getNpmInfo: getNpmInfoMock,
}));
});

afterAll(() => {
console.log = consoleLog;
jest.unmock('../../npm-utils');
});

beforeEach(() => {
jest.resetAllMocks();
});

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

await publishNpm('nightly');
Expand All @@ -110,19 +137,31 @@ describe('publish-npm', () => {
expectedVersion,
'nightly',
);
expect(execMock.mock.calls[0][0]).toBe(
`npm view react-native@next version`,
publishPackageMock.mock.calls.forEach(params => {
expect(params[1]).toEqual({
tags: ['nightly'],
otp: undefined,
});
});
expect(publishPackageMock).toHaveBeenCalledWith(
path.join(REPO_ROOT, 'packages/react-native'),
{otp: undefined, tags: ['nightly']},
);
expect(execMock.mock.calls[1][0]).toBe('npm publish --tag nightly');
expect(echoMock).toHaveBeenCalledWith(
`Published to npm ${expectedVersion}`,
);
expect(exitMock).toHaveBeenCalledWith(0);
});

it('should fail to set version', async () => {
execMock.mockReturnValueOnce({stdout: '0.81.0-rc.1\n', code: 0});
const expectedVersion = '0.82.0-nightly-20230420-currentco';
publishPackageMock.mockImplementation(() => ({
code: 0,
}));
getNpmInfoMock.mockImplementation(() => ({
version: expectedVersion,
tag: 'nightly',
}));
setVersionMock.mockImplementation(() => {
throw new Error('something went wrong');
});
Expand All @@ -131,14 +170,40 @@ describe('publish-npm', () => {

expect(removeNewArchFlags).not.toHaveBeenCalled();
expect(publishAndroidArtifactsToMavenMock).not.toBeCalled();
expect(execMock.mock.calls[0][0]).toBe(
`npm view react-native@next version`,
);
expect(consoleErrorMock).toHaveBeenCalledWith(
`Failed to set version number to ${expectedVersion}`,
);
expect(exitMock).toHaveBeenCalledWith(1);
});
it('should fail to publish react-native if some monorepo packages fail', async () => {
publishPackageMock.mockImplementation(packagePath => ({
code: 1,
}));

getNpmInfoMock.mockImplementation(() => ({
version: expectedVersion,
tag: 'nightly',
}));

const expectedVersion = '0.82.0-nightly-20230420-currentco';

await publishNpm('nightly');

expect(removeNewArchFlags).not.toHaveBeenCalled();
expect(setVersionMock).toBeCalledWith(expectedVersion);
expect(publishAndroidArtifactsToMavenMock).toHaveBeenCalledWith(
expectedVersion,
'nightly',
);
expect(exitMock).toHaveBeenCalledWith(1);
publishPackageMock.mock.calls.forEach(params => {
expect(params[1]).toEqual({
tags: ['nightly'],
otp: undefined,
});
});
expect(echoMock).toHaveBeenCalledWith('Failed to publish package to npm');
});
});

describe('release', () => {
Expand Down
28 changes: 28 additions & 0 deletions scripts/releases-ci/publish-npm.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const {getNpmInfo, publishPackage} = require('../npm-utils');
const {removeNewArchFlags} = require('../releases/remove-new-arch-flags');
const {setReactNativeVersion} = require('../releases/set-rn-version');
const setVersion = require('../releases/set-version');
const {getPackages} = require('../releases/utils/monorepo');
const {
generateAndroidArtifacts,
publishAndroidArtifactsToMaven,
Expand Down Expand Up @@ -67,6 +68,32 @@ async function main() {
await publishNpm(buildType);
}

async function publishMonorepoPackages(tag /*: ?string */) {
const projectInfo = await getPackages({
includePrivate: false,
includeReactNative: false,
});

for (const packageInfo of Object.values(projectInfo)) {
console.log(`Publishing ${packageInfo.name}...`);
const result = publishPackage(packageInfo.path, {
// $FlowFixMe[incompatible-call]
tags: [tag],
otp: process.env.NPM_CONFIG_OTP,
});

const spec = `${packageInfo.name}@${packageInfo.packageJson.version}`;

if (result.code) {
echo(`Failed to publish ${spec} to npm. Stopping all nightly publishes`);
exit(1);
} else {
echo(`Published ${spec} to npm`);
exit(0);
}
}
}

async function publishNpm(buildType /*: BuildType */) /*: Promise<void> */ {
const {version, tag} = getNpmInfo(buildType);

Expand All @@ -80,6 +107,7 @@ async function publishNpm(buildType /*: BuildType */) /*: Promise<void> */ {
if (buildType === 'nightly') {
// Set same version for all monorepo packages
await setVersion(version);
await publishMonorepoPackages(tag);
} else {
await setReactNativeVersion(version, null, buildType);
}
Expand Down

0 comments on commit 1a5af20

Please sign in to comment.