what's the lerna ?
A tool for managing JavaScript projects with multiple packages.
管理多模块JS项目的工具
What can Lerna do?
The two primary commands in Lerna are lerna bootstrap and lerna publish.
bootstrap will link dependencies in the repo together. publish will help publish any updated packages. 最初的两个命令是lerna bootstrap和lerna publish
bootstrap会关联仓库内的依赖。publish会发布所有更新的包
practice
1.安装并初始化目录
// 全局安装
npm i lerna -g
// 创建实践目录
mkdir lerna-demo
cd lerna-demo
lerna init
初始化之后,会自动挂钩git,虽然还没有仓库
安装完毕之后,目录如下:
- packages(目录)
- lerna.json(配置文件)
- package.json(工程描述文件)
2.创建一个包
mkdir module-1
cd module-1
npm init -y
npm init -y命令可以跳过输入面板直接生成默认的package.json,日常项目中不要这样用。
这是现在的文件目录
➜ lerna-demo git:(master) ✗ tree
.
├── lerna.json
├── package.json
└── packages
└── module-1
└── package.json
2 directories, 3 files
3.给项目添加依赖
{
"name": "module-1",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
// 这是添加的依赖
"dependencies": {
"lodash": "^4.17.4"
}
}
安装依赖
lerna bootstrap
4.上传项目
你要现在git上新建一个仓库,然后按照指令链接即可
echo "# lerna-demo" >> README.md
git init
git add README.md
git commit -m "first commit"
git remote add origin https://github.com/daly-young/lerna-demo.git
git push -u origin master
仓库连接上了,我们开始发布吧
lerna publish
选择版本号
我选了major,然后朝下走
报错原因是根目录的package.json没有设置证书~所以我再重新设置下证书,每次加一个新包都会报这个问题,还是按照提示新增一个LICENSE.md文件即可。
npm set init-license ISC
然后再重新发布一次
没问题,发布成功
publish的大致过程是:
1.本地打个tag(例如git tag v1.0.0)
2.自动更新依赖项版本号 示例
3.然后把各个package发布到npm
4.最后把tag和相应的commit给push上去
以上是lerna的常规创建发布流程,默认模式是Fixed/Locked mode (固定模式)。
lerna mode
lerna提供Fixed/Locked mode(固定模式)和Independent mode(独立模式)
这两种模式的区别在哪呢?
Fixed/Locked mode
Fixed mode Lerna projects operate on a single version line. The version is kept in the lerna.json file at the root of your project under the version key. When you run lerna publish, if a module has been updated since the last time a release was made, it will be updated to the new version you're releasing. This means that you only publish a new version of a package when you need to.
This is the mode that Babel is currently using. Use this if you want to automatically tie all package versions together. One issue with this approach is that a major change in any package will result in all packages having a new major version.
固定模式的Lerna项目在单一版本线上运行。 该版本在根目录的lerna.json文件中进行版本管理。 当执行lerna publish时,如果自上次发布版本以来有模块更新,则它将更新为您要发布的新版本。 这意味着你只需在需要时发布新版本的软件包。
这是Babel目前使用的模式。 如果要自动将所有包版本绑定在一起,请使用此选项。 这种方法的一个问题是,任何包中的重大更改都将导致所有包都将升级新版本。
直接上代码运行试试吧~~
mkdir module-2
cd module-2
npm init
lerna bootstrap
虽然我只加了一个包,但是显示是关联了两个,这说明包括module-1的依赖包都重新关联了一次
然后我把这个包也发布上去了
接着,我修改了module-2
touch index.js
vim index.js
index.js
exports.demo = (param) => {
console.log('hello ' + param)
}
我还在依赖文件里新增了
"vuex": "^2.5.0"
这样的话,我算是有个重大修改,那么这个版本号是我手动修改的呢?还是默认修改的呢?
还让我手动修改就有点傻了~~所以我直接推送上去,然后lerna publish
果然,会让我再次选择版本号
选择之后,我们会发现
无辜的module-1也更新了,lerna.json也更新了了版本号
这就是固定模式,我们了解了,一个更新,全体更新,这样也不用担心包之间相互依赖,版本不协调问题。
但是,如果存在很多package,一次改动就要叠加下版本号,总感觉这操作有点凶残,所以我们看看第二种模式是不是有新的思路
Independent mode
这个模式初始化需要指定
lerna init --independent
Independent mode Lerna projects allows maintainers to increment package versions independently of each other. Each time you publish, you will get a prompt for each package that has changed to specify if it's a patch, minor, major or custom change.
Independent mode allows you to more specifically update versions for each package and makes sense for a group of components. Combining this mode with something like semantic-release would make it less painful. (There is work on this already at atlassian/lerna-semantic-release). 独立模式的Lerna项目允许维护人员相对独立地增加包版本。 每次发布时,你都会看到每个已更改的包的提示,以指定它是补丁,次要,主要还是自定义更改。
独立模式允许您更具体地更新每个包的版本,并且只对一组组件有意义。 将这种模式与语义释放(???)之类的相结合可以减少痛苦。 (在atlassian / lerna-semantic-release上已有相关工作)。
// 将lerna.json中的版本密钥设置为独立以在独立模式下运行。
Set the version key in lerna.json to independent to run in independent mode.
看起来这个是独立的,互不影响,我们先来试验一波~
重启一个lerna项目,这里不重复了,别忘了指定模式。 我们新建了module-1和module-2的包,并且已经发布成功了,OK,接下来我们修改module-2,依然是新增index.js,然后提发布
我改的是module-2,但是询问我module-1的修改?
然后询问module-2?是不是我操作有误?
不是,立马再去查文档,发现是自己理解有误,这个相对独立并不是说我改了一个包就只发这个一个包,而是我改了一个包,可以具体制定其他包的版本,而不是像固定模式一样,一个包升级大版本,所有包都升级大版本,这里可以对每个包独立定制版本。。原来是这个意思~~
那么到这,两种模式都实验完毕~这只是简单的操作,其他API(官方)大家可以再试试~~没事记得给人家官方star一下~