过滤
过滤允许你将命令限制为包的特定子集。
🌐 Filtering allows you to restrict commands to specific subsets of packages.
pnpm 支持丰富的选择器语法,可按名称或关系选择软件包。
🌐 pnpm supports a rich selector syntax for picking packages by name or by relation.
选择器可以通过 --filter(或 -F)标志指定:
🌐 Selectors may be specified via the --filter (or -F) flag:
pnpm --filter <package_selector> <command>
匹配
🌐 Matching
--filter <package_name>
要选择一个具体的软件包,只需指定其名称(@scope/pkg)或使用模式选择一组软件包(@scope/*)。
🌐 To select an exact package, just specify its name (@scope/pkg) or use a
pattern to select a set of packages (@scope/*).
示例:
🌐 Examples:
pnpm --filter "@babel/core" test
pnpm --filter "@babel/*" test
pnpm --filter "*core" test
指定包的作用域是可选的,因此如果找不到 core,--filter=core 将选择 @babel/core。 但是,如果工作区中有多个同名包(例如 @babel/core 和 @types/core),那么不使用作用域进行筛选将不会选择任何内容。
🌐 Specifying the scope of the package is optional, so --filter=core will pick @babel/core if core is not found.
However, if the workspace has multiple packages with the same name (for instance, @babel/core and @types/core),
then filtering without scope will pick nothing.
--filter <package_name>...
要选择一个软件包及其依赖(直接和间接依赖),请在软件包名称后加上省略号:<package_name>...。例如,下面的命令将运行 foo 及其所有依赖的测试:
🌐 To select a package and its dependencies (direct and non-direct), suffix the
package name with an ellipsis: <package_name>.... For instance, the next
command will run tests of foo and all of its dependencies:
pnpm --filter foo... test
你可以使用模式来选择一组根包:
🌐 You may use a pattern to select a set of root packages:
pnpm --filter "@babel/preset-*..." test
--filter <package_name>^...
要仅选择一个软件包的依赖(包括直接和间接依赖),在名称后加上前面提到的省略号,并在其前加上尖括号。例如,下面的命令将对 foo 的所有依赖运行测试:
🌐 To ONLY select the dependencies of a package (both direct and non-direct),
suffix the name with the aforementioned ellipsis preceded by a chevron. For
instance, the next command will run tests for all of foo's
dependencies:
pnpm --filter "foo^..." test
--filter ...<package_name>
要选择一个软件包及其依赖的软件包(直接依赖和间接依赖),在软件包名称前加上省略号:...<package_name>。例如,这将运行 foo 及其所有依赖包的测试:
🌐 To select a package and its dependent packages (direct and non-direct), prefix
the package name with an ellipsis: ...<package_name>. For instance, this will
run the tests of foo and all packages dependent on it:
pnpm --filter ...foo test
--filter "...^<package_name>"
要仅选择某个软件包的依赖包(包括直接依赖和间接依赖),请在软件包名称前加上省略号和尖括号。例如,这将运行依赖于 foo 的所有软件包的测试:
🌐 To ONLY select a package's dependents (both direct and non-direct), prefix the
package name with an ellipsis followed by a chevron. For instance, this will
run tests for all packages dependent on foo:
pnpm --filter "...^foo" test
--filter ./<glob>, --filter {<glob>}
相对于当前工作目录匹配项目的通配符模式。
🌐 A glob pattern relative to the current working directory matching projects.
pnpm --filter "./packages/**" <cmd>
包括指定目录下的所有项目。
🌐 Includes all projects that are under the specified directory.
它也可以与省略号和尖括号操作符一起使用,以选择从属项/依赖:
🌐 It may be used with the ellipsis and chevron operators to select dependents/dependencies as well:
pnpm --filter ...{<directory>} <cmd>
pnpm --filter {<directory>}... <cmd>
pnpm --filter ...{<directory>}... <cmd>
它也可以与 [<since>] 结合使用。例如,要选择目录中所有已更改的项目:
pnpm --filter "{packages/**}[origin/master]" <cmd>
pnpm --filter "...{packages/**}[origin/master]" <cmd>
pnpm --filter "{packages/**}[origin/master]..." <cmd>
pnpm --filter "...{packages/**}[origin/master]..." <cmd>
或者你可以从目录中选择所有名称与给定模式匹配的包:
🌐 Or you may select all packages from a directory with names matching the given pattern:
pnpm --filter "@babel/*{components/**}" <cmd>
pnpm --filter "@babel/*{components/**}[origin/master]" <cmd>
pnpm --filter "...@babel/*{components/**}[origin/master]" <cmd>
--filter "[<since>]"
选择自指定提交/分支以来更改的所有包。可以在前或后加上 ... 以包含依赖/被依赖。
🌐 Selects all the packages changed since the specified commit/branch. May be
suffixed or prefixed with ... to include dependencies/dependents.
例如,下面的命令将运行自 master 以来所有已更改包以及任何依赖包中的测试:
🌐 For example, the next command will run tests in all changed packages since
master and on any dependent packages:
pnpm --filter "...[origin/master]" test
--fail-if-no-match
如果你希望 CLI 在没有包与过滤器匹配的情况下失败,请使用此标志。
🌐 Use this flag if you want the CLI to fail if no packages have matched the filters.
你也可以通过 [failIfNoMatch 设置] 永久设置它。
🌐 You may also set this permanently with a failIfNoMatch setting.
排除
🌐 Excluding
当过滤器选择器前面有“!”时,它们可以作为排除运算符。在 zsh(以及可能的其他 shell)中,“!” 应该被转义:\!。
🌐 Any of the filter selectors may work as exclusion operators when they have a
leading "!". In zsh (and possibly other shells), "!" should be escaped: \!.
例如,这将在除 foo 之外的所有项目中运行命令:
🌐 For instance, this will run a command in all projects except for foo:
pnpm --filter=!foo <cmd>
这将在所有不在 lib 目录下的项目中运行命令:
🌐 And this will run a command in all projects that are not under the lib
directory:
pnpm --filter=!./lib <cmd>
多重性
🌐 Multiplicity
当包被过滤时,每个至少匹配一个选择器的包都会被选取。你可以使用任意数量的过滤器:
🌐 When packages are filtered, every package is taken that matches at least one of the selectors. You can use as many filters as you want:
pnpm --filter ...foo --filter bar --filter baz... test
--filter-prod <filtering_pattern>
行为与 --filter 相同,但在从工作区选择依赖目时会省略 devDependencies。
🌐 Acts the same a --filter but omits devDependencies when selecting dependency projects
from the workspace.
--test-pattern <glob>
test-pattern 允许检测修改的文件是否与测试相关。如果是,这些已修改包的依赖包将不被包括在内。
此选项在“自上次更改以来”过滤器中非常有用。例如,下面的命令将对所有已更改的包运行测试,如果更改出现在包的源代码中,依赖包中的测试也会运行:
🌐 This option is useful with the "changed since" filter. For instance, the next command will run tests in all changed packages, and if the changes are in the source code of the package, tests will run in the dependent packages as well:
pnpm --filter="...[origin/master]" --test-pattern="test/*" test
--changed-files-ignore-pattern <glob>
允许在过滤自指定提交/分支以来更改的项目时按 glob 模式忽略更改的文件。
🌐 Allows to ignore changed files by glob patterns when filtering for changed projects since the specified commit/branch.
使用示例:
🌐 Usage example:
pnpm --filter="...[origin/master]" --changed-files-ignore-pattern="**/README.md" run build