Skip to content

Commit b408772

Browse files
0420syjJosh-Cena
andauthored
fix(core): docusaurus CLI should detect the correct yarn version when suggesting upgrades (facebook#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>
1 parent 6939444 commit b408772

File tree

1 file changed

+25
-9
lines changed

1 file changed

+25
-9
lines changed

packages/docusaurus/bin/beforeCli.mjs

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import fs from 'fs-extra';
1111
import path from 'path';
1212
import {createRequire} from 'module';
13+
import shell from 'shelljs';
1314
import logger from '@docusaurus/logger';
1415
import semver from 'semver';
1516
import updateNotifier from 'update-notifier';
@@ -105,18 +106,33 @@ export default async function beforeCli() {
105106
.map((p) => p.concat('@latest'))
106107
.join(' ');
107108

109+
const getYarnVersion = async () => {
110+
if (!(await fs.pathExists(path.resolve('yarn.lock')))) {
111+
return undefined;
112+
}
113+
114+
const yarnVersionResult = shell.exec('yarn --version', {silent: true});
115+
if (yarnVersionResult?.code === 0) {
116+
const majorVersion = parseInt(
117+
yarnVersionResult.stdout?.trim().split('.')[0] ?? '',
118+
10,
119+
);
120+
if (!Number.isNaN(majorVersion)) {
121+
return majorVersion;
122+
}
123+
}
124+
125+
return undefined;
126+
};
127+
108128
const getUpgradeCommand = async () => {
109-
const isYarnUsed = await fs.pathExists(path.resolve('yarn.lock'));
110-
if (!isYarnUsed) {
129+
const yarnVersion = await getYarnVersion();
130+
if (!yarnVersion) {
111131
return `npm i ${siteDocusaurusPackagesForUpdate}`;
112132
}
113-
114-
const isYarnClassicUsed = !(await fs.pathExists(
115-
path.resolve('.yarnrc.yml'),
116-
));
117-
return isYarnClassicUsed
118-
? `yarn upgrade ${siteDocusaurusPackagesForUpdate}`
119-
: `yarn up ${siteDocusaurusPackagesForUpdate}`;
133+
return yarnVersion >= 2
134+
? `yarn up ${siteDocusaurusPackagesForUpdate}`
135+
: `yarn upgrade ${siteDocusaurusPackagesForUpdate}`;
120136
};
121137

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

0 commit comments

Comments
 (0)