Skip to main content

运行任务

¥Run Tasks

大仓可以拥有数百甚至数千个项目,因此能够针对所有(或部分)项目运行 npm 脚本是 Lerna 这样的工具的一个关键功能。

¥Monorepos can have hundreds or even thousands of projects, so being able to run npm scripts against all (or some) of them is a key feature of a tool like Lerna.

定义

¥Definitions

  • 命令 - 开发者在终端中输入的任何内容(例如,lerna run build --scope=header --concurrency=5)。

    ¥Command - anything the developer types into the terminal (e.g., lerna run build --scope=header --concurrency=5).

  • 目标 - npm 脚本的名称(例如 build

    ¥Target - the name of an npm script (e.g., build)

  • 任务 - npm 脚本的调用(例如 header:build)。

    ¥Task - an invocation of an npm script (e.g., header:build).

示例存储库

¥Example Repository

示例基于 这个存储库,因此请随意克隆它并继续操作。

¥Examples are based on this repository, so feel free to clone it and follow along.

运行所有

¥Run Everything

每个项目都定义了 testbuild 脚本。

¥Each project has the test and build scripts defined.

运行:

¥Run:

npx lerna run build

这将以正确的顺序构建项目:footerheader,然后是 remixapp

¥This will build the projects in the right order: footer and header and then remixapp.

Terminal Output
    ✔  header:build (501ms)
✔ footer:build (503ms)
✔ remixapp:build (670ms)

—————————————————————————————————————————————————————————————————————————————

Lerna (powered by Nx) Successfully ran target build for 3 projects (1s)

请注意,Lerna 并不关心每个构建脚本的作用。build 这个名字也并不特殊:它只是 npm 脚本的名称。

¥Note that Lerna doesn't care what each of the build scripts does. The name build is also not special: it's simply the name of the npm script.

同时运行多个任务

¥Run a Multiple Tasks concurrently

你可以传递希望触发同时运行的目标的逗号分隔列表。

¥You can pass a comma-delimited list of targets you wish to trigger to run concurrently.

npx lerna run test,build,lint

例如,如果你的任务之间存在依赖,例如对于特定包,build 需要在 test 之前运行,只要你配置了适当的 任务管道配置,任务运行程序就会为你进行协调。

¥If, for example, there are dependencies between your tasks, such as build needing to run before test for particular packages, the task-runner will coordinate that for you as long as you have configured an appropriate Task Pipeline Configuration.

为单个包运行任务

¥Run a Task for a single Package

在开发过程中,你很少运行所有构建或所有测试。相反,你通常只针对你正在更改的项目运行操作。例如,你可以像这样运行 header 测试:

¥While developing you rarely run all the builds or all the tests. Instead, you often run things only against the projects you are changing. For instance, you can run the header tests like this:

npx lerna run test --scope=header

运行受 PR 影响的任务

¥Run Tasks Affected by a PR

你还可以为 PR 中受影响的所有项目运行命令,如下所示:

¥You can also run a command for all the projects affected in your PR like this:

npx lerna run test --since=origin/main

了解更多 此处

¥Learn more here.

控制任务的运行方式

¥Control How Tasks Run

要更好地控制任务的执行顺序,请编辑 任务管道配置

¥For more control over the order tasks are executed, edit the Task Pipeline Configuration.

要加快任务执行速度,请了解如何 缓存任务结果分布式任务执行

¥To speed up your task execution, learn how to Cache Task Results and Distribute Task Execution

自动加载 .env 文件

¥Automatic loading of .env files

默认情况下,由 Nx 提供支持的现代任务运行程序将自动为你加载 .env 文件。如果你出于任何原因想要禁用此行为,你可以将 --load-env-files 设置为 false。

¥By default the modern task runner powered by Nx will automatically load .env files for you. You can set --load-env-files to false if you want to disable this behavior for any reason.

有关默认情况下将加载哪些 .env 文件的更多详细信息,请参阅:https://nx.dev/recipes/environment-variables/define-environment-variables

¥For more details about what .env files will be loaded by default please see: https://nx.dev/recipes/environment-variables/define-environment-variables