-
Notifications
You must be signed in to change notification settings - Fork 932
feat: notify of new major RN version at most once a week #268
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
thymikee
merged 20 commits into
react-native-community:master
from
matei-radu:feature/189-notify-updates
Jun 6, 2019
Merged
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
471e12a
Add getLatestRelease
matei-radu cdeda70
Add getNewerReleaseDataIfAvailable
matei-radu cdfd8be
Check for newer release in cliEntry
matei-radu 535d111
Rename some parts of the release checker
matei-radu 02f275a
Add new RN printing utility
matei-radu e589b17
Replace boxen with logger, simplify message
matei-radu 76dd8cc
Extract release cache manager to own file
matei-radu 8146945
Add one week interval to new printNewRelease
matei-radu 060cece
Force RN version checking before command execution
matei-radu 8956aaa
Check for new releases against rn-diff-purge
matei-radu 2b923ad
Improve logging for printNewRelease
matei-radu 34accf0
Safeguard new release check in cliEntry
matei-radu dc1c496
Log getLatestRelease error in verbose mode
matei-radu 83463fa
Name printNewRelease function
matei-radu 733c1dc
Use resolveNodeModuleDir helper
matei-radu 953a5c0
Check for new release after setupAndRun
matei-radu 56cee5a
make it run after the command
thymikee 41e316c
Move cache age check before network fetch
matei-radu 043640f
Create cache dir in os.homedir()
matei-radu 7e50dee
avoid loading config twice
thymikee File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
140 changes: 140 additions & 0 deletions
140
packages/cli/src/tools/releaseChecker/getLatestRelease.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,140 @@ | ||
| /** | ||
| * @flow | ||
| */ | ||
| import https from 'https'; | ||
| import semver from 'semver'; | ||
| import logger from '../logger'; | ||
| import cacheManager from './releaseCacheManager'; | ||
|
|
||
| export type Release = { | ||
| version: string, | ||
| changelogUrl: string, | ||
| }; | ||
|
|
||
| /** | ||
| * Checks via GitHub API if there is a newer stable React Native release and, | ||
| * if it exists, returns the release data. | ||
| * | ||
| * If the latest release is not newer or if it's a prerelease, the function | ||
| * will return undefined. | ||
| */ | ||
| export default async function getLatestRelease( | ||
| name: string, | ||
| currentVersion: string, | ||
| ) { | ||
| logger.debug('Checking for a newer version of React Native'); | ||
| try { | ||
| logger.debug(`Current version: ${currentVersion}`); | ||
|
|
||
| const cachedLatest = cacheManager.get(name, 'latestVersion'); | ||
|
|
||
| if (cachedLatest) { | ||
| logger.debug(`Cached release version: ${cachedLatest}`); | ||
| } | ||
|
|
||
| const aWeek = 7 * 24 * 60 * 60 * 1000; | ||
| const lastChecked = cacheManager.get(name, 'lastChecked'); | ||
| const now = new Date(); | ||
| if (lastChecked && now - new Date(lastChecked) < aWeek) { | ||
| logger.debug('Cached release is still recent, skipping remote check'); | ||
| return; | ||
| } | ||
|
|
||
| logger.debug('Checking for newer releases on GitHub'); | ||
| const eTag = cacheManager.get(name, 'eTag'); | ||
| const latestVersion = await getLatestRnDiffPurgeVersion(name, eTag); | ||
| logger.debug(`Latest release: ${latestVersion}`); | ||
|
|
||
| if ( | ||
| semver.compare(latestVersion, currentVersion) === 1 && | ||
| !semver.prerelease(latestVersion) | ||
| ) { | ||
| return { | ||
| version: latestVersion, | ||
| changelogUrl: buildChangelogUrl(latestVersion), | ||
| }; | ||
| } | ||
| } catch (e) { | ||
| logger.debug( | ||
| 'Something went wrong with remote version checking, moving on', | ||
| ); | ||
| logger.debug(e); | ||
| } | ||
| } | ||
|
|
||
| function buildChangelogUrl(version: string) { | ||
| return `https://github.com/facebook/react-native/releases/tag/v${version}`; | ||
| } | ||
|
|
||
| /** | ||
| * Returns the most recent React Native version available to upgrade to. | ||
| */ | ||
| async function getLatestRnDiffPurgeVersion(name: string, eTag: ?string) { | ||
| const options = { | ||
| hostname: 'api.github.com', | ||
| path: '/repos/react-native-community/rn-diff-purge/tags', | ||
| // https://developer.github.com/v3/#user-agent-required | ||
| headers: ({'User-Agent': 'React-Native-CLI'}: Headers), | ||
| }; | ||
|
|
||
| if (eTag) { | ||
| options.headers['If-None-Match'] = eTag; | ||
| } | ||
|
|
||
| const response = await httpsGet(options); | ||
|
|
||
| // Remote is newer. | ||
| if (response.statusCode === 200) { | ||
| const latestVersion = JSON.parse(response.body)[0].name.substring(8); | ||
|
|
||
| // Update cache only if newer release is stable. | ||
| if (!semver.prerelease(latestVersion)) { | ||
| logger.debug(`Saving ${response.eTag} to cache`); | ||
| cacheManager.set(name, 'eTag', response.eTag); | ||
| cacheManager.set(name, 'latestVersion', latestVersion); | ||
| } | ||
|
|
||
| return latestVersion; | ||
| } | ||
|
|
||
| // Cache is still valid. | ||
| if (response.statusCode === 304) { | ||
| const latestVersion = cacheManager.get(name, 'latestVersion'); | ||
| if (latestVersion) { | ||
| return latestVersion; | ||
| } | ||
| } | ||
|
|
||
| // Should be returned only if something went wrong. | ||
| return '0.0.0'; | ||
| } | ||
|
|
||
| type Headers = { | ||
| 'User-Agent': string, | ||
| [header: string]: string, | ||
| }; | ||
|
|
||
| function httpsGet(options: any) { | ||
thymikee marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| return new Promise((resolve, reject) => { | ||
| https | ||
| .get(options, result => { | ||
| let body = ''; | ||
|
|
||
| result.setEncoding('utf8'); | ||
| result.on('data', data => { | ||
| body += data; | ||
| }); | ||
|
|
||
| result.on('end', () => { | ||
| resolve({ | ||
| body, | ||
| eTag: result.headers.etag, | ||
| statusCode: result.statusCode, | ||
| }); | ||
| }); | ||
|
|
||
| result.on('error', error => reject(error)); | ||
| }) | ||
| .on('error', error => reject(error)); | ||
| }); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| /** | ||
| * @flow | ||
| */ | ||
| import chalk from 'chalk'; | ||
| import logger from '../logger'; | ||
| import type {Release} from './getLatestRelease'; | ||
| import cacheManager from './releaseCacheManager'; | ||
|
|
||
| /** | ||
| * Notifies the user that a newer version of React Native is available. | ||
| */ | ||
| export default function printNewRelease( | ||
| name: string, | ||
| latestRelease: Release, | ||
| currentVersion: string, | ||
| ) { | ||
| logger.info( | ||
| `React Native v${ | ||
| latestRelease.version | ||
| } is now available (your project is running on v${currentVersion}).`, | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. cc @cpojer is this ok, or do you have better ideas? |
||
| ); | ||
| logger.info(`Changelog: ${chalk.dim.underline(latestRelease.changelogUrl)}.`); | ||
| logger.info(`To upgrade, run "${chalk.bold('react-native upgrade')}".`); | ||
|
|
||
| cacheManager.set(name, 'lastChecked', new Date().toISOString()); | ||
| } | ||
69 changes: 69 additions & 0 deletions
69
packages/cli/src/tools/releaseChecker/releaseCacheManager.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| /** | ||
| * @flow | ||
| */ | ||
| import path from 'path'; | ||
| import fs from 'fs'; | ||
| import os from 'os'; | ||
| import mkdirp from 'mkdirp'; | ||
| import logger from '../logger'; | ||
|
|
||
| type ReleaseCacheKey = 'eTag' | 'lastChecked' | 'latestVersion'; | ||
| type Cache = {[key: ReleaseCacheKey]: string}; | ||
|
|
||
| function loadCache(name: string): ?Cache { | ||
| try { | ||
| const cacheRaw = fs.readFileSync( | ||
| path.resolve(getCacheRootPath(), name), | ||
| 'utf8', | ||
| ); | ||
| const cache = JSON.parse(cacheRaw); | ||
| return cache; | ||
| } catch (e) { | ||
| if (e.code === 'ENOENT') { | ||
| // Create cache file since it doesn't exist. | ||
| saveCache(name, {}); | ||
| } | ||
| logger.debug('No release cache found'); | ||
| } | ||
| } | ||
|
|
||
| function saveCache(name: string, cache: Cache) { | ||
| fs.writeFileSync( | ||
| path.resolve(getCacheRootPath(), name), | ||
| JSON.stringify(cache, null, 2), | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * Returns the path string of `$HOME/.react-native-cli`. | ||
| * | ||
| * In case it doesn't exist, it will be created. | ||
| */ | ||
| function getCacheRootPath() { | ||
| const cachePath = path.resolve(os.homedir(), '.react-native-cli', 'cache'); | ||
| if (!fs.existsSync(cachePath)) { | ||
| mkdirp(cachePath); | ||
| } | ||
|
|
||
| return cachePath; | ||
| } | ||
|
|
||
| function get(name: string, key: ReleaseCacheKey): ?string { | ||
| const cache = loadCache(name); | ||
| if (cache) { | ||
| return cache[key]; | ||
| } | ||
| } | ||
|
|
||
| function set(name: string, key: ReleaseCacheKey, value: string) { | ||
| const cache = loadCache(name); | ||
| if (cache) { | ||
| cache[key] = value; | ||
| saveCache(name, cache); | ||
| } | ||
| } | ||
|
|
||
| export default { | ||
| get, | ||
| set, | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.

Uh oh!
There was an error while loading. Please reload this page.