Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Yarn 换源命令已过时 #53

Closed
MrWillCom opened this issue Aug 28, 2024 · 8 comments
Closed

Yarn 换源命令已过时 #53

MrWillCom opened this issue Aug 28, 2024 · 8 comments
Assignees
Labels
语言 语言
Milestone

Comments

@MrWillCom
Copy link
Contributor

对 npm 等换源时,发生如下错误:

❯ chsrc set npm
[chsrc 检查] ✓ 命令 npm 存在
[chsrc 检查] ✓ 命令 yarn 存在
[chsrc 检查] ✓ 命令 pnpm 存在
[chsrc 检查] ✓ 命令 curl 存在
测速 https://npmmirror.com/ ... 6.27 MByte/s
测速 https://mirrors.huaweicloud.com/ ... 6.93 MByte/s
测速 https://mirrors.zju.edu.cn/ ... 8.41 MByte/s
最快镜像站: 浙江大学开源软件镜像站
选中镜像站: ZJU (zju)
--------------------------------
[chsrc 运行] npm config set registry https://mirrors.zju.edu.cn/npm
[chsrc 运行] ✓ 命令执行成功

[chsrc 运行] yarn config set registry https://mirrors.zju.edu.cn/npm
Usage Error: Couldn't find a configuration settings named "registry"

$ yarn config set [--json] [-H,--home] <name> <value>
[chsrc 运行] x 命令执行失败,返回码 256
chsrc: 关键错误,强制结束

事实上,自 Yarn v2 开始,全局换源的指令变更为了:

$ yarn config set npmRegistryServer -H <registry>

以下文档可供参考:


此外,如果已在 npm 中配置全局镜像源,且 pnpm 中没有配置自定义源,那么 pnpm 会默认跟随 npm 的配置而不需要额外对之单独配置。当然,对三个包管理器都执行配置镜像源的命令也是一种更可靠的做法。

@MrWillCom MrWillCom changed the title Yarn 换源命令不是最新 Yarn 换源命令已过时 Aug 28, 2024
ccmywish added a commit that referenced this issue Aug 28, 2024
@ccmywish ccmywish self-assigned this Aug 28, 2024
@ccmywish ccmywish added the 语言 语言 label Aug 28, 2024
@ccmywish ccmywish added this to the v0.1.9 milestone Aug 28, 2024
@ccmywish
Copy link
Contributor

ccmywish commented Aug 28, 2024

@MrWillCom

👍 问题已确认,感谢你详尽的描述和准确的文档引用。对 Node.js 的支持需要像你这样的专业前端开发人员提供帮助。

Yarn v2 的支持代码已实现,我在本地已简单测试过了,可审查:df8e7c2

由于对此换源方法的修正很重要,你可在此 https://github.com/RubyMetric/chsrc/blob/main/src/recipe/lang/Node.js.c#L5 加上你的信息,以记载你的贡献 🤝

新版本将于近日发布。

MrWillCom added a commit to MrWillCom/chsrc that referenced this issue Aug 28, 2024
@MrWillCom
Copy link
Contributor Author

🚀 谢谢你的超快 fix!

我在 #56 里提交了关于贡献者的变更

ccmywish pushed a commit that referenced this issue Aug 28, 2024
@ccmywish
Copy link
Contributor

🤝 已合并,感谢贡献。若使用新版本换源无问题,可随时关闭此 issue.

@tapqr
Copy link

tapqr commented Aug 29, 2024

指定npm工具,为啥会设置yarn的源,而且是设置全局的。

命令:

chsrc set -local npm npmmirror

结果:

[chsrc 检查] ✓ 命令 npm 存在
[chsrc 检查] ✓ 命令 yarn 存在
[chsrc 检查] x 命令 pnpm 不存在
选中镜像站: npmmirror (npmmirror)
--------------------------------
[chsrc 运行] npm config --location project set registry https://registry.npmmirror.com
[chsrc 运行] ✓ 命令执行成功

[chsrc 运行] yarn config set registry https://registry.npmmirror.com
yarn config v1.22.22
success Set "registry" to "https://registry.npmmirror.com".
✨  Done in 0.04s.
[chsrc 运行] ✓ 命令执行成功

