antfu-collective / ni

💡 Use the right package manager
MIT License
6.28k stars 208 forks source link

[Feature Request] default configuration upward recursive lookup #74

Closed Wxh16144 closed 2 years ago

Wxh16144 commented 2 years ago

有这么一个场景,假设有一个文件目录为这样:

| ~
├── react-project
│   ├── .nirc (content: yarn) #不支持
│   ├── bar
│   └── foo
└── vue-project
    ├── .nirc (content: pnpm) #不支持
    └── vite-project

我希望在 react-project 目录下使用 yarn, 在 vue-project 目录中使用 pnpm

这个时候我有以下几种方案:

  1. 在目录下的项目的 package.json 文件中添加 "packageManager": "yarn@1.22.0"
  2. 借助 NI_CONFIG_FILE 环境变量,指定 .nirc 文件路径;
  3. 本次请求的功能:使用 findUp 递归向上查找配置文件,如上树形图所示;

上述方案的一些顾虑:

方案1: 如果是三方开源项目,需要手动去添加记录,产生不必要的 git 变更。 方案2: 可能需写一些 shell 脚本来支持目录变更后修改环境变量或者覆盖 .nirc 文件内容,如借助 .zshrc 的 hooks 方案3: 如果实现则会存在一个问题是配置文件混乱,使用者不知道执行命令后使用的是那个包管理器,当其实可以丰富一下帮助文档,比如 ni "?" 目前只是一个 debugger 功能,但可以在这个上面输出更多的内容, config 信息等等等

本次功能请求:

如上树形目录图,在没有明确包管理器时,递归向父目录查找配置文件。

antfu commented 2 years ago
  1. "packageManager" is the standard. Ideally, projects should specify it whenever possible. For OSS, you could create PR to ti.

What's the reason the projects don't have a lockfile? ni expects projects to have lockfiles. .nirc is aim for global configurations like .zshrc, you don't expect to have .zshrc in every directory that changes zsh's behavior when you cd into some folder right?

Wxh16144 commented 2 years ago

嗯嗯,非常感谢你的回复,赞同你说的,就像 .zshrc 配置文件一样,本身就只是一个全局配置。 如果我真的要使用这个功能,在不去开源仓库提交 PR 的情况下,也只有方案2可以满足我个人特殊需求。

另外据 NodJS 官方文档中所说的 packageManager 还处于实验性阶段

Wxh16144 commented 2 years ago

对于我这种不太合理的需求,且属于小众需求,我认为发一个 @wuxh/ni 私人包即可。感谢大佬的包,站在巨人的肩膀上😂

Wxh16144 commented 1 year ago

Sorry to continue this discussion in a closed issue. For this issue, I have implemented a solution myself and it has been stable on my computer for a while, I think it works well, but I don't want to lose the upstream repository updates, if I can I will organize the code and submit a PR, I hope the author can reconsider whether to adopt it. Please forgive me if I am disturbing you!

https://github.com/Wxh16144/ni/commit/017dbbb9390c28497eb93d4848ae0acb0781c892