Skip to content

Conversation

@wannysim
Copy link
Contributor

@wannysim wannysim commented May 24, 2023

Pre-flight checklist

  • I have read the Contributing Guidelines on pull requests.
  • If this is a code change: I have written unit tests and/or added dogfooding pages to fully verify the new behavior.
  • If this is a new API or substantial change: the PR has an accompanying issue (closes #0000) and the maintainers have approved on my working plan.

Motivation

I've checked PR #8908.
Great idea to check which Yarn verison is being used.

However, the presence of a .yarnrc.yml file alone is not a definitive indicator of Yarn 2+ being used, as this file can be used by Yarn 1.x as well.

Check out the capture image below

screenshot1

This can be happened when you change Yarn 2+ -> Yarn 1

yarn set version 1

So, I think we should check yarnPath value (by using js-yaml),
to make sure what Yarn version is being used.

Test Plan

  • Check out an older docusaurus project and copy the file in place.
  • Install the project with npm
  • Run a npm run start
  • Close and run it again
    • Confirm the notice had an appropriate npm i command
  • rm -rf node_modules
  • Install with yarn classic
  • Run yarn start
  • Close and run it again
    • Confirm the notice had an appropriate npm i command
  • rm -rf node_modules
  • Repeat with yarn 2

Test links

Deploy preview: https://deploy-preview-9006--docusaurus-2.netlify.app/

Related issues/PRs

Related PR : #8908

@facebook-github-bot facebook-github-bot added the CLA Signed Signed Facebook CLA label May 24, 2023
@netlify
Copy link

netlify bot commented May 24, 2023

[V2]

Built without sensitive environment variables

Name Link
🔨 Latest commit 47ec4f5
🔍 Latest deploy log https://app.netlify.com/sites/docusaurus-2/deploys/646e31624aaf150008133b5c
😎 Deploy Preview https://deploy-preview-9006--docusaurus-2.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site settings.

@github-actions
Copy link

github-actions bot commented May 24, 2023

⚡️ Lighthouse report for the deploy preview of this PR

URL Performance Accessibility Best Practices SEO PWA Report
/ 🟠 63 🟢 97 🟢 100 🟢 100 🟠 89 Report
/docs/installation 🟠 81 🟢 100 🟢 100 🟢 100 🟠 89 Report

@netlify
Copy link

netlify bot commented May 24, 2023

[V2]

Built without sensitive environment variables

Name Link
🔨 Latest commit c4db321
🔍 Latest deploy log https://app.netlify.com/sites/docusaurus-2/deploys/6476eff07113f0000844c836
😎 Deploy Preview https://deploy-preview-9006--docusaurus-2.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site settings.

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

@slorber slorber left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That looks a bit complicated to me 🤪

What about:

import shell from 'shelljs';

async function getYarnVersion() {
  const fallbackYarnVersion = 1;
  const yarnVersionResult = shell.exec("yarn --version");
  if (yarnVersionResult.code === 0) {
    const majorVersion = parseInt(yarnVersionResult.stdout.split(".")[0]);
    if ( typeof majorVersion === "number" && !Number.isNaN(majorVersion) ) {
      return majorVersion;
    }
  }
  if (await fs.pathExists(path.resolve('yarn.lock'))) {
    return fallbackYarnVersion;
  }
  return undefined;
}

And then usage:

    const getUpgradeCommand = async () => {
      const yarnVersion = await getYarnVersion();
      if (!yarnVersion) {
        return `npm i ${siteDocusaurusPackagesForUpdate}`;
      }
      return yarnVersion === 1
        ? `yarn upgrade ${siteDocusaurusPackagesForUpdate}`
        : `yarn up ${siteDocusaurusPackagesForUpdate}`;
    };

@wannysim
Copy link
Contributor Author

That looks a bit complicated to me 🤪

What about:

import shell from 'shelljs';

async function getYarnVersion() {
  const fallbackYarnVersion = 1;
  const yarnVersionResult = shell.exec("yarn --version");
  if (yarnVersionResult.code === 0) {
    const majorVersion = parseInt(yarnVersionResult.stdout.split(".")[0]);
    if ( typeof majorVersion === "number" && !Number.isNaN(majorVersion) ) {
      return majorVersion;
    }
  }
  if (await fs.pathExists(path.resolve('yarn.lock'))) {
    return fallbackYarnVersion;
  }
  return undefined;
}

And then usage:

    const getUpgradeCommand = async () => {
      const yarnVersion = await getYarnVersion();
      if (!yarnVersion) {
        return `npm i ${siteDocusaurusPackagesForUpdate}`;
      }
      return yarnVersion === 1
        ? `yarn upgrade ${siteDocusaurusPackagesForUpdate}`
        : `yarn up ${siteDocusaurusPackagesForUpdate}`;
    };

Actually, my first idea was more close to this one🤣
But I was wary of letting it run shell commands.

If there are no security concerns, I fully agree with this improvement.
Not only is it more clear, but I think it's more concise.

Copy link
Contributor Author

@wannysim wannysim left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I made a new commit based on the review !

@wannysim wannysim requested a review from slorber May 31, 2023 02:18
@Josh-Cena Josh-Cena changed the title fix(core): Correct yarn version detection fix(core): correct yarn version detection May 31, 2023
@Josh-Cena Josh-Cena added the pr: bug fix This PR fixes a bug in a past release. label May 31, 2023
@wannysim wannysim requested a review from Josh-Cena May 31, 2023 06:34
�Nullish coalescing operator still provides the fallback value

Co-authored-by: Joshua Chen <[email protected]>
@wannysim wannysim requested a review from Josh-Cena May 31, 2023 07:00
Copy link
Collaborator

@slorber slorber left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM thanks 👍

@slorber slorber changed the title fix(core): correct yarn version detection fix(core): docusaurus CLI should detect the correct yarn version when suggesting upgrades May 31, 2023
@slorber slorber merged commit b408772 into facebook:main May 31, 2023
@wannysim wannysim deleted the correct-yarn-version-detection branch May 31, 2023 10:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed Signed Facebook CLA pr: bug fix This PR fixes a bug in a past release.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants