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

fix(v2): Fix update-notifier not run at first and not notifying consistently #5110

Merged
merged 2 commits into from
Jul 1, 2021
Merged
Show file tree
Hide file tree
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
100 changes: 100 additions & 0 deletions packages/docusaurus/bin/beforeCli.js
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 &&
Copy link
Collaborator

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...

Copy link
Collaborator Author

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 😅

Copy link
Collaborator

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...

Copy link
Collaborator Author

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 ;)

Copy link
Collaborator

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.

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);
}
67 changes: 1 addition & 66 deletions packages/docusaurus/bin/docusaurus.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,8 @@
*/

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 @@ -27,69 +23,8 @@ const {
writeTranslations,
writeHeadingIds,
} = require('../lib');
const {
name,
version,
engines: {node: requiredVersion},
} = require('../package.json');

// notify user if @docusaurus packages is outdated
const notifier = updateNotifier({
pkg: {
name,
version,
},
});

// allow the user to be notified for updates on the first run
if (notifier.lastUpdateCheck === Date.now()) {
notifier.lastUpdateCheck = 0;
}

if (notifier.update && semver.gt(this.update.latest, this.update.current)) {
// 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.js version not met :(`) +
chalk.yellow(
`\n\nYou are using Node.js ${process.version}. We require Node.js ${requiredVersion} or up!\n`,
),
);
process.exit(1);
}
require('./beforeCli');

cli.version(require('../package.json').version).usage('<command> [options]');

Expand Down