lerna
概要
lerna是GitHub上面開源的一款js代碼庫管理軟件委乌, 用來對一系列相互耦合比較大皮迟、又相互獨立的js git庫進行管理败京。解決各個庫之間修改混亂、難以跟蹤的問題涩哟。lerna可以優(yōu)化這種情形下的工作流。
對于一些功能比較全的庫盼玄,我們往往會把各個小功能拆分成獨立的npm庫贴彼,他們直接有比較強的依賴關系。比如:Babel埃儿、React等開源代碼都是按照這種方式進行處理的器仗。
代碼庫結(jié)構
lerna管理的庫文件結(jié)構類似如下這樣
my-lerna-repo/
package.json
packages/
package-1/
package.json
package-2/
package.json
lerna主要做了什么
- 通過lerna的命令
lerna bootstrap
將會把代碼庫進行l(wèi)ink。 - 通過
lerna publish
發(fā)布最新改動的庫
Get Started
下面使用3.x版本的lerna進行測試童番, 官方也建議使用3.x代替2.x
首先精钮,初始化lerna庫
cd lerna-repo
lerna init
默認會創(chuàng)建lerna.json和packages目錄, 如下
lerna-repo/
packages/
package.json
lerna.json
lerna如何工作的
默認lerna有兩種管理模式, 固定模式和獨立模式
固定模式
固定模式剃斧,通過lerna.json的版本進行版本管理轨香。當你執(zhí)行lerna publish
命令時, 如果距離上次發(fā)布只修改了一個模塊幼东,將會更新對應模塊的版本到新的版本號臂容,然后你可以只發(fā)布修改的庫。
這種模式也是Babel使用的方式筋粗。如果你希望所有的版本一起變更策橘, 可以更新minor版本號,這樣會導致所有的模塊都更新版本娜亿。
獨立模式
獨立模式丽已,init的時候需要設置選項 --independent
. 獨立模式允許管理者對每個庫單獨改變版本號,每次發(fā)布的時候买决,你需要為每個改動的庫指定版本號沛婴。這種情況下, lerna.json
的版本號不會變化了督赤, 默認為independent
嘁灯。
lerna.json解析
{
"version": "1.1.3",
"npmClient": "npm",
"command": {
"publish": {
"ignoreChanges": [
"ignored-file",
"*.md"
]
},
"bootstrap": {
"ignore": "component-*",
"npmClientArgs": ["--no-package-lock"]
}
},
"packages": ["packages/*"]
}
- version , 當前庫的版本
- npmClient , 允許指定命令使用的client, 默認是 npm躲舌, 可以設置成 yarn
- command.publish.ignoreChanges 丑婿, 可以指定那些目錄或者文件的變更不會被publish
- command.bootstrap.ignore , 指定不受 bootstrap 命令影響的包
- command.bootstrap.npmClientArgs , 指定默認傳給 lerna bootstrap 命令的參數(shù)
- command.bootstrap.scope 羹奉, 指定那些包會受 lerna bootstrap 命令影響
- packages 秒旋, 指定包所在的目錄
命令行
lerna publish
發(fā)布新的庫版本
lerna publish // 發(fā)布最新commit的修改
lerna publish <commit-id> // 發(fā)布指定commit-id的代碼
Options
canary
可以用來獨立發(fā)布每個commit,不打tag
lerna publish --canary
# 1.0.0 => 1.0.1-alpha.0+${SHA} of packages changed since the previous commit
# a subsequent canary publish will yield 1.0.1-alpha.1+${SHA}, etc
lerna publish --canary --preid beta
# 1.0.0 => 1.0.1-beta.0+${SHA}
# The following are equivalent:
lerna publish --canary minor
lerna publish --canary preminor
# 1.0.0 => 1.1.0-alpha.0+${SHA}
--npm-client
默認npm
--npm-tag
為發(fā)布的版本添加 dist-tag
--no-verify-access
不進行用戶發(fā)布的權限校驗
--registry <url>
指定registry
--yes
用于ci自動輸入 yes
--temp-tag
沒啥用
lerna version
這個命令 識別出修改的包 --> 創(chuàng)建新的版本號 --> 修改package.json --> 提交修改 打上版本的tag --> 推送到git上诀拭。
版本號 遵循 semver
Options
--allow-branch <glob>
設置git上的哪些分支允許執(zhí)行 lerna version 命令迁筛, 也可以在lerna.json中設置
{
"command": {
"publish": {
"allowBranch": "master"
}
}
}
多個
{
"command": {
"publish": {
"allowBranch": ["master", "feature/*"]
}
}
}
--amend
把version的修改都合并到前一個commit, 而且不會推送哦耕挨。
--commit-hooks
執(zhí)行對應的commit-hook细卧, 默認true
--conventional-commits
使用了這個選項, lerna會收集日志筒占, 自動生成 CHANGELOG
--changelog-preset
修改changelog生成插件贪庙, 默認是 angular
--exact
??? 沒試出什么效果
--force-publish
強制更改所有包的版本, 不管有沒有修改
--ignore-changes
忽略檢查某些文件的修改
lerna version --ignore-changes '**/*.md' '**/__tests__/**'
也可以在lerna.json中配置
{
"ignoreChanges": [
"**/__fixtures__/**",
"**/__tests__/**",
"**/*.md"
]
}
--git-remote <name>
把修改推送到其它的源赋铝, 而不是origin
--git-tag-version
添加git的tag插勤, 默認true
--message <msg>
指定提交信息, 而不是自動生成的log
也可以在lerna.json配置
{
"command": {
"publish": {
"message": "chore(release): publish %s"
}
}
}
lerna bootstrap
bootstrap作了如下工作
- 為每個包安裝依賴
- 鏈接相互依賴的庫到具體的目錄
- 執(zhí)行 npm run prepublish
- 執(zhí)行 npm run prepare
可以通過 -- 后添加選項, 設置npm cient的參數(shù)
lerna bootstrap -- --production --no-optional
也可以在lerna.json中配置
{
...
"npmClient": "yarn",
"npmClientArgs": ["--production", "--no-optional"]
}
Options
--hoist
這個選項革骨,會把共同依賴的庫安裝到根目錄的node_modules下农尖, 統(tǒng)一版本
--ignore
忽略部分目錄, 不進行安裝依賴
lerna bootstrap --ignore component-*
也可以在lerna.json中配置
{
"version": "0.0.0",
"command": {
"bootstrap": {
"ignore": "component-*"
}
}
}
--ignore-scripts
不執(zhí)行聲明周期腳本命令良哲, 比如 prepare
--registry <url>
指定registry
--npm-client
指定安裝用的npm client
lerna bootstrap --npm-client=yarn
也可以在lerna.json中配置
{
...
"npmClient": "yarn"
}
--use-workspace
使用yarn workspace盛卡, 沒用過
--no-ci
默認調(diào)用 npm ci 替換 npm install , 使用選項修改設置
lerna list
列舉當前l(fā)erna 庫包含的包
Options
--json
顯示信息為json格式
--all
顯示包含private的包
--parseable
顯示如下格式 <fullpath>:<name>:<version>[:flags..]
--long
顯示更多的擴展信息
lerna changed
顯示自上次relase tag以來有修改的包, 選項通 list
lerna diff
顯示自上次relase tag以來有修改的包的差異筑凫, 執(zhí)行 git diff
lerna exec
在每個包目錄下執(zhí)行任意命令
使用示例
$ lerna exec -- <command> [..args] # runs the command in all packages
$ lerna exec -- rm -rf ./node_modules
$ lerna exec -- protractor conf.js
$ lerna exec -- npm view \$LERNA_PACKAGE_NAME
$ lerna exec -- node \$LERNA_ROOT_PATH/scripts/some-script.js
Options
--concurrency
默認命令時并行執(zhí)行的滑沧, 我們可以設置并發(fā)量為1
lerna exec --concurrency 1 -- ls -la
--scope
lerna exec --scope my-component -- ls -la
--stream
交叉并行輸出結(jié)果
lerna exec --stream -- babel src -d lib
--parallel
--no-bail
默認lerna exec,如果有命令返回了非0巍实, 則會停止執(zhí)行滓技, 通過設置這個參數(shù) 忽略返回非0, 繼續(xù)執(zhí)行其它命令
lerna run
執(zhí)行每個包package.json中的腳本命令
$ lerna run <script> -- [..args] # runs npm run my-script in all packages that have it
$ lerna run test
$ lerna run build
# watch all packages and transpile on change, streaming prefixed output
$ lerna run --parallel watch
選項通 lerna exec
lerna init
創(chuàng)建一個新的lerna庫或者是更新lerna版本
- 修改package.json中l(wèi)erna版本
- 創(chuàng)建lerna.json
Options
--independent
獨立模式
--exeact
版本號固定棚潦?
lerna add
添加一個包的版本為各個包的依賴
$ lerna add <package>[@version] [--dev] [--exact]
lerna clean
刪除各個包下的node_modules
lerna import
導入指定git倉庫的包作為lerna管理的包
lerna import <path-to-external-repository>
Options
--flatten
如果有merge沖突令漂, 用戶可以使用這個選項所謂單獨的commit
$ lerna import ~/Product --flatten
--dest
可以指定導入的目錄(lerna.json中設定的目錄)
$ lerna import ~/Product --dest=utilities
lerna link
鏈接互相引用的庫
lerna create
新建包
lerna create <name> [loc]
Create a new lerna-managed package
Positionals:
name The package name (including scope), which must be locally unique _and_
publicly available [string] [required]
loc A custom package location, defaulting to the first configured package
location [string]
Command Options:
--access When using a scope, set publishConfig.access value
[choices: "public", "restricted"] [default: public]
--bin Package has an executable. Customize with --bin
<executableName> [default: <name>]
--description Package description [string]
--dependencies A list of package dependencies [array]
--es-module Initialize a transpiled ES Module
--homepage The package homepage, defaulting to a subpath of the root
pkg.homepage [string]
--keywords A list of package keywords [array]
--license The desired package license (SPDX identifier) [default: ISC]
--private Make the new package private, never published
--registry Configure the package's publishConfig.registry [string]
--tag Configure the package's publishConfig.tag [string]
--yes Skip all prompts, accepting default values
作者:sovran
鏈接:http://www.reibang.com/p/8b7e6025354b
來源:簡書
著作權歸作者所有。商業(yè)轉(zhuǎn)載請聯(lián)系作者獲得授權丸边,非商業(yè)轉(zhuǎn)載請注明出處叠必。