Skip to content

Commit 1928648

Browse files
facebook-github-botpull[bot]
authored andcommitted
Re-sync with internal repository (#37831)
Co-authored-by: Facebook Community Bot <[email protected]>
1 parent 3cd70ac commit 1928648

11 files changed

+238
-115
lines changed

.circleci/config.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -795,7 +795,7 @@ jobs:
795795
name: Create Android template project
796796
command: |
797797
REPO_ROOT=$(pwd)
798-
node ./scripts/set-rn-template-version.js "file:$REPO_ROOT/build/$(cat build/react-native-package-version)"
798+
node ./scripts/update-template-package.js "{\"react-native\":\"file:$REPO_ROOT/build/$(cat build/react-native-package-version)\"}"
799799
node ./scripts/template/initialize.js --reactNativeRootPath $REPO_ROOT --templateName $PROJECT_NAME --templateConfigPath "$REPO_ROOT/packages/react-native" --directory "/tmp/$PROJECT_NAME"
800800
- run:
801801
name: Build the template application for << parameters.flavor >> with Architecture set to << parameters.architecture >>, and using the << parameters.jsengine>> JS engine.
@@ -879,7 +879,7 @@ jobs:
879879
REPO_ROOT=$(pwd)
880880
PACKAGE=$(cat build/react-native-package-version)
881881
PATH_TO_PACKAGE="$REPO_ROOT/build/$PACKAGE"
882-
node ./scripts/set-rn-template-version.js "file:$PATH_TO_PACKAGE"
882+
node ./scripts/update-template-package.js "{\"react-native\":\"file:$PATH_TO_PACKAGE\"}"
883883
node ./scripts/template/initialize.js --reactNativeRootPath $REPO_ROOT --templateName $PROJECT_NAME --templateConfigPath "$REPO_ROOT/packages/react-native" --directory "/tmp/$PROJECT_NAME"
884884
- run:
885885
name: Install iOS dependencies - Configuration << parameters.flavor >>; New Architecture << parameters.architecture >>; JS Engine << parameters.jsengine>>; Flipper << parameters.flipper >>

scripts/__tests__/npm-utils-test.js

+46-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,11 @@
77
* @format
88
*/
99

10-
const {getPackageVersionStrByTag, publishPackage} = require('../npm-utils');
10+
const {
11+
applyPackageVersions,
12+
getPackageVersionStrByTag,
13+
publishPackage,
14+
} = require('../npm-utils');
1115

1216
const execMock = jest.fn();
1317
jest.mock('shelljs', () => ({
@@ -20,6 +24,47 @@ describe('npm-utils', () => {
2024
jest.resetAllMocks();
2125
});
2226

27+
describe('applyPackageVersions', () => {
28+
it('should replace package.json with dependencies', () => {
29+
const originalPackageJson = {
30+
name: 'my-package',
31+
dependencies: {
32+
'my-dependency-a': 'nightly',
33+
'my-dependency-b': '^1.2.3',
34+
},
35+
devDependencies: {
36+
'my-dev-dependency-a': 'nightly',
37+
'my-dev-dependency-b': '^1.2.3',
38+
},
39+
someOtherField: {
40+
'my-dependency-a': 'should-be-untouched',
41+
},
42+
};
43+
44+
const dependencies = {
45+
'my-dependency-a': '0.72.0-nightly-shortcommit',
46+
'my-dev-dependency-a': 'updated-version',
47+
'my-non-existant-dep': 'some-version',
48+
};
49+
50+
const package = applyPackageVersions(originalPackageJson, dependencies);
51+
expect(package).toEqual({
52+
name: 'my-package',
53+
dependencies: {
54+
'my-dependency-a': '0.72.0-nightly-shortcommit',
55+
'my-dependency-b': '^1.2.3',
56+
},
57+
devDependencies: {
58+
'my-dev-dependency-a': 'updated-version',
59+
'my-dev-dependency-b': '^1.2.3',
60+
},
61+
someOtherField: {
62+
'my-dependency-a': 'should-be-untouched',
63+
},
64+
});
65+
});
66+
});
67+
2368
describe('getPackageVersionStrByTag', () => {
2469
it('should return package version string', () => {
2570
execMock.mockImplementationOnce(() => ({code: 0, stdout: '0.34.2 \n'}));

scripts/__tests__/publish-npm-test.js

+20-19
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@
1010
const execMock = jest.fn();
1111
const echoMock = jest.fn();
1212
const exitMock = jest.fn();
13+
const consoleErrorMock = jest.fn();
1314
const isTaggedLatestMock = jest.fn();
15+
const setReactNativeVersionMock = jest.fn();
1416
const publishAndroidArtifactsToMavenMock = jest.fn();
1517
const env = process.env;
1618

@@ -33,11 +35,13 @@ jest
3335
generateAndroidArtifacts: jest.fn(),
3436
publishAndroidArtifactsToMaven: publishAndroidArtifactsToMavenMock,
3537
}))
38+
.mock('./../set-rn-version', () => setReactNativeVersionMock)
3639
.mock('../monorepo/get-and-update-nightlies');
3740

3841
const date = new Date('2023-04-20T23:52:39.543Z');
3942

4043
const publishNpm = require('../publish-npm');
44+
let consoleError;
4145

4246
describe('publish-npm', () => {
4347
beforeAll(() => {
@@ -47,8 +51,14 @@ describe('publish-npm', () => {
4751
afterAll(() => {
4852
jest.useRealTimers();
4953
});
54+
beforeEach(() => {
55+
consoleError = console.error;
56+
console.error = consoleErrorMock;
57+
});
58+
5059
afterEach(() => {
5160
process.env = env;
61+
console.error = consoleError;
5262
});
5363

5464
afterEach(() => {
@@ -58,27 +68,25 @@ describe('publish-npm', () => {
5868

5969
describe('dry-run', () => {
6070
it('should set version and not publish', () => {
61-
execMock.mockReturnValueOnce({code: 0});
62-
6371
publishNpm('dry-run');
6472

6573
expect(exitMock).toHaveBeenCalledWith(0);
6674
expect(isTaggedLatestMock.mock.calls).toHaveLength(0);
6775
expect(echoMock).toHaveBeenCalledWith(
6876
'Skipping `npm publish` because --dry-run is set.',
6977
);
70-
expect(execMock).toHaveBeenCalledWith(
71-
'node scripts/set-rn-version.js --to-version 1000.0.0-currentco --build-type dry-run',
78+
expect(setReactNativeVersionMock).toBeCalledWith(
79+
'1000.0.0-currentco',
80+
null,
81+
'dry-run',
7282
);
73-
expect(execMock.mock.calls).toHaveLength(1);
7483
});
7584
});
7685

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

@@ -91,37 +99,30 @@ describe('publish-npm', () => {
9199
expect(execMock.mock.calls[0][0]).toBe(
92100
`npm view react-native@next version`,
93101
);
94-
expect(execMock.mock.calls[1][0]).toBe(
95-
`node scripts/set-rn-version.js --to-version ${expectedVersion} --build-type nightly`,
96-
);
97-
expect(execMock.mock.calls[2][0]).toBe('npm publish --tag nightly');
102+
expect(execMock.mock.calls[1][0]).toBe('npm publish --tag nightly');
98103
expect(echoMock).toHaveBeenCalledWith(
99104
`Published to npm ${expectedVersion}`,
100105
);
101106
expect(exitMock).toHaveBeenCalledWith(0);
102-
expect(execMock.mock.calls).toHaveLength(3);
103107
});
104108

105109
it('should fail to set version', () => {
106-
execMock
107-
.mockReturnValueOnce({stdout: '0.81.0-rc.1\n', code: 0})
108-
.mockReturnValueOnce({code: 1});
110+
execMock.mockReturnValueOnce({stdout: '0.81.0-rc.1\n', code: 0});
109111
const expectedVersion = '0.82.0-nightly-20230420-currentco';
112+
setReactNativeVersionMock.mockImplementation(() => {
113+
throw new Error('something went wrong');
114+
});
110115

111116
publishNpm('nightly');
112117

113118
expect(publishAndroidArtifactsToMavenMock).not.toBeCalled();
114119
expect(execMock.mock.calls[0][0]).toBe(
115120
`npm view react-native@next version`,
116121
);
117-
expect(execMock.mock.calls[1][0]).toBe(
118-
`node scripts/set-rn-version.js --to-version ${expectedVersion} --build-type nightly`,
119-
);
120-
expect(echoMock).toHaveBeenCalledWith(
122+
expect(consoleErrorMock).toHaveBeenCalledWith(
121123
`Failed to set version number to ${expectedVersion}`,
122124
);
123125
expect(exitMock).toHaveBeenCalledWith(1);
124-
expect(execMock.mock.calls).toHaveLength(2);
125126
});
126127
});
127128

scripts/__tests__/set-rn-version-test.js

+28-28
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,19 @@
99

1010
const execMock = jest.fn();
1111
const echoMock = jest.fn();
12-
const exitMock = jest.fn();
1312
const catMock = jest.fn();
1413
const sedMock = jest.fn();
1514
const writeFileSyncMock = jest.fn();
15+
const updateTemplatePackageMock = jest.fn();
1616

1717
jest
1818
.mock('shelljs', () => ({
1919
exec: execMock,
2020
echo: echoMock,
21-
exit: exitMock,
2221
cat: catMock,
2322
sed: sedMock,
2423
}))
24+
.mock('./../update-template-package', () => updateTemplatePackageMock)
2525
.mock('./../scm-utils', () => ({
2626
saveFiles: jest.fn(),
2727
}))
@@ -45,7 +45,7 @@ describe('set-rn-version', () => {
4545
it('should set nightly version', () => {
4646
catMock.mockImplementation(path => {
4747
if (path === 'packages/react-native/package.json') {
48-
return '{"name": "myPackage", "version": 2}';
48+
return '{"name": "myPackage", "version": 2, "dependencies": {"@react-native/package-a": "nightly", "@react-native/package-b": "^0.73.0"}}';
4949
} else if (
5050
path === 'scripts/versiontemplates/ReactNativeVersion.java.template' ||
5151
path === 'scripts/versiontemplates/RCTVersion.m.template' ||
@@ -58,13 +58,14 @@ describe('set-rn-version', () => {
5858
}
5959
});
6060

61-
execMock
62-
.mockReturnValueOnce({code: 0})
63-
.mockReturnValueOnce({stdout: 'line1\nline2\nline3\n'});
61+
execMock.mockReturnValueOnce({stdout: 'line1\nline2\nline3\n'});
6462
sedMock.mockReturnValueOnce({code: 0});
6563

6664
const version = '0.81.0-nightly-29282302-abcd1234';
67-
setReactNativeVersion(version, 'nightly');
65+
const nightlyVersions = {
66+
'@react-native/package-a': version,
67+
};
68+
setReactNativeVersion(version, nightlyVersions, 'nightly');
6869

6970
expect(sedMock).toHaveBeenCalledWith(
7071
'-i',
@@ -91,14 +92,19 @@ describe('set-rn-version', () => {
9192
expect(writeFileSyncMock.mock.calls[4][0]).toBe(
9293
'packages/react-native/package.json',
9394
);
94-
expect(writeFileSyncMock.mock.calls[4][1]).toBe(
95-
`{\n "name": "myPackage",\n "version": "${version}"\n}`,
96-
);
97-
98-
expect(exitMock.mock.calls[0][0]).toBe(0);
99-
expect(execMock.mock.calls[0][0]).toBe(
100-
`node scripts/set-rn-template-version.js ${version}`,
101-
);
95+
expect(writeFileSyncMock.mock.calls[4][1]).toBe(`{
96+
"name": "myPackage",
97+
"version": "${version}",
98+
"dependencies": {
99+
"@react-native/package-a": "0.81.0-nightly-29282302-abcd1234",
100+
"@react-native/package-b": "^0.73.0"
101+
}
102+
}`);
103+
104+
expect(updateTemplatePackageMock).toHaveBeenCalledWith({
105+
'@react-native/package-a': '0.81.0-nightly-29282302-abcd1234',
106+
'react-native': version,
107+
});
102108
});
103109

104110
it('should set release version', () => {
@@ -109,13 +115,11 @@ describe('set-rn-version', () => {
109115
return 'exports.version = {major: ${major}, minor: ${minor}, patch: ${patch}, prerelease: ${prerelease}}';
110116
});
111117

112-
execMock
113-
.mockReturnValueOnce({code: 0})
114-
.mockReturnValueOnce({stdout: 'line1\nline2\nline3\n'});
118+
execMock.mockReturnValueOnce({stdout: 'line1\nline2\nline3\n'});
115119
sedMock.mockReturnValueOnce({code: 0});
116120

117121
const version = '0.81.0';
118-
setReactNativeVersion(version, 'release');
122+
setReactNativeVersion(version, null, 'release');
119123

120124
expect(sedMock).toHaveBeenCalledWith(
121125
'-i',
@@ -137,21 +141,18 @@ describe('set-rn-version', () => {
137141
`{\n "name": "myPackage",\n "version": "${version}"\n}`,
138142
);
139143

140-
expect(exitMock.mock.calls[0][0]).toBe(0);
144+
expect(updateTemplatePackageMock).toHaveBeenCalledWith({
145+
'react-native': version,
146+
});
141147
expect(execMock.mock.calls[0][0]).toBe(
142-
`node scripts/set-rn-template-version.js ${version}`,
143-
);
144-
expect(execMock.mock.calls[1][0]).toBe(
145148
`diff -r ./rn-set-version/ . | grep '^[>]' | grep -c ${version} `,
146149
);
147150
});
148151

149152
it('should fail validation', () => {
150153
catMock.mockReturnValue('{}');
151154

152-
execMock
153-
.mockReturnValueOnce({code: 0})
154-
.mockReturnValueOnce({stdout: 'line1\nline2\n'});
155+
execMock.mockReturnValueOnce({stdout: 'line1\nline2\n'});
155156
sedMock.mockReturnValueOnce({code: 0});
156157
const filesToValidate = [
157158
'packages/react-native/package.json',
@@ -160,9 +161,8 @@ describe('set-rn-version', () => {
160161
];
161162

162163
const version = '0.81.0';
163-
setReactNativeVersion(version, 'release');
164+
setReactNativeVersion(version, null, 'release');
164165

165-
expect(exitMock).toHaveBeenCalledWith(0);
166166
expect(echoMock).toHaveBeenNthCalledWith(
167167
1,
168168
'The tmp versioning folder is ./rn-set-version/',

scripts/npm-utils.js

+28
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,35 @@ function pack(packagePath) {
5656
}
5757
}
5858

59+
/**
60+
* `package` is an object form of package.json
61+
* `dependencies` is a map of dependency to version string
62+
*
63+
* This replaces both dependencies and devDependencies in package.json
64+
*/
65+
function applyPackageVersions(originalPackageJson, packageVersions) {
66+
const packageJson = {...originalPackageJson};
67+
68+
for (const name of Object.keys(packageVersions)) {
69+
if (
70+
packageJson.dependencies != null &&
71+
packageJson.dependencies[name] != null
72+
) {
73+
packageJson.dependencies[name] = packageVersions[name];
74+
}
75+
76+
if (
77+
packageJson.devDependencies != null &&
78+
packageJson.devDependencies[name] != null
79+
) {
80+
packageJson.devDependencies[name] = packageVersions[name];
81+
}
82+
}
83+
return packageJson;
84+
}
85+
5986
module.exports = {
87+
applyPackageVersions,
6088
getPackageVersionStrByTag,
6189
publishPackage,
6290
diffPackages,

0 commit comments

Comments
 (0)