Skip to content

Commit

Permalink
More accurate check for latest version
Browse files Browse the repository at this point in the history
Summary:
When testing `react-native-git-upgrade` locally I noticed a warning:

    A more recent version of "react-native-git-upgrade" has been found:
    Current: 0.1.0
    Latest: 0.0.1

See https://www.npmjs.com/package/react-native-git-upgrade

This won't happen to a lot of people but better fix the warning. Also, if the check for updates fails, don't crash - the check for updates is not critical to the tool working.

Also, slightly updated one error message.

**Test plan (required)**

Installed `react-native-git-upgrade` locally, ran it inside an app folder (RN 0.29).

Didn't see the wrong "more recent version" warning anymore.

Tried making `checkForUpdates` fail by adding some dummy code: `semver.foo()`. Saw a warning but the process continued:

    git-upgrade WARN Check for latest version failed semver.foo is not a function

Saw a more descriptive error message:

    git-upgrade ERR! Error: react-native version in "package.json" (0.29.0) doesn't match the installed version in "node_mod
Closes facebook#11188

Differential Revision: D4244002

Pulled By: bestander

fbshipit-source-id: 772044750a933663cb516201d09e2873462cca4a
  • Loading branch information
Martin Konicek authored and Facebook Github Bot committed Nov 29, 2016
1 parent f1a5233 commit b20b206
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 11 deletions.
4 changes: 2 additions & 2 deletions react-native-git-upgrade/checks.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ function checkDeclaredVersion(declaredVersion) {
function checkMatchingVersions(currentVersion, declaredVersion) {
if (!semver.satisfies(currentVersion, declaredVersion)) {
throw new Error(
'react-native version in "package.json" doesn\'t match ' +
'the installed version in "node_modules".\n' +
'react-native version in "package.json" (' + declaredVersion + ') doesn\'t match ' +
'the installed version in "node_modules" (' + currentVersion + ').\n' +
'Try running "npm install" to fix this.'
);
}
Expand Down
29 changes: 20 additions & 9 deletions react-native-git-upgrade/cliEntry.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,27 +156,38 @@ function runYeomanGenerators(generatorDir, appName, verbose) {
return new Promise((resolve) => env.run(generatorArgs, {upgrade: true, force: true}, resolve));
}

async function run(requiredVersion, cliArgs) {
const context = {
tmpDir: path.resolve(os.tmpdir(), 'react-native-git-upgrade'),
generatorDir: path.resolve(process.cwd(), 'node_modules', 'react-native', 'local-cli', 'generator'),
requiredVersion,
cliArgs,
};

/**
* If there's a newer version of react-native-git-upgrade in npm, suggest to the user to upgrade.
*/
async function checkForUpdates() {
try {
log.info('Check for react-native-git-upgrade updates');
const lastGitUpgradeVersion = await exec('npm view react-native-git-upgrade@latest version');
const current = require('./package').version;
const latest = semver.clean(lastGitUpgradeVersion);
if (current !== latest) {
if (semver.gt(latest, current)) {
log.warn(
'A more recent version of "react-native-git-upgrade" has been found.\n' +
`Current: ${current}\n` +
`Latest: ${latest}\n` +
'Please run "npm install -g react-native-git-upgrade"'
);
}
} catch (err) {
log.warn('Check for latest version failed', err.message);
}
}

async function run(requiredVersion, cliArgs) {
const context = {
tmpDir: path.resolve(os.tmpdir(), 'react-native-git-upgrade'),
generatorDir: path.resolve(process.cwd(), 'node_modules', 'react-native', 'local-cli', 'generator'),
requiredVersion,
cliArgs,
};

try {
await checkForUpdates();

log.info('Read package.json files');
const {rnPak, pak} = readPackageFiles();
Expand Down

0 comments on commit b20b206

Please sign in to comment.