From b4087720cbdd587428a3199d91e45d2c5ab2f539 Mon Sep 17 00:00:00 2001 From: Wan Sim <0420syj@naver.com> Date: Wed, 31 May 2023 18:54:44 +0900 Subject: [PATCH] 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 --------- Co-authored-by: Joshua Chen --- packages/docusaurus/bin/beforeCli.mjs | 36 +++++++++++++++++++-------- 1 file changed, 26 insertions(+), 10 deletions(-) diff --git a/packages/docusaurus/bin/beforeCli.mjs b/packages/docusaurus/bin/beforeCli.mjs index da868f2330..e9a91576a5 100644 --- a/packages/docusaurus/bin/beforeCli.mjs +++ b/packages/docusaurus/bin/beforeCli.mjs @@ -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} */