nx-run-tasks

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese
You can run tasks with Nx in the following way.
Keep in mind that you might have to prefix things with npx/pnpx/yarn if the user doesn't have nx installed globally. Look at the package.json or lockfile to determine which package manager is in use.
For more details on any command, run it with
--help
(e.g.
nx run-many --help
,
nx affected --help
).
你可以通过以下方式使用Nx运行任务。
请注意,如果用户未全局安装Nx,可能需要在命令前添加npx/pnpx/yarn前缀。可以查看package.json或锁文件来确定当前使用的包管理器。
如需了解任何命令的详细信息,可在命令后添加
--help
参数运行(例如
nx run-many --help
nx affected --help
)。

Understand which tasks can be run

了解可运行的任务

You can check those via
nx show project <projectname> --json
, for example
nx show project myapp --json
. It contains a
targets
section which has information about targets that can be run. You can also just look at the
package.json
scripts or
project.json
targets, but you might miss out on inferred tasks by Nx plugins.
你可以通过
nx show project <projectname> --json
命令查看可运行的任务,例如
nx show project myapp --json
。该命令的输出包含
targets
部分,其中列出了所有可运行的目标任务。你也可以直接查看
package.json
中的脚本或
project.json
中的目标,但这样可能会遗漏Nx插件自动推断出的任务。

Run a single task

运行单个任务

nx run <project>:<task>
where
project
is the project name defined in
package.json
or
project.json
(if present).
nx run <project>:<task>
其中
project
package.json
project.json
(如果存在)中定义的项目名称。

Run multiple tasks

运行多个任务

nx run-many -t build test lint typecheck
You can pass a
-p
flag to filter to specific projects, otherwise it runs on all projects. You can also use
--exclude
to exclude projects, and
--parallel
to control the number of parallel processes (default is 3).
Examples:
  • nx run-many -t test -p proj1 proj2
    — test specific projects
  • nx run-many -t test --projects=*-app --exclude=excluded-app
    — test projects matching a pattern
  • nx run-many -t test --projects=tag:api-*
    — test projects by tag
nx run-many -t build test lint typecheck
你可以通过
-p
参数筛选特定项目,否则会在所有项目上运行任务。你也可以使用
--exclude
参数排除某些项目,使用
--parallel
参数控制并行进程的数量(默认值为3)。
示例:
  • nx run-many -t test -p proj1 proj2
    —— 测试特定项目
  • nx run-many -t test --projects=*-app --exclude=excluded-app
    —— 测试匹配指定模式的项目
  • nx run-many -t test --projects=tag:api-*
    —— 测试带有指定标签的项目

Run tasks for affected projects

为受影响的项目运行任务

Use
nx affected
to only run tasks on projects that have been changed and projects that depend on changed projects. This is especially useful in CI and for large workspaces.
nx affected -t build test lint
By default it compares against the base branch. You can customize this:
  • nx affected -t test --base=main --head=HEAD
    — compare against a specific base and head
  • nx affected -t test --files=libs/mylib/src/index.ts
    — specify changed files directly
使用
nx affected
仅在已变更的项目及其依赖项目上运行任务。这在CI环境和大型工作区中尤其有用。
nx affected -t build test lint
默认情况下,该命令会与基准分支进行对比。你可以自定义对比规则:
  • nx affected -t test --base=main --head=HEAD
    —— 与指定的基准分支和当前分支进行对比
  • nx affected -t test --files=libs/mylib/src/index.ts
    —— 直接指定已变更的文件

Useful flags

实用参数

These flags work with
run
,
run-many
, and
affected
:
  • --skipNxCache
    — rerun tasks even when results are cached
  • --verbose
    — print additional information such as stack traces
  • --nxBail
    — stop execution after the first failed task
  • --configuration=<name>
    — use a specific configuration (e.g.
    production
    )
以下参数适用于
run
run-many
affected
命令:
  • --skipNxCache
    —— 即使结果已缓存,仍重新运行任务
  • --verbose
    —— 打印额外信息,如堆栈跟踪
  • --nxBail
    —— 第一个任务失败后立即停止执行
  • --configuration=<name>
    —— 使用特定配置(例如
    production