Skip to main content

工作区监视

¥Workspace Watching

注意

从 Lerna 6.4.0 开始,工作区监视功能可用。

¥Workspace Watching is available as of Lerna 6.4.0.

Lerna 可以监视包内的文件更改,并自动从存储库的根目录执行命令。如果你在开发工作流程中更新文件时需要重建包或重新运行测试,这非常有用。

¥Lerna can watch for file changes within packages and automatically execute commands from the root of the repository. This is useful if you need to rebuild packages or rerun tests as you update files during your development workflow.

这取代了手动设置单独监视每个包的需要。

¥This replaces the need to manually set up watching for each package individually.

示例

¥Examples

监视所有包并回显包名称和更改的文件:

¥Watch all packages and echo the package name and the files that changed:

$ lerna watch -- echo \$LERNA_PACKAGE_NAME \$LERNA_FILE_CHANGES

监视所有包并在包中的文件发生更改时在包上运行 "build" 脚本:

¥Watch all packages and run the "build" script on a package when a file within it changes:

$ lerna watch -- lerna run build --scope=\$LERNA_PACKAGE_NAME

监视所有包并对受更改影响的所有内容运行 "build" 脚本:

¥Watch all packages and run the "build" script on everything affected by the changes:

$ lerna watch -- lerna run build --since

监视单个包并在其中的文件发生更改时对其运行 "build" 脚本:

¥Watch a single package and run the "build" script on it when a file within it changes:

$ lerna watch --scope="my-package-1" -- lerna run build --scope=\$LERNA_PACKAGE_NAME

观察单个包及其依赖,对其中任何一个发生更改的包运行 "build" 脚本:

¥Watch a single package and its dependencies, running the "build" script on any of them that change:

$ lerna watch --scope="my-package-1" --include-dependencies -- lerna run build --scope=\$LERNA_PACKAGE_NAME

监视所有包并为已更改的包以及依赖它的所有包运行 build 脚本:

¥Watch all packages and run the build script for the package that changed and all packages that depend on it:

$ lerna watch -- lerna run build --scope=\$LERNA_PACKAGE_NAME --include-dependents

有关更高级的过滤,请参阅 过滤选项 文档。有关更多可用选项,请参阅 lerna watch 文档。

¥For more advanced filtering, see the filter options documentation. For more available options, see the lerna watch documentation.

观察环境变量

¥Watch Environment Variables

Lerna 在运行内部命令时会设置环境变量 $LERNA_PACKAGE_NAME$LERNA_FILE_CHANGES。这些可用于自定义运行的命令。

¥Lerna will set the environment variables $LERNA_PACKAGE_NAME and $LERNA_FILE_CHANGES when running the inner command. These can be used to customize the command that is run.

  • $LERNA_PACKAGE_NAME 将替换为已更改的包的名称。

    ¥$LERNA_PACKAGE_NAME will be replaced with the name of the package that changed.

  • $LERNA_FILE_CHANGES 将被更改的文件替换。如果在一个周期内检测到多个文件更改,则 $LERNA_FILE_CHANGES 将全部列出,并以空格分隔。

    ¥$LERNA_FILE_CHANGES will be replaced with the file(s) that changed. If multiple file changes are detected in one cycle, then $LERNA_FILE_CHANGES will list them all, separated by spaces.

注意

使用 $LERNA_PACKAGE_NAME$LERNA_FILE_CHANGES 时,你需要使用反斜杠 (\) 对 $ 进行转义。参见上面的 examples

¥When using $LERNA_PACKAGE_NAME and $LERNA_FILE_CHANGES, you will need to escape the $ with a backslash (\). See the examples above.

使用包管理器运行

¥Running With Package Managers

上面的示例展示了直接在终端中使用 lerna。但是,你也可以通过包管理器使用 lerna,而不将其添加到你的路径中:

¥The examples above showcase using lerna directly in the terminal. However, you can also use lerna via a package manager without adding it to your path:

下午:

¥pnpm:

pnpm lerna watch -- lerna run build --scope=\$LERNA_PACKAGE_NAME

yarn:

yarn lerna -- watch -- lerna run build --scope=\$LERNA_PACKAGE_NAME

npx:

npx -c 'lerna watch -- lerna run build --scope=\$LERNA_PACKAGE_NAME'
注意

使用 npx 时,你需要使用 -c 并将整个 lerna watch 命令用单引号 (') 括起来。如果没有这个,npx 将在将命令传递给 lerna 之前尝试替换 观察环境变量,从而导致 $LERNA_PACKAGE_NAME$LERNA_FILE_CHANGES 始终为空值。

¥When using npx, you will need to use -c and surround the entire lerna watch command in single quotes ('). Without this, npx will try to replace the watch environment variables before passing the command to lerna, resulting in an always empty value for $LERNA_PACKAGE_NAME and $LERNA_FILE_CHANGES.

注意

如果你在 Windows 中使用 Lerna,则必须在 '%' 中构建环境变量。例如:

¥If you use Lerna in Windows, you must frame environment variables in '%'. For example:

$ lerna watch -- lerna run build --scope=%LERNA_PACKAGE_NAME% --include-dependents