Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 62 additions & 0 deletions scripts/__tests__/npm-utils-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/**
* Copyright (c) Meta Platforms, Inc. and 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 {getPackageVersionStrByTag, publishPackage} = require('../npm-utils');

const execMock = jest.fn();
jest.mock('shelljs', () => ({
exec: execMock,
}));

describe('npm-utils', () => {
beforeEach(() => {
jest.resetModules();
jest.resetAllMocks();
});

describe('getPackageVersionStrByTag', () => {
it('should return package version string', () => {
execMock.mockImplementationOnce(() => ({code: 0, stdout: '0.34.2 \n'}));
const versionStr = getPackageVersionStrByTag('my-package', 'next');
expect(versionStr).toBe('0.34.2');
});
it('should throw error when invalid result', () => {
execMock.mockImplementationOnce(() => ({
code: 1,
stderr: 'Some error message',
}));

expect(() => {
getPackageVersionStrByTag('my-package', 'next');
}).toThrow('Failed to get next version from npm\nSome error message');
});
});

describe('publishPackage', () => {
it('should run publish command', () => {
publishPackage(
'path/to/my-package',
{tag: 'latest', otp: 'otp'},
{silent: true, cwd: 'i/expect/this/to/be/overriden'},
);
expect(execMock).toHaveBeenCalledWith(
'npm publish --tag latest --otp otp',
{silent: true, cwd: 'path/to/my-package'},
);
});

it('should run publish command when no execOptions', () => {
publishPackage('path/to/my-package', {tag: 'latest', otp: 'otp'});
expect(execMock).toHaveBeenCalledWith(
'npm publish --tag latest --otp otp',
{cwd: 'path/to/my-package'},
);
});
});
});
4 changes: 2 additions & 2 deletions scripts/__tests__/publish-npm-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ describe('publish-npm', () => {
true,
);
expect(execMock.mock.calls[0][0]).toBe(
`npm view react-native dist-tags.next`,
`npm view react-native@next version`,
);
expect(execMock.mock.calls[1][0]).toBe(
`node scripts/set-rn-version.js --to-version ${expectedVersion} --build-type nightly`,
Expand All @@ -111,7 +111,7 @@ describe('publish-npm', () => {

expect(publishAndroidArtifactsToMavenMock).not.toBeCalled();
expect(execMock.mock.calls[0][0]).toBe(
`npm view react-native dist-tags.next`,
`npm view react-native@next version`,
);
expect(execMock.mock.calls[1][0]).toBe(
`node scripts/set-rn-version.js --to-version ${expectedVersion} --build-type nightly`,
Expand Down
15 changes: 4 additions & 11 deletions scripts/monorepo/find-and-publish-all-bumped-packages.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,19 +101,12 @@ const findAndPublishAllBumpedPackages = () => {
);
}

const npmOTPFlag = NPM_CONFIG_OTP ? `--otp ${NPM_CONFIG_OTP}` : '';

const {status, stderr} = spawnSync('npm', ['publish', `${npmOTPFlag}`], {
cwd: packageAbsolutePath,
shell: true,
stdio: 'pipe',
encoding: 'utf-8',
});
if (status !== 0) {
const result = publishPackage(packageAbsolutePath, {otp: NPM_CONFIG_OTP});
if (result.code !== 0) {
console.log(
`\u274c Failed to publish version ${nextVersion} of ${packageManifest.name}. npm publish exited with code ${status}:`,
`\u274c Failed to publish version ${nextVersion} of ${packageManifest.name}. npm publish exited with code ${result.code}:`,
);
console.log(stderr);
console.log(result.stderr);

process.exit(1);
} else {
Expand Down
37 changes: 37 additions & 0 deletions scripts/npm-utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @format
*/

'use strict';

const {exec} = require('shelljs');

function getPackageVersionStrByTag(packageName, tag) {
const result = exec(`npm view ${packageName}@${tag} version`);

if (result.code) {
throw `Failed to get ${tag} version from npm\n${result.stderr}`;
}
return result.stdout.trim();
}

function publishPackage(packagePath, packageOptions, execOptions) {
const {tag, otp} = packageOptions;
const tagFlag = tag ? ` --tag ${tag}` : '';
const otpFlag = otp ? ` --otp ${otp}` : '';
const options = execOptions
? {...execOptions, cwd: packagePath}
: {cwd: packagePath};

return exec(`npm publish${tagFlag}${otpFlag}`, options);
}

module.exports = {
getPackageVersionStrByTag,
publishPackage,
};
22 changes: 9 additions & 13 deletions scripts/publish-npm.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

const {exec, echo, exit} = require('shelljs');
const {parseVersion} = require('./version-utils');
const {getPackageVersionStrByTag, publishPackage} = require('./npm-utils');
const {
exitIfNotOnGit,
getCurrentCommit,
Expand Down Expand Up @@ -76,13 +77,8 @@ if (require.main === module) {

// 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');
const versionStr = getPackageVersionStrByTag('react-native', 'next');
const {major, minor} = parseVersion(versionStr, 'release');
return `${major}.${parseInt(minor, 10) + 1}.0`;
}

Expand Down Expand Up @@ -165,12 +161,12 @@ function publishNpm(buildType) {
// NPM publishing is done just after.
publishAndroidArtifactsToMaven(version, buildType === 'nightly');

const tagFlag = `--tag ${tag}`;
const otp = process.env.NPM_CONFIG_OTP;
const otpFlag = otp ? ` --otp ${otp}` : '';

const packageDirPath = path.join(__dirname, '..', 'packages', 'react-native');
if (exec(`npm publish ${tagFlag}${otpFlag}`, {cwd: packageDirPath}).code) {
const packagePath = path.join(__dirname, '..', 'packages', 'react-native');
const result = publishPackage(packagePath, {
tag,
otp: process.env.NPM_CONFIG_OTP,
});
if (result.code) {
echo('Failed to publish package to npm');
return exit(1);
} else {
Expand Down