lerna###
Lerna 是一個用來優(yōu)化托管在git\npm上的多package代碼庫的工作流的一個管理工具,可以讓你在主項目下管理多個子項目,從而解決了多個包互相依賴赐劣,且發(fā)布時需要手動維護多個包的問題。
lerna項目看起來是什么樣子的呢蜈抓?###
my-lerna-repo/
package.json
packages/
package-1/
package.json
package-2/
package.json
Lerna 能做什么?###
Lerna中主要要用到 lerna bootstrap、lerna publish這二個命令
- lerna bootstrap: 安裝依賴
- lerna publish:發(fā)布和更新package
開始我們的探索之旅###
目前的最新版Lerna 2.x 是beta版
安裝######
# install the latest 2.x version using the `prerelease` dist-tag
$ npm install --global lerna@prerelease
# install version directly
$ npm install --global lerna@^2.0.0-beta
新建git倉庫并用lerna初始化######
$ git init lerna-repo
$ cd lerna-repo
lerna init
現(xiàn)在我們的項目看起來應(yīng)該是這樣的:
lerna-repo/
package.json
lerna.json
How it works####
Lerna 提供兩類管理項目的模式: Fixed 和 Independent.
- Fixed/Locked mode (default)
Fixed模式下拇厢,項目通過單一的版本進行控制。版本號放在項目根目錄下的lerna.json文件的version這個字段晒喷。當你執(zhí)行 lerna publish孝偎,如果有文件更新,它將發(fā)布新的版本凉敲。
babel項目采用的就是這種模式 - Independent mode (--independent)
這種模式下衣盾,項目里的各個package獨立維護自己的version,它將會忽略lerna.json中定義的version
Commands####
init#####
$ lerna init
創(chuàng)建一個新的或升級一個已經(jīng)存在的lerna項目
- 在 package.json文件的devDependency字段里增加 lerna
- 生成lerna.json
--independent, -i#####
$ lerna init --independent
bootstrap####
lerna bootstrap
引導(dǎo)packages安裝各自的依賴爷抓,它有--ignore和--scope二個可選參數(shù)
它主要做以下幾件事:
- 為每個package npm install 安裝dependencies.
- 為packages 中存在相互 dependencies的做Symlink
- npm prepublish all bootstrapped packages.
publish####
$ lerna publish
發(fā)布當前項目
他創(chuàng)建一個新的release,生成新的版本势决,執(zhí)行g(shù)it commit/tag并發(fā)布到npm
- 發(fā)布項目里的每個模塊
- 執(zhí)行l(wèi)erna updated確定是否需要發(fā)布
- 假如需要發(fā)布 給lerna.json 版本號做自增
- 更新package.json里的版本號至最新
- 為新版本更新dependencies
- 為新版本創(chuàng)建一個git commit 和tag
- 發(fā)布更新項目到npm
- 一次發(fā)布所有packages,刪除lerna-temp tags和增加tags到latest
通過在package.json中標記 "private": true,將使該包不發(fā)布
--npm-tag [tagname]####
$ lerna publish --npm-tag=next
When run with this flag, publish
will publish to npm with the given npm dist-tag (defaults to latest
).
當加上這個選項蓝撇,發(fā)布時除了發(fā)布到npm果复,同時也會同步到給定的dist-tag(默認為latest)
This option can be used to publish a prerelease
or beta
version.
這個選項一般用來發(fā)布prerelease或beta版
通常,通過npm install my-package這個命令安裝的就是latest tag,你可以通過npm install my-package@prerelease來安裝prerelease版
--canary, -c####
$ lerna publish --canary
When run with this flag, publish publishes packages in a more granular way (per commit). Before publishing to npm, it creates the new version tag by taking the current version and appending the current git sha (ex: 1.0.0-alpha.81e3b443
).
The intended use case for this flag is a per commit level release or nightly release.
--skip-git####
$ lerna publish --skip-git
When run with this flag, publish will publish to npm without running any of the git commands.
Only publish to npm; skip committing, tagging, and pushing git changes (this only affects publish).
--skip-npm####
$ lerna publish --skip-npm
When run with this flag, publish will update all package.json package versions and dependency versions, but it will not actually publish the packages to npm.
This is useful as a workaround for an npm issue which prevents README updates from appearing on npmjs.com when published via Lerna. When publishing with README changes, use --skip-npm and do the final npm publish by hand for each package.
This flag can be combined with --skip-git to just update versions and dependencies, without committing, tagging, pushing or publishing.
Only update versions and dependencies; don't actually publish (this only affects publish).
--force-publish [packages]####
$ lerna publish --force-publish=package-2,package-4
# force publish all packages
$ lerna publish --force-publish=*
When run with this flag, publish will force publish the specified packages (comma-separated) or all packages using *
.
This will skip the lerna updated check for changed packages and forces a package that didn't have a git diff change to be updated.
--yes####
$ lerna publish --canary --yes
# skips `Are you sure you want to publish the above changes?`
When run with this flag, publish will skip all confirmation prompts. Useful in Continuous integration (CI) to automatically answer the publish confirmation prompt.
--repo-version####
$ lerna publish --repo-version 1.0.1
# applies version and skips `Select a new version for...` prompt
When run with this flag, publish will skip the version selection prompt and use the specified version. Useful for bypassing the user input prompt if you already know which version to publish.
updated####
$ lerna updated
Check which packages have changed since the last release (the last git tag).
Lerna determines the last git tag created and runs git diff --name-only v6.8.1 to get all files changed since that tag. It then returns an array of packages that have an updated file.
clean####
$ lerna clean
Remove the node_modules directory from all packages.
lerna clean respects the --ignore and --scope flags (see Flags).
diff####
$ lerna diff [package?]
$ lerna diff
# diff a specific package
$ lerna diff package-name
Diff all packages or a single package since the last release.
Similar to lerna updated. This command runs git diff
ls####
$ lerna ls
List all of the public packages in the current Lerna repo.
lerna ls respects the --ignore and --scope flags (see Flags).
run####
$ lerna run [script] # runs npm run my-script in all packages that have it
$ lerna run test
$ lerna run build
Run an npm script in each package that contains that script.
lerna run respects the --concurrency, --scope and ignore flags (see Flags).
$ lerna run --scope my-component test
exec####
$ lerna exec -- [command] # runs the command in all packages
$ lerna exec -- rm -rf ./node_modules
$ lerna exec -- protractor conf.js
Run an arbitrary command in each package.
lerna exec respects the --concurrency, --scope and --ignore flags (see Flags).
$ lerna exec --scope my-component -- ls -la
Hint: The commands are spawned in parallel, using the concurrency given. The output is piped through, so not deterministic. If you want to run the command in one package after another, use it like this:
$ lerna exec --concurrency 1 -- ls -la
import####
$ lerna import <path-to-external-repository>
Import the package at <path-to-external-repository>, with commit history, into packages/<directory-name>. Original commit authors, dates and messages are preserved. Commits are applied to the current branch.
This is useful for gathering pre-existing standalone packages into a Lerna repo. Each commit is modified to make changes relative to the package directory. So, for example, the commit that added package.json will instead add packages/<directory-name>/package.json.
Misc####
Lerna will log to a lerna-debug.log file (same as npm-debug.log) when it encounters an error running a command.
Lerna also has support for scoped packages.
Running lerna without arguments will show all commands/options.
lerna.json####
{
"lerna": "2.0.0-beta.18",
"version": "1.1.3",
"publishConfig": {
"ignore": [
"ignored-file",
"*.md"
]
},
"bootstrapConfig": {
"ignore": "component-*"
},
"packages": ["packages/*"]
}
- lerna: the current version of Lerna being used.
- version: the current version of the repository.
- publishConfig.ignore: an array of globs that won't be included in lerna updated/publish. Use this to prevent publishing a new version unnecessarily for changes, such as fixing a README.md typo.
- bootstrapConfig.ignore: an glob that won't be bootstrapped when running the lerna bootstrap command.
- bootstrapConfig.scope: an glob that restricts which packages will be bootstrapped when running the lerna bootstrap command.
- packages: Array of globs to use as package locations.
詳細文檔請看這里