Skip to content
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

refactor(v2): improve notifier message #4257

Merged
merged 2 commits into from
Feb 23, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 29 additions & 10 deletions packages/docusaurus/bin/docusaurus.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,12 @@
*/

const chalk = require('chalk');
const fs = require('fs-extra');
const semver = require('semver');
const path = require('path');
const cli = require('commander');
const updateNotifier = require('update-notifier');
const boxen = require('boxen');
const {
build,
swizzle,
Expand All @@ -21,15 +24,18 @@ const {
clear,
writeTranslations,
} = require('../lib');
const requiredVersion = require('../package.json').engines.node;
const pkg = require('../package.json');
const updateNotifier = require('update-notifier');
const boxen = require('boxen');
const {
name,
version,
engines: {node: requiredVersion},
} = require('../package.json');

// notify user if @docusaurus/core is outdated
// notify user if @docusaurus packages is outdated
const notifier = updateNotifier({
pkg,
updateCheckInterval: 1000 * 60 * 60 * 24, // one day
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pkg: {
name,
version,
},
});

// allow the user to be notified for updates on the first run
Expand All @@ -38,6 +44,17 @@ if (notifier.lastUpdateCheck === Date.now()) {
}

if (notifier.update && notifier.update.current !== notifier.update.latest) {
// eslint-disable-next-line import/no-dynamic-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,
Expand All @@ -49,9 +66,11 @@ if (notifier.update && notifier.update.current !== notifier.update.latest) {
const docusaurusUpdateMessage = boxen(
`Update available ${chalk.dim(`${notifier.update.current}`)}${chalk.reset(
' → ',
)}${chalk.green(`${notifier.update.latest}`)}\nRun ${chalk.cyan(
'yarn upgrade @docusaurus/core',
)} to update`,
)}${chalk.green(
`${notifier.update.latest}`,
)}\n\nTo upgrade Docusaurus packages with the latest version, run the following command:\n${chalk.cyan(
`${upgradeCommand}`,
)}`,
boxenOptions,
);

Expand Down