跳至主要内容
非官方测试版翻译

本页面由 PageTurner AI 翻译(测试版)。未经项目官方认可。 发现错误? 报告问题 →

命令

过滤选项

Lerna 命令可应用过滤选项来控制其操作的包范围。

适用于需要包过滤的 lerna 子命令

安装 lerna 以使用 lerna 命令行工具。

选项

--scope <glob>

仅包含名称匹配指定 glob 模式的包。

$ lerna exec --scope my-component -- ls -la
$ lerna run --scope "toolbar-*" test
$ lerna run --scope package-1 --scope "*-2" lint

注意: 对于某些 glob 模式,可能需要为参数添加引号以避免 shell 提前展开。

通过 npx 运行

使用 npx 运行 lerna 时,传递 glob 参数需显式添加 "=" 符号,防止 npx 提前展开参数。

例如:

$ npx lerna run --scope="toolbar-*" test
$ npx lerna run --scope="package-{1,2,5}" test

--ignore <glob>

排除名称匹配指定 glob 模式的包。

$ lerna exec --ignore "package-{1,2,5}"  -- ls -la
$ lerna run --ignore package-1 test
$ lerna run --ignore "package-@(1|2)" --ignore package-3 lint

更多过滤示例可查看此处

--no-private

排除私有包(默认包含私有包)。

--since [ref]

仅包含自指定 ref 后有变更的包。若未提供 ref,则默认使用最新标签。

# List the contents of packages that have changed since the latest tag
$ lerna exec --since -- ls -la

# Run the tests for all packages that have changed since `main`
$ lerna run test --since main

# List all packages that have changed since `some-branch`
$ lerna ls --since some-branch

这在 CI 环境中尤其有用——当您能获取 PR 将要合并的目标分支时,可直接将其作为 --since 选项的 ref 参数。此方法对合并到默认分支和功能分支的 PR 均适用。

--exclude-dependents

使用 --since 运行命令时排除所有传递被依赖项,覆盖默认的 "changed" 算法。

此标志必须配合 --since 使用,单独使用会抛出错误。

--include-dependents

运行命令时包含所有传递被依赖项,不受 --scope--ignore--since 限制。

--include-dependencies

运行命令时包含所有传递依赖项,不受 --scope--ignore--since 限制。

适用于所有支持 --scope 的命令(bootstrap, clean, ls, run, exec)。
确保作用域内包(通过 --scope--ignore 指定)的所有依赖项(含开发依赖)都会被处理。

注意:此标志将覆盖 --scope--ignore 标志。

例如:被 --ignore 标记的包若被其他启动中的包依赖,仍会被 bootstrapped。

此功能适用于需要"单独设置"依赖其他包的场景。

$ lerna bootstrap --scope my-component --include-dependencies
# my-component and all of its dependencies will be bootstrapped
$ lerna bootstrap --scope "package-*" --ignore "package-util-*" --include-dependencies
# all packages matching "package-util-*" will be ignored unless they are
# depended upon by a package whose name matches "package-*"

--include-merged-tags

$ lerna exec --since --include-merged-tags -- ls -la

配合 --since 使用时包含已合并分支的标签。仅适用于频繁从功能分支发布的情况(通常不推荐)。

限制

虽然可通过 pnpm dlxnpx 免安装直接运行 Lerna,但不推荐。命令可能执行成功,但输出结果未必完全准确。详见此问题