如何使用 Lerna 管理 Monorepos

作者选择了 多样性在技术基金作为 写给捐款计划的一部分接受捐款。

介绍

Lerna是一个用于管理多个包的JavaScript项目的工具。

管理 Monorepos 可能具有挑战性,因为连续构建和发布单个包需要很长时间。Lerna 提供包启动、并行构建和文物出版等功能,可帮助您管理 monorepos。

在本教程中,您将安装Lerna,创建工作目录,初始化Lerna项目,创建monorepo,启动您的包,并为所有包添加依赖性。

前提条件

要完成本教程,您将需要:

步骤 1 – 安装 Lerna 和初始化项目

在此步骤中,您将安装 Lerna 并设置您的项目. 使用 Lerna,您可以在项目中管理共同的包,这在使用 monorepos 时非常有用。

开始使用以下命令来安装 Lerna:

1npm i -g lerna

npm i 将使用 npm 来安装 lerna 包. -g 旗帜意味着 lerna 命令通过终端在全球范围内可用。

<$>[注] 注: 如果您遇到权限错误,您可能需要使用管理员访问重新启动命令。

您现在可以将目录更改为您选择的位置,并创建一个样本工作目录来容纳Lerna项目。

运行以下命令来创建您的样本工作目录:

1mkdir lerna-demo
2cd lerna-demo

您现在可以在您的目录中运行init:

1lerna init

您将看到以下结果:

1[secondary_label Output]
2...
3lerna notice cli v4.0.0
4lerna info Initializing Git repository
5lerna info Creating package.json
6lerna info Creating lerna.json
7lerna info Creating packages directory
8lerna success Initialized Lerna files

init命令创建一个lerna.json文件. 这个文件可以自定义,虽然本教程将使用默认状态. 这个命令还初始化 git 存储库,并创建一个package.json文件和一个packages/目录。

在此步骤中,您安装了Lerna并初始化了您的项目,接下来,您将创建Monorepo。

步骤2 - 创建Monorepo

在此步骤中,您将创建与Lerna合作所需的_monorepo。Monorepo是一个包含一个项目(或多个项目)和多个包的存储库。

使用以下命令创建apple/文件夹在packages/目录下,然后运行npm init来设置目录:

1mkdir apple
2cd apple
3npm init

您将在苹果/目录中看到npm init的以下输出:

 1[secondary_label Output]
 2...
 3 npm init
 4This utility will walk you through creating a package.json file.
 5It only covers the most common items and tries to guess sensible defaults.
 6
 7See `npm help init` for definitive documentation on these fields
 8and exactly what they do.
 9
10Use `npm install <pkg>` afterward to install a package and
11save it as a dependency in the package.json file.
12
13Press ^C at any time to quit.
14package name: (apple) (ENTER)
15version: (1.0.0) (ENTER)
16description: (ENTER)
17entry point: (index.js) (ENTER)
18test command: (ENTER)
19git repository: (ENTER)
20keywords: (ENTER)
21author: (ENTER)
22license: (ISC) (ENTER)
23About to write to lerna-demo/packages/apple/package.json:
24
25{
26  "name": "apple",
27  "version": "1.0.0",
28  "description": "",
29  "main": "index.js",
30  "scripts": {
31    "test": "echo \"Error: no test specified\" && exit 1"
32  },
33  "author": "",
34  "license": "ISC"
35}
36
37Is this OK? (yes) y

创建和更新苹果文件夹后,按照相同的过程创建橙色香蕉文件夹。

现在你已经用npm init步骤初始化了npm包,你可以运行bootstrap命令,在所有包中运行npm install。Lerna的bootstrap命令会安装包依赖性并将这些包连接在一起。

运行以下命令,从项目根文件夹中启动 npm 包:

1lerna bootstrap

您将看到以下输出:

