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