Skip to main content

实用工具

¥Utilities

Lerna 提供了一些实用函数,可用于在 Lerna monorepo 中创建你自己的工具。

¥Lerna ships some utility functions that can be used in creating your own tools within a Lerna monorepo.

const utils = require("lerna/utils");

detectProjects()

detectProjects() 函数创建 Lerna 在幕后使用的相同项目图形文件映射来执行其命令。这对于编写你自己的脚本非常有用,这些脚本需要与 Lerna 操作同一组包。

¥The detectProjects() function creates the same project graph file mapping that Lerna uses under the covers to execute its commands. This is useful for writing your own scripts that need to operate on the same set of packages that Lerna would.

const { detectProjects } = require("lerna/utils");

const { projectGraph, projectFileMap } = await detectProjects();

返回的 projectGraph 将是 ProjectGraphWithPackages,它是 @nx/devkitProjectGraph 类型的扩展。它包含有关具有 package.json 文件的项目的附加元数据。它还具有 localPackageDependencies 属性,用于跟踪项目之间的内部 npm 依赖(而不是从注册表下载的外部 npm 依赖)。

¥The projectGraph that is returned will be a ProjectGraphWithPackages, which is an extension of the ProjectGraph type from @nx/devkit. It contains additional metadata about projects that have package.json files. It also has a localPackageDependencies property that tracks internal npm dependencies between projects (as opposed to external npm dependencies that are downloaded from the registry).

projectFileMap 是项目名称到其中文件的映射。这用于确定当文件更改时需要对哪个项目进行版本控制。

¥The projectFileMap is a mapping of project names to the files within them. This is used to determine which project needs to be versioned when a file changes.

具体类型详情请参见 Lerna 的 TypeScript 源代码

¥See Lerna's TypeScript source code for specific type details.