More accurate check for latest version

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 https://github.com/facebook/react-native/pull/11188

Differential Revision: D4244002

Pulled By: bestander

fbshipit-source-id: 772044750a933663cb516201d09e2873462cca4a
This commit is contained in:
Martin Konicek 2016-11-29 06:10:24 -08:00 коммит произвёл Facebook Github Bot
Родитель f1a5233fd2
Коммит b20b20656a
2 изменённых файлов: 25 добавлений и 14 удалений

4
react-native-git-upgrade/checks.js поставляемый
Просмотреть файл

@ -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.'
);
}

35
react-native-git-upgrade/cliEntry.js поставляемый
Просмотреть файл

@ -156,6 +156,28 @@ function runYeomanGenerators(generatorDir, appName, verbose) {
return new Promise((resolve) => env.run(generatorArgs, {upgrade: true, force: true}, resolve));
}
/**
* 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 (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'),
@ -165,18 +187,7 @@ async function run(requiredVersion, cliArgs) {
};
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) {
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"'
);
}
await checkForUpdates();
log.info('Read package.json files');
const {rnPak, pak} = readPackageFiles();