自动安装程序
一个 Monorepo 通常需要安装提供工具(例如 shell 命令)的 NPM 包。在大多数情况下,这些工具依赖项可以声明为某个 Rush 项目的 devDependencies
,然后它们将由 rush install
使用其集中式 shrinkwrap 文件(包管理器锁文件)安装。但是,有时在 rush install
未被调用或 rush install
可能会失败的情况下,需要这些依赖项,如果某人的未完成工作包含一些 package.json 修改。对于这些情况,Rush 的 自动安装程序 功能提供了一种隔离机制来安装工具依赖项。
自动安装程序被定义为 common/autoinstallers/ 下的文件夹,其中包含一个 package.json 文件及其自己的私有 shrinkwrap 文件。此文件夹已添加到 Git,但它不是普通的 Rush 项目:它不会被 rush install
安装,也不包含任何可构建的源代码供 rush build
使用。自动安装程序纯粹是用于安装 NPM 依赖项的容器。自动安装程序可以与 Rush 功能相关联,例如 自定义命令 或 Rush 插件;当调用关联的功能时,Rush 将自动安装依赖项。
何时使用自动安装程序
如果要启用 Rush 插件,则必须配置自动安装程序。
如果你要创建一个 Rush 自定义命令,其脚本需要 NPM 依赖项,则有几种可能的做法可供考虑
rush install:通常,Rush Monorepo 中的大多数依赖项将通过
rush install
使用集中式 shrinkwrap 文件一起安装。对于大多数需求,这是最简单的方法,也是最容易维护的方法。如果某些依赖项与特定任务无关,则可以使用 项目选择参数(例如rush install --to example-project
)跳过安装它们。install-run.js:install-run.js 脚本使你能够在
rush install
之外安装 NPM 包。这对于在完全未调用rush install
的上下文中运行的命令或rush install
可能已损坏的命令非常有用。例如,Git 提交钩子脚本会在rush install
可能失败的分支上运行:开发人员经常提交正在进行的工作,或者 Git 变基可能会引入由后续提交修复的损坏提交。自动安装程序:install-run.js 的限制是它只安装一个 NPM 包。例如,如果你的自定义命令需要多个包(例如
pretty-quick
驱动程序、prettier
引擎和一些 Prettier 插件),你可以将它们添加到自动安装程序的 package.json 文件中。自动安装程序通常具有较小的依赖项树,因此安装速度比rush install
快得多。自动安装程序的一些潜在缺点:在需要多个自动安装程序和/或
rush install
的情况下,包管理器将被调用多次,可能需要从不同的 shrinkwrap 文件中安装相同的依赖项。这可能比rush install
使用集中式 shrinkwrap 文件一起安装所有内容要慢得多。此外,自动安装程序不会由rush update
验证或更新,因此它们需要额外的维护才能进行升级。
创建自动安装程序
使用 rush init-autoinstaller 命令创建文件夹
# This creates the common/autoinstallers/my-autoinstaller/package.json file
rush init-autoinstaller --name my-autoinstaller编辑 my-autoinstaller/package.json 文件以添加你的依赖项。
运行 rush update-autoinstaller 以更新 shrinkwrap 文件。在你修改 package.json 文件后,应重新执行此步骤。
# Create or update common/autoinstallers/my-autoinstaller/pnpm-lock.yaml
# This file should be committed and tracked by Git.
rush update-autoinstaller --name my-autoinstaller将更新的文件提交到 git
git add common/autoinstallers/my-autoinstaller/
git commit -m "Updated autoinstaller"
要将自动安装程序与自定义命令相关联,请在 command-line.json 中的 autoinstallerName
字段中指定其名称。
要将自动安装程序与 Rush 插件相关联,请参阅 创建 Rush 插件 文档。
维护自动安装程序
要修改自动安装程序,请编辑其 package.json 文件。
# This will also upgrade any indirect dependencies.
rush update-autoinstaller --name my-autoinstaller
# Commit the updated pnpm-lock.yaml
git commit -m "Updated autoinstaller"要删除自动安装程序,只需删除其文件夹即可
# BE CAREFUL WHEN RECURSIVELY DELETING FOLDERS
rm -Rf common/autoinstallers/my-autoinstaller
# Commit the changes to Git
git add common/autoinstallers
git commit -m "Deleted autoinstaller"