antfu / install-pkg

Install package programmatically.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

preferOffline option not working with yarn v2+

cimchd opened this issue · comments

Describe the bug

--preferOffline is not available anymore since yarn v2+. Therefor an error occurs:

Unsupported option name ("--prefer-offline").

$ yarn add [--json] [-E,--exact] [-T,--tilde] [-C,--caret] [-D,--dev] [-P,--peer] [-O,--optional] [--prefer-dev] [-i,--interactive] [--cached] [--mode #0] ...
/home/chd/impot-pkg-yarn-3/node_modules/execa/lib/error.js:60
                error = new Error(message);
                        ^

Error: Command failed with exit code 1: yarn add --prefer-offline vite

Instead of

yarn add --prefer-offline vite

The right syntax for this example for yarn v3 should probably be:

yarn add --cached vite

I looked into the code if a pr is easy but there is a though design decission to make how to detect the yarn version. One way could be to read the package.json file. If yarn 2 oder 3 is installed, the field "packageManager" is filled.
E.g.:

// package.json
{
  // ...
  "packageManager": "yarn@3.2.3"
}

Reproduction

https://github.com/cimchd/impot-pkg-yarn-3

System Info

System:
    OS: Linux 4.19 Ubuntu 20.04 LTS (Focal Fossa)
    CPU: (8) x64 Intel(R) Core(TM) i7-7700HQ CPU @ 2.80GHz
    Memory: 17.24 GB / 19.55 GB
    Container: Yes
    Shell: 5.0.16 - /bin/bash
  Binaries:
    Node: 16.14.0 - /usr/local/bin/node
    Yarn: 3.2.2 - ~/.yarn/bin/yarn
    npm: 8.3.1 - /usr/local/bin/npm

Used Package Manager

yarn

Validations

  • Follow our Code of Conduct
  • Read the Contributing Guide.
  • Check that there isn't already an issue that reports the same bug to avoid creating a duplicate.
  • Check that this is a concrete bug. For Q&A, please open a GitHub Discussion instead.
  • The provided reproduction is a minimal reproducible of the bug.

I made a pr. I think best way to detect the version is parsing the env var "npm_config_user_agent".