-
-
Notifications
You must be signed in to change notification settings - Fork 8.7k
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
fix(v2): Fix update-notifier not run at first and not notifying consistently #5110
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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 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,100 @@ | ||
/** | ||
* Copyright (c) Facebook, Inc. and its affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
const chalk = require('chalk'); | ||
const fs = require('fs-extra'); | ||
const semver = require('semver'); | ||
const path = require('path'); | ||
const updateNotifier = require('update-notifier'); | ||
const boxen = require('boxen'); | ||
|
||
const { | ||
name, | ||
version, | ||
engines: {node: requiredVersion}, | ||
} = require('../package.json'); | ||
|
||
// Notify user if @docusaurus packages is outdated | ||
// | ||
// Note: this is a 2-step process to avoid delaying cli usage by awaiting a response: | ||
// - 1st run: trigger background job to check releases + store result | ||
// - 2nd run: display potential update to users | ||
// | ||
// Note: even if the | ||
// | ||
// cache data is stored in ~/.config/configstore/update-notifier-@docusaurus | ||
// | ||
const notifier = updateNotifier({ | ||
pkg: { | ||
name, | ||
version, | ||
}, | ||
// Check is in background so it's fine to use a small value like 1h | ||
// Use 0 for debugging | ||
updateCheckInterval: 1000 * 60 * 60, | ||
// updateCheckInterval: 0 | ||
}); | ||
|
||
// Hacky way to ensure we check for updates on first run | ||
// Note: the notification will only happen in the 2nd run | ||
// See https://github.com/yeoman/update-notifier/issues/209 | ||
if ( | ||
!notifier.disabled && | ||
Date.now() - notifier.config.get('lastUpdateCheck') < 50 | ||
) { | ||
notifier.config.set('lastUpdateCheck', 0); | ||
notifier.check(); | ||
} | ||
|
||
if (notifier.update && notifier.update.current !== notifier.update.latest) { | ||
// Because notifier clears cached data after reading it, leading to notifier not consistently displaying the update | ||
// See https://github.com/yeoman/update-notifier/issues/209 | ||
notifier.config.set('update', notifier.update); | ||
|
||
// eslint-disable-next-line import/no-dynamic-require, global-require | ||
const sitePkg = require(path.resolve(process.cwd(), 'package.json')); | ||
const siteDocusaurusPackagesForUpdate = Object.keys(sitePkg.dependencies) | ||
.filter((p) => p.startsWith('@docusaurus')) | ||
.map((p) => p.concat('@latest')) | ||
.join(' '); | ||
const isYarnUsed = fs.existsSync(path.resolve(process.cwd(), 'yarn.lock')); | ||
const upgradeCommand = isYarnUsed | ||
? `yarn upgrade ${siteDocusaurusPackagesForUpdate}` | ||
: `npm i ${siteDocusaurusPackagesForUpdate}`; | ||
|
||
const boxenOptions = { | ||
padding: 1, | ||
margin: 1, | ||
align: 'center', | ||
borderColor: 'yellow', | ||
borderStyle: 'round', | ||
}; | ||
|
||
const docusaurusUpdateMessage = boxen( | ||
`Update available ${chalk.dim(`${notifier.update.current}`)}${chalk.reset( | ||
' → ', | ||
)}${chalk.green( | ||
`${notifier.update.latest}`, | ||
)}\n\nTo upgrade Docusaurus packages with the latest version, run the following command:\n${chalk.cyan( | ||
`${upgradeCommand}`, | ||
)}`, | ||
boxenOptions, | ||
); | ||
|
||
console.log(docusaurusUpdateMessage); | ||
} | ||
|
||
// notify user if node version needs to be updated | ||
if (!semver.satisfies(process.version, requiredVersion)) { | ||
console.log( | ||
chalk.red(`\nMinimum Node version not met :(`) + | ||
chalk.yellow( | ||
`\n\nYou are using Node ${process.version}. We require Node ${requiredVersion} or up!\n`, | ||
), | ||
); | ||
process.exit(1); | ||
} |
This file contains 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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@slorber I didn't find this field anywhere in the docs...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this update notifier package is a bit complicated, you'd rather read source code to understand how it works 😅
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The reason is because I'm touching that source code a little and
ts-check
throws an error because this field is not in the typedef. That means it's not public API? I do see that exists, just not sure if it's actually useful...There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
pretty sure we had failures without that check, I'd rather not change something that works just to please TS ;)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I clearly see that. I'm not saying to remove it, just curious how you dug it up if it's neither typedefed nor documented.