1[secondary_label Output]
2...
3❯ lerna bootstrap
4lerna notice cli v4.0.0
5lerna info Bootstrapping 3 packages
6lerna info Symlinking packages and binaries
7lerna success Bootstrapped 3 packages

在此步骤中,您将设置三个文件夹的 monorepo,并将您的包启动到目录中。

步骤 3 — 在 Monorepo 中安装包裹

在此步骤中,您将使用 Lerna 在您的 monorepo 中安装样本包。

若要将包添加到您的项目中,请使用lerna exec命令执行壳命令:

1lerna exec npm i lite-server --parallel

您正在使用 lite-server作为一个例子,因为它是一个轻量级的包,您可能会考虑使用这种方法添加其他包,这取决于您的项目要求。

- 平行旗表示构建要同时运行,从而在编译过程中节省时间。

预计产量将是这样的:

 1[secondary_label Output]
 2...
 3❯ lerna exec npm i lite-server --parallel
 4lerna notice cli v4.0.0
 5lerna info Executing command in 3 packages: "npm i lite-server"
 6apple: added 179 packages, and audited 180 packages in 12s
 7apple: 6 packages are looking for funding
 8apple:   run `npm fund` for details
 9apple: found 0 vulnerabilities
10orange: added 179 packages, and audited 180 packages in 12s
11orange: 6 packages are looking for funding
12orange:   run `npm fund` for details
13orange: found 0 vulnerabilities
14banana: added 179 packages, and audited 180 packages in 12s
15banana: 6 packages are looking for funding
16banana:   run `npm fund` for details
17banana: found 0 vulnerabilities
18lerna success exec Executed command in 3 packages: "npm i lite-server"

输出日志末尾的lerna 成功消息表示该命令成功运行,并且您已在每个项目文件夹中安装了lite-server包。

在此步骤中,您已在所有包中安装了依赖性,接下来,您将在您的包中运行脚本。

步骤4 - 运行脚本

在此步骤中,您将运行为您的软件包定制构建的脚本。您可以使用「lerna run 」在所有软件包中执行「npm run 」。

例如,运行附带您在步骤 2 中添加的包的测试脚本:

1lerna run test --no-bail

通过一个-no-bail旗告诉Lerna运行所有包的脚本,即使某个特定包的脚本有一个错误。

您将看到以下结果:

 1[secondary_label Output]
 2...
 3
 4lerna notice cli v4.0.0
 5lerna info Executing command in 3 packages: "npm run test"
 6lerna info run Ran npm script 'test' in 'apple' in 0.2s:
 7
 8> [email protected] test
 9> echo "Error: no test specified" && exit 1
10
11Error: no test specified
12lerna info run Ran npm script 'test' in 'banana' in 0.2s:
13
14> [email protected] test
15> echo "Error: no test specified" && exit 1
16
17Error: no test specified
18lerna info run Ran npm script 'test' in 'orange' in 0.2s:
19
20> [email protected] test
21> echo "Error: no test specified" && exit 1
22
23Error: no test specified
24lerna ERR! Received non-zero exit code 1 during execution
25lerna success run Ran npm script 'test' in 3 packages in 0.2s:
26lerna success - apple
27lerna success - banana
28lerna success - orange

使用此命令,您为您的包运行了测试脚本. 由于您没有定义任何测试,您将收到错误:没有测试指定的的默认输出。 您确实收到退出代码1的通知,但通过无保释的旗帜意味着您的脚本没有问题。

结论

在本文中,您了解了如何使用 Lerna 管理 monorepos. 您现在可以使用 Lerna 来自动化需要在所有包中进行类似更改的任务. 您还通过了特殊的旗帜,例如 --no-bail--parallel 来定制您的构建。 有关更多信息,请访问官方 Lerna 文档页面

要使您的包在全球范围内可用,您可以将您的包发布到您选择的文物库。 npmjs是一个公共文物库,您可以 推送您的包

Published At
Categories with 技术
comments powered by Disqus