--------------------------------
chsrc: 全自动换源完成, 感谢镜像提供方: npmmirror (阿里云赞助)

@ccmywish
Copy link
Contributor

@tapqr

你好,感谢反馈

  1. 指定npm工具,为啥会设置yarn的源

    这是因为,npm, yarn, pnpm, node 都其实只是 nodejs 的别称。选择任何一个别称只是为了触发 nodejs 这个 recipe 换源方法。这个方法中,会对 npm, yarn, pnpm 三个包管理器都换源。

    因为大部分用户都是直接换三个源。如果你有仅换一个的需求,可以描述下原因,如果合理,可以后续提供支持。


  1. 而且是设置全局的

    因为对 yarn 1.22 暂时没有提供局部换源支持。你可以通过 chsrc ls node 查看支持情况。

    如果你知道如何对 yarn 1.22 实现基于项目的换源,请发一下官方文档链接,我将参照文档实现支持。

image

@tapqr
Copy link

tapqr commented Aug 29, 2024

@ccmywish

个人观点:
1、枪指向哪里就打哪里,不要有啥副作用。这些工具虽然都是nodejs的包管理工具,但是管理包的方式还是有些许差别的。
2、同一,在茫茫码海中找到了-local这个参数 ,说明用户很在意全局环境干扰;如果程序不支持,可以报错或者提示用户,而不是默默的把全局给换了,虽然运行结果有提示,如果没仔细看运行结果的话,可能会给开发环境造成不必要的困扰。

关于yarn基于项目换源,貌似没有命令行的方式,我一般是在项目下创建配置文件,项目成员可以免去那些乱七八糟的配置,特别是存在私有仓库的情况

yarn 1.X.X 在项目根目录创建 .yarnrc

registry "https://registry.npmmirror.com"

yarn > 2.X.X 在项目根目录创建 .yarnrc.yml ,yarn2+初始化的时候一般会自动创建该文件

npmRegistryServer: https://registry.npmmirror.com

docs:

yarn1
yarn2+

@ccmywish
Copy link
Contributor

@tapqr

Yarn v1 的支持

用户在意全局环境的干扰,这是合理的需求,但是仍需要考虑上游软件本身对此需求的实现情况。

根据:yarnpkg/yarn#1037

yarn v1 至少在命令行上不提供基于项目的配置修改,有无 -g 是完全一样的。那么 chsrc 不可能越俎代庖,因为 yarn v1 本身决定了需要让用户手动创建和维护 .yarnrc


对不支持 -local 的 target 的换源

将参考你的意见,以后将在不支持 -local 的换源目标,停止换源


分多个 target 来换源

针对这一点,我自己也曾经考虑过,但是大部分用户反馈的都是想要方便,数位用户甚至想要提供 chsrc set all 一下子换完所有源。这个可以考虑之后分别实现 set npm set yarn set pnpm 三个独立的target,如果用户想要三个全换,再使用 set nodejs

@MrWillCom
Copy link
Contributor Author

MrWillCom commented Aug 29, 2024

对于不同包管理器分开换源,由于 pnpm 会直接使用 npm 的配置文件Yarn 也会继承 npm 的配置,且 v1 的 registry 配置项名称也是和 npm 一样的,所以想要让 npm、Yarn v1 和 pnpm 分开换源可能有些麻烦了,把 Yarn v2 分开倒是可以。

至于当包管理器不支持 -local 换源时则不执行换源的 behavior,我也觉得这样更加妥当。


刚刚试了一下发现 Yarn 不论 v1 v2 的 registry 或者 npmRegistryServer 配置项都是不继承 npm 的配置的,是除此之外的其他配置项有继承的现象。因此 Yarn 的换源的确是可以分开的。

ccmywish added a commit that referenced this issue Sep 10, 2024
ccmywish added a commit that referenced this issue Sep 10, 2024
ccmywish added a commit that referenced this issue Sep 10, 2024
ccmywish added a commit that referenced this issue Sep 10, 2024
ccmywish added a commit that referenced this issue Sep 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
语言 语言
Projects
None yet
Development

No branches or pull requests

3 participants