Skip to main content

入门

¥Getting Started

Lerna 附带了专用的 init 命令,可帮助你将 lerna 添加到现有存储库,或从头开始创建一个。

¥Lerna comes with a dedicated init command to assist you with both adding lerna to an existing repo, or creating one from scratch.

从头开始

¥Starting from scratch

在最简单的情况下,lerna init 可用于在空目录中创建新存储库。为此,我们可以运行以下命令:

¥In the simplest case, lerna init can be used to create a new repository in an empty directory. For that, we can run the following commands:

# Create an empty directory
mkdir ./new-lerna-workspace
# Change into the new directory
cd ./new-lerna-workspace
# Initialize lerna (using --dryRun to preview the changes)
npx lerna init --dryRun

请注意,我们在这里传递了 --dryRun 标志,这使我们能够预览 lerna init 对我们的文件系统所做的更改。这使我们能够调整传递给 lerna init 的任何其他参数的值(例如 --exact--independent),而不必担心消除任何错误。

¥Note that we have passed the --dryRun flag here, this allows us to see a preview of the changes that lerna init will make to our file system. This allows us to tweak the values of any other arguments we pass to lerna init (such as --exact or --independent) without having to worry about undoing any mistakes.

一旦我们对其所做的更改感到满意,我们就可以简单地重复 npx lerna init 命令,但保留 --dryRun 标志。

¥Once we are happy with the changes it will make, we can simply repeat the npx lerna init command but leave off the --dryRun flag.

现在,你将启动并运行一个工作 git 存储库,包括 npm 工作区,并且 lerna 可用于创建、版本控制和发布你想要开发的任何包。

¥You will now be up and running with a working git repository, including npm workspaces, with lerna available to create, version and publish any packages you wish to develop.

将 lerna 添加到现有存储库

¥Adding lerna to an existing repo

如果你已经有一个现有的存储库,你仍然可以使用 lerna initlerna 添加到其中。

¥If you already have an existing repo, you can still add lerna to it using lerna init.

信息

Lerna 不负责在存储库中安装和链接依赖,你的包管理器更适合该任务。

¥Lerna is not responsible for installing and linking your dependencies in your repo, your package manager is much better suited to that task.

相反,我们强烈建议配置你选择的包管理器以使用其 workspaces 功能:

¥Instead, we strongly recommend configuring your package manager of choice to use its workspaces feature:

当在现有存储库上初始化 lerna 时,它将需要一种方法来知道它应该在哪些包上运行。如果你正在使用包管理器的 workspaces 功能(请参阅上面的注释),那么 lerna 将默认使用你已经配置的 workspaces 模式。不需要额外的参数。

¥When initializing lerna on an existing repo, it will need a way to know what packages it should operate on. If you are using your package manager's workspaces feature (see note above), then lerna will default to using the workspaces patterns you have already configured. No extra arguments are required.

或者,你可以通过使用 lerna init--packages 标志来手动指定一组要匹配的模式:

¥Alternatively, you can manually specify a set of patterns to match against instead by using the --packages flag for lerna init:

# Passing a single pattern
npx lerna init --packages="packages/*"
# Passing multiple patterns
npx lerna init --packages="foo/*" --packages="bar/*"