Skip to content

Commit

Permalink
fix(core): docusaurus CLI should detect the correct yarn version when…
Browse files Browse the repository at this point in the history
… 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>
  • Loading branch information
0420syj and Josh-Cena committed May 31, 2023
1 parent 6939444 commit b408772
Showing 1 changed file with 25 additions and 9 deletions.
34 changes: 25 additions & 9 deletions packages/docusaurus/bin/beforeCli.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -105,18 +106,33 @@ export default async function beforeCli() {
.map((p) => p.concat('@latest'))
.join(' ');

const getYarnVersion = async () => {
if (!(await fs.pathExists(path.resolve('yarn.lock')))) {
return undefined;
}

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 isYarnUsed = await fs.pathExists(path.resolve('yarn.lock'));
if (!isYarnUsed) {
const yarnVersion = await getYarnVersion();
if (!yarnVersion) {
return `npm i ${siteDocusaurusPackagesForUpdate}`;
}

const isYarnClassicUsed = !(await fs.pathExists(
path.resolve('.yarnrc.yml'),
));
return isYarnClassicUsed
? `yarn upgrade ${siteDocusaurusPackagesForUpdate}`
: `yarn up ${siteDocusaurusPackagesForUpdate}`;
return yarnVersion >= 2
? `yarn up ${siteDocusaurusPackagesForUpdate}`
: `yarn upgrade ${siteDocusaurusPackagesForUpdate}`;
};

/** @type {import('boxen').Options} */
Expand Down

0 comments on commit b408772

Please sign in to comment.