Skip to content

Commit a0f3a93

Browse files
ncuilleryFacebook Github Bot
authored and
Facebook Github Bot
committed
Upgrade React and React Native at the same time
Summary: **Motivation** As NoelBroda pointed out in #11104 and #11123, NPM often produces an unmet peerDependency when upgrading React Native. It causes a failure when running "npm install" with NPM2. During the git-upgrade, we have to take care of the `react` peerDep: this PR checks if the installed `react` package matches the `react` peerDep of the new `react-native` version. If so, R & RN are upgraded at the same time (in the same `npm install`). **Test plan** - Publish `react-native-git-upgrade` to Sinopia, - Run `react-native-git-upgrade` inside a RN project with version < 0.37, - Verify that no "unmet peer dependency" warning is displayed - Open the `package.json` and verify that both R & RN have been updated Closes #11226 Differential Revision: D4258007 Pulled By: mkonicek fbshipit-source-id: cff466d4710807d97fc6161f47bb974097b75261
1 parent c5da106 commit a0f3a93

File tree

1 file changed

+29
-14
lines changed

1 file changed

+29
-14
lines changed

react-native-git-upgrade/cliEntry.js

+29-14
Original file line numberDiff line numberDiff line change
@@ -68,27 +68,35 @@ stdout: ${stdout}`));
6868
+ * - Parsed package.json
6969
+ */
7070
function readPackageFiles() {
71-
const nodeModulesPakPath = path.resolve(
71+
const reactNativeNodeModulesPakPath = path.resolve(
7272
process.cwd(),
7373
'node_modules',
7474
'react-native',
7575
'package.json'
7676
);
7777

78+
const reactNodeModulesPakPath = path.resolve(
79+
process.cwd(),
80+
'node_modules',
81+
'react',
82+
'package.json'
83+
);
84+
7885
const pakPath = path.resolve(
7986
process.cwd(),
8087
'package.json'
8188
);
8289

8390
try {
84-
const nodeModulesPak = JSON.parse(fs.readFileSync(nodeModulesPakPath, 'utf8'));
91+
const reactNativeNodeModulesPak = JSON.parse(fs.readFileSync(reactNativeNodeModulesPakPath, 'utf8'));
92+
const reactNodeModulesPak = JSON.parse(fs.readFileSync(reactNodeModulesPakPath, 'utf8'));
8593
const pak = JSON.parse(fs.readFileSync(pakPath, 'utf8'));
8694

87-
return {nodeModulesPak, pak};
95+
return {reactNativeNodeModulesPak, reactNodeModulesPak, pak};
8896
} catch (err) {
8997
throw new Error(
90-
'Unable to find "' + pakPath + '" or "' + nodeModulesPakPath + '". ' +
91-
'Make sure you ran "npm install" and that you are inside a React Native project.'
98+
'Unable to find one of "' + pakPath + '", "' + rnPakPath + '" or "' + reactPakPath + '". ' +
99+
'Make sure you ran "npm install" and that you are inside a React Native project.'
92100
)
93101
}
94102
}
@@ -191,16 +199,16 @@ async function checkForUpdates() {
191199
async function run(requestedVersion, cliArgs) {
192200
const tmpDir = path.resolve(os.tmpdir(), 'react-native-git-upgrade');
193201
const generatorDir = path.resolve(process.cwd(), 'node_modules', 'react-native', 'local-cli', 'generator');
202+
let projectBackupCreated = false;
194203

195204
try {
196-
let projectBackupCreated = false;
197-
198205
await checkForUpdates();
199206

200207
log.info('Read package.json files');
201-
const {nodeModulesPak, pak} = readPackageFiles();
208+
const {reactNativeNodeModulesPak, reactNodeModulesPak, pak} = readPackageFiles();
202209
const appName = pak.name;
203-
const currentVersion = nodeModulesPak.version;
210+
const currentVersion = reactNativeNodeModulesPak.version;
211+
const currentReactVersion = reactNodeModulesPak.version;
204212
const declaredVersion = pak.dependencies['react-native'];
205213
const declaredReactVersion = pak.dependencies.react;
206214

@@ -218,10 +226,11 @@ async function run(requestedVersion, cliArgs) {
218226
log.info('Check that Git is installed');
219227
checkGitAvailable();
220228

221-
log.info('Get react-native version from NPM registry');
222-
const versionOutput = await exec('npm view react-native@' + (requestedVersion || 'latest') + ' version', verbose);
223-
const newVersion = semver.clean(versionOutput);
224-
log.info('Upgrading to React Native ' + newVersion);
229+
log.info('Get information from NPM registry');
230+
const viewCommand = 'npm view react-native@' + (requestedVersion || 'latest') + ' peerDependencies.react version --json';
231+
const viewOutput = await exec(viewCommand, verbose).then(JSON.parse);
232+
const newVersion = viewOutput.version;
233+
const newReactVersionRange = viewOutput['peerDependencies.react'];
225234

226235
log.info('Check new version');
227236
checkNewVersionValid(newVersion, requestedVersion);
@@ -255,7 +264,13 @@ async function run(requestedVersion, cliArgs) {
255264
await exec('git commit -m "Old version" --allow-empty', verbose);
256265

257266
log.info('Install the new version');
258-
await exec('npm install --save react-native@' + newVersion + ' --color=always', verbose);
267+
let installCommand = 'npm install --save --color=always';
268+
installCommand += ' react-native@' + newVersion;
269+
if (!semver.satisfies(currentReactVersion, newReactVersionRange)) {
270+
// Install React as well to avoid unmet peer dependency
271+
installCommand += ' react@' + newReactVersionRange;
272+
}
273+
await exec(installCommand, verbose);
259274

260275
log.info('Generate new version template');
261276
await generateTemplates(generatorDir, appName, verbose);

0 commit comments

Comments
 (0)