Skip to content

Commit c2db59e

Browse files
ncuilleryMartin Konicek
authored and
Martin Konicek
committed
Fix the parse error when specifying an unknown version.
Summary: **Motivation** When running `react-native-git-upgrade` with an unknown version, the error message isn't very helpful **Test Plan** - Publish the `master` branch to Sinopia - Run `react-native-git-upgrade 0.666.0` inside a RN project - Error message is `SyntaxError: Unexpected end of JSON input` - Publish this branch to Sinopia - Run `react-native-git-upgrade 0.666.0` inside a RN project - Error message should be `Error: The specified version of React Native 0.666.0 doesn't exist. Re-run the react-native-git-upgrade command with an existing version, for example: "react-native-git-upgrade 0.38.0", or without arguments to upgrade to the latest: "react-native-git-upgrade".` Closes #11264 Differential Revision: D4265553 Pulled By: mkonicek fbshipit-source-id: 8597eb09cc3397bfa6d2205a9b3bb30acfad530f
1 parent 6751779 commit c2db59e

File tree

2 files changed

+22
-19
lines changed

2 files changed

+22
-19
lines changed

react-native-git-upgrade/checks.js

-12
Original file line numberDiff line numberDiff line change
@@ -54,21 +54,9 @@ function checkGitAvailable() {
5454
}
5555
}
5656

57-
function checkNewVersionValid(newVersion, requestedVersion) {
58-
if (!semver.valid(newVersion) && requestedVersion) {
59-
throw new Error(
60-
'The specified version of React Native ' + requestedVersion + ' doesn\'t exist.\n' +
61-
'Re-run the react-native-git-upgrade command with an existing version,\n' +
62-
'for example: "react-native-git-upgrade 0.38.0",\n' +
63-
'or without arguments to upgrade to the latest: "react-native-git-upgrade".'
64-
);
65-
}
66-
}
67-
6857
module.exports = {
6958
checkDeclaredVersion,
7059
checkMatchingVersions,
7160
checkReactPeerDependency,
7261
checkGitAvailable,
73-
checkNewVersionValid,
7462
};

react-native-git-upgrade/cliEntry.js

+22-7
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
const fs = require('fs');
1212
const os = require('os');
13+
const assert = require('assert');
1314
const path = require('path');
1415
const shell = require('shelljs');
1516
const Promise = require('promise');
@@ -25,7 +26,6 @@ const {
2526
checkMatchingVersions,
2627
checkReactPeerDependency,
2728
checkGitAvailable,
28-
checkNewVersionValid
2929
} = require('./checks');
3030

3131
log.heading = 'git-upgrade';
@@ -96,6 +96,25 @@ function readPackageFiles(useYarn) {
9696
}
9797
}
9898

99+
function parseInformationJsonOutput(jsonOutput, requestedVersion) {
100+
try {
101+
const output = JSON.parse(jsonOutput);
102+
const newVersion = output.version;
103+
const newReactVersionRange = output['peerDependencies.react'];
104+
105+
assert(semver.valid(newVersion));
106+
107+
return {newVersion, newReactVersionRange}
108+
} catch (err) {
109+
throw new Error(
110+
'The specified version of React Native ' + requestedVersion + ' doesn\'t exist.\n' +
111+
'Re-run the react-native-git-upgrade command with an existing version,\n' +
112+
'for example: "react-native-git-upgrade 0.38.0",\n' +
113+
'or without arguments to upgrade to the latest: "react-native-git-upgrade".'
114+
);
115+
}
116+
}
117+
99118

100119
function setupWorkingDir(tmpDir) {
101120
return new Promise((resolve, reject) => {
@@ -240,15 +259,11 @@ async function run(requestedVersion, cliArgs) {
240259

241260
log.info('Get information from NPM registry');
242261
const viewCommand = 'npm view react-native@' + (requestedVersion || 'latest') + ' peerDependencies.react version --json';
243-
const viewOutput = await exec(viewCommand, verbose).then(JSON.parse);
244-
const newVersion = viewOutput.version;
245-
const newReactVersionRange = viewOutput['peerDependencies.react'];
262+
const jsonOutput = await exec(viewCommand, verbose);
263+
const {newVersion, newReactVersionRange} = parseInformationJsonOutput(jsonOutput, requestedVersion);
246264
// Print which versions we're upgrading to
247265
log.info('Upgrading to React Native ' + newVersion + (newReactVersionRange ? ', React ' + newReactVersionRange : ''));
248266

249-
log.info('Check new version');
250-
checkNewVersionValid(newVersion, requestedVersion);
251-
252267
log.info('Setup temporary working directory');
253268
await setupWorkingDir(tmpDir);
254269

0 commit comments

Comments
 (0)