Skip to content

Commit

Permalink
fix(v2): Fix update-notifier not run at first and not notifying consi…
Browse files Browse the repository at this point in the history
…stently (#5110)

* Fix update-notifier issues

* update for notifier disabled
  • Loading branch information
slorber authored Jul 1, 2021
1 parent d65cc9d commit fbdeefc
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 66 deletions.
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 &&
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

0 comments on commit fbdeefc

Please sign in to comment.