A tool for managing JavaScript projects with multiple packages.
管理多模塊JS項目的工具前端
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
會發布全部更新的包vue
// 全局安裝
npm i lerna -g
// 建立實踐目錄
mkdir lerna-demo
cd lerna-demo
lerna init
複製代碼
初始化以後,會自動掛鉤git,雖然尚未倉庫git
安裝完畢以後,目錄以下:github
- packages(目錄)
- lerna.json(配置文件)
- package.json(工程描述文件)
複製代碼
mkdir module-1
cd module-1
npm init -y
複製代碼
npm init -y
命令能夠跳過輸入面板直接生成默認的package.json
,平常項目中不要這樣用。vuex
這是如今的文件目錄npm
➜ lerna-demo git:(master) ✗ tree
.
├── lerna.json
├── package.json
└── packages
└── module-1
└── package.json
2 directories, 3 files
複製代碼
{
"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"
}
}
複製代碼
安裝依賴json
lerna bootstrap
複製代碼
你要如今git上新建一個倉庫,而後按照指令連接便可bootstrap
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
複製代碼
倉庫鏈接上了,咱們開始發佈吧vim
lerna publish
複製代碼
選擇版本號 bash
我選了major
,而後朝下走
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提供Fixed/Locked mode
(固定模式)和Independent 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,一次改動就要疊加下版本號,總感受這操做有點兇殘,因此咱們看看第二種模式是否是有新的思路
這個模式初始化須要指定
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一下~