fix(core): docusaurus CLI should detect the correct yarn version when suggesting upgrades (#9006)

* fix(core): Correct yarn version detection

Correct yarn version detection by checking `yarnPath` value inside `.yarnrc.yml` file
Add js-yaml in package.json

* Change to use `shelljs` instead of `js-yaml`

* Change echo mode to silent

* Check `yarn.lock` exist, before version checking

* Remove unnecessary optional chaining

Nullish coalescing operator still provides the fallback value

Co-authored-by: Joshua Chen <sidachen2003@gmail.com>

---------

Co-authored-by: Joshua Chen <sidachen2003@gmail.com>
This commit is contained in:
Wan Sim 2023-05-31 18:54:44 +09:00 committed by GitHub
parent 6939444c31
commit b4087720cb
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -10,6 +10,7 @@
import fs from 'fs-extra'; import fs from 'fs-extra';
import path from 'path'; import path from 'path';
import {createRequire} from 'module'; import {createRequire} from 'module';
import shell from 'shelljs';
import logger from '@docusaurus/logger'; import logger from '@docusaurus/logger';
import semver from 'semver'; import semver from 'semver';
import updateNotifier from 'update-notifier'; import updateNotifier from 'update-notifier';
@ -105,18 +106,33 @@ export default async function beforeCli() {
.map((p) => p.concat('@latest')) .map((p) => p.concat('@latest'))
.join(' '); .join(' ');
const getUpgradeCommand = async () => { const getYarnVersion = async () => {
const isYarnUsed = await fs.pathExists(path.resolve('yarn.lock')); if (!(await fs.pathExists(path.resolve('yarn.lock')))) {
if (!isYarnUsed) { return undefined;
return `npm i ${siteDocusaurusPackagesForUpdate}`;
} }
const isYarnClassicUsed = !(await fs.pathExists( const yarnVersionResult = shell.exec('yarn --version', {silent: true});
path.resolve('.yarnrc.yml'), if (yarnVersionResult?.code === 0) {
)); const majorVersion = parseInt(
return isYarnClassicUsed yarnVersionResult.stdout?.trim().split('.')[0] ?? '',
? `yarn upgrade ${siteDocusaurusPackagesForUpdate}` 10,
: `yarn up ${siteDocusaurusPackagesForUpdate}`; );
if (!Number.isNaN(majorVersion)) {
return majorVersion;
}
}
return undefined;
};
const getUpgradeCommand = async () => {
const yarnVersion = await getYarnVersion();
if (!yarnVersion) {
return `npm i ${siteDocusaurusPackagesForUpdate}`;
}
return yarnVersion >= 2
? `yarn up ${siteDocusaurusPackagesForUpdate}`
: `yarn upgrade ${siteDocusaurusPackagesForUpdate}`;
}; };
/** @type {import('boxen').Options} */ /** @type {import('boxen').Options} */