lerna入門篇

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.

  1. Fixed/Locked mode (default)
    Fixed模式下拇厢,項目通過單一的版本進行控制。版本號放在項目根目錄下的lerna.json文件的version這個字段晒喷。當你執(zhí)行 lerna publish孝偎,如果有文件更新,它將發(fā)布新的版本凉敲。
    babel項目采用的就是這種模式
  2. Independent mode (--independent)
    這種模式下衣盾,項目里的各個package獨立維護自己的version,它將會忽略lerna.json中定義的version

Commands####

init#####
$ lerna init

創(chuàng)建一個新的或升級一個已經(jīng)存在的lerna項目

  1. 在 package.json文件的devDependency字段里增加 lerna
  2. 生成lerna.json
--independent, -i#####
$ lerna init --independent

bootstrap####

lerna bootstrap

引導(dǎo)packages安裝各自的依賴爷抓,它有--ignore和--scope二個可選參數(shù)
它主要做以下幾件事:

  1. 為每個package npm install 安裝dependencies.
  2. 為packages 中存在相互 dependencies的做Symlink
  3. npm prepublish all bootstrapped packages.

publish####

$ lerna publish

發(fā)布當前項目
他創(chuàng)建一個新的release,生成新的版本势决,執(zhí)行g(shù)it commit/tag并發(fā)布到npm

  1. 發(fā)布項目里的每個模塊
  2. 執(zhí)行l(wèi)erna updated確定是否需要發(fā)布
  3. 假如需要發(fā)布 給lerna.json 版本號做自增
  4. 更新package.json里的版本號至最新
  5. 為新版本更新dependencies
  6. 為新版本創(chuàng)建一個git commit 和tag
  7. 發(fā)布更新項目到npm
  8. 一次發(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.

詳細文檔請看這里

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末渤昌,一起剝皮案震驚了整個濱河市据悔,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌耘沼,老刑警劉巖极颓,帶你破解...
    沈念sama閱讀 206,602評論 6 481
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異群嗤,居然都是意外死亡菠隆,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 88,442評論 2 382
  • 文/潘曉璐 我一進店門狂秘,熙熙樓的掌柜王于貴愁眉苦臉地迎上來骇径,“玉大人,你說我怎么就攤上這事者春∑葡危” “怎么了?”我有些...
    開封第一講書人閱讀 152,878評論 0 344
  • 文/不壞的土叔 我叫張陵钱烟,是天一觀的道長晰筛。 經(jīng)常有香客問我,道長拴袭,這世上最難降的妖魔是什么读第? 我笑而不...
    開封第一講書人閱讀 55,306評論 1 279
  • 正文 為了忘掉前任,我火速辦了婚禮拥刻,結(jié)果婚禮上怜瞒,老公的妹妹穿的比我還像新娘。我一直安慰自己般哼,他們只是感情好吴汪,可當我...
    茶點故事閱讀 64,330評論 5 373
  • 文/花漫 我一把揭開白布惠窄。 她就那樣靜靜地躺著,像睡著了一般漾橙。 火紅的嫁衣襯著肌膚如雪杆融。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 49,071評論 1 285
  • 那天近刘,我揣著相機與錄音,去河邊找鬼臀晃。 笑死觉渴,一個胖子當著我的面吹牛,可吹牛的內(nèi)容都是我干的徽惋。 我是一名探鬼主播案淋,決...
    沈念sama閱讀 38,382評論 3 400
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼险绘!你這毒婦竟也來了踢京?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 37,006評論 0 259
  • 序言:老撾萬榮一對情侶失蹤宦棺,失蹤者是張志新(化名)和其女友劉穎瓣距,沒想到半個月后,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體代咸,經(jīng)...
    沈念sama閱讀 43,512評論 1 300
  • 正文 獨居荒郊野嶺守林人離奇死亡蹈丸,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 35,965評論 2 325
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了呐芥。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片逻杖。...
    茶點故事閱讀 38,094評論 1 333
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖思瘟,靈堂內(nèi)的尸體忽然破棺而出荸百,到底是詐尸還是另有隱情,我是刑警寧澤滨攻,帶...
    沈念sama閱讀 33,732評論 4 323
  • 正文 年R本政府宣布够话,位于F島的核電站,受9級特大地震影響光绕,放射性物質(zhì)發(fā)生泄漏更鲁。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 39,283評論 3 307
  • 文/蒙蒙 一奇钞、第九天 我趴在偏房一處隱蔽的房頂上張望澡为。 院中可真熱鬧,春花似錦景埃、人聲如沸媒至。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,286評論 0 19
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽拒啰。三九已至驯绎,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間谋旦,已是汗流浹背剩失。 一陣腳步聲響...
    開封第一講書人閱讀 31,512評論 1 262
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留册着,地道東北人拴孤。 一個月前我還...
    沈念sama閱讀 45,536評論 2 354
  • 正文 我出身青樓,卻偏偏與公主長得像甲捏,于是被迫代替她去往敵國和親演熟。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 42,828評論 2 345

推薦閱讀更多精彩內(nèi)容