lerna

what lerna

Lerna is a tool that optimizes the workflow around managing multi-package repositories with git and npm

lerna是一个优化基于git+npm的多包项目管理工具

why use lerna

lerna工具解决了多包管理的开发痛点,减少了重复操作,规范了操作的流程,从而提高了研发效能。

多包开发痛点:

重复操作:

  • 本地link
    • 例:开发某个包,依赖于其他包,需要到每个包里手动npm link,然后才能被安装使用
  • 依赖安装
  • 单元测试
  • 代码提交
    • 例:每个包都是git仓库
  • 代码发布

    • 例:每个包都需要发布到npm官网或公司内部私服上,需要到每个包里手动执行对应的发布流程,以及更新包后的版本升级需手动更新

      版本一致性:

  • 版本不一致性

    • 例:发布时,需要到每个包里手动指定对应版本
  • 相互依赖版本升级

    • 例:utils包添加新功能后,所有依赖此新功能的包,都需要手动升级utils版本

      who use

  • create-react-app: https://github.com/facebook/create-react-app

  • umi: https://github.com/umijs/umi
  • dumi: https://github.com/umijs/dumi
  • pro-components: https://github.com/ant-design/pro-components

    初始化项目

    • 初始化npm项目

      1. npm init
    • 安装lerna

      1. npm config set registry https://registry.npm.taobao.org npm install -g nrm
      2. npm install lerna -g
    • lerna init:初始化项目

      1. lerna init
      1. 注:初始化时,会初始化git仓库,同时需要关联到远程仓库
      1. git remote add origin git@server-name:path/repo-name.git

      固定模式 —exact

      固定模式,通过lerna.json的文件进行版本管理。当你执行lerna publish命令时,如果有一个包更新后,发布时所有包的版本一起升级。

      独立模式 —independent

      独立模式,init的时候需要设置选项 —independent. 独立模式允许管理者对每个库单独改变版本号,每次发布的时候,你需要为每个改动的库指定版本号。这种情况下, lerna.json的版本号不会变化了, 默认为independent。

      创建包

  • lerna create:创建包

    1. lerna create @dlyndon/cli
  • lerna add: 添加依赖

    1. # 安装到所有的包里
    2. lerna add lodash
    3. # 安装到某个包里
    4. lerna add lodash --scope @dlyndon/cli
    5. lerna add lodash packages/cli

    开发和测试

  • lerna bootstrap: 安装多包下所有的依赖

    1. lerna bootstrap
  • lerna clean:清空多包下所有的依赖

    1. lerna clean
  • lerna link: 软链接依赖

    1. # 在cli包里手动加上本地utils包的依赖,再link
    2. # 或lerna add @dlyndon/utils packages/cli 会自动完成软链接
    3. lerna link

    注:npm file方式加载依赖(不能使用lerna boostrap进行安装,不能使用tnpm)

    1. "dependencies": {
    2. "utils": "file:../utils"
    3. }
  • learn exec: 执行shell脚本

    1. lerna exec -- ls -l
    2. lerna exec --scope @dlyndon/cli -- ls -l
  • lerna run: 执行npm命令(package.json中的scripts)

    1. lerna run test
    2. lerna run --scope @dlyndon/cli test

    发布上线

  • lerna version:升级版本号等

    1. lerna version
    2. lerna version --no-push
  • lerna changed:查看当前版本与上个版本的包有那些变更了

    1. lerna changed
  • lerna diff:查看变更内容

    1. lerna diff
  • lerna publish:项目发布 ```bash lerna publish

发布当前 commit 中打上tag version 的包

lerna publish from-git

发布 package 中 pkg.json 上的 version 在 registry(高于 latest version)不存在的包

lerna publish from-package

// 用于不发布到远程仓库,并使用本地的Npm仓库 lerna publish —no-push —registry http://localhost:4873

  1. 注:同时可使用.npmrc文件进行配置registry
  2. ```bash
  3. # just in case a private registry is configured in ~/.npmrc
  4. registry = http://localhost:4873

image.png
image.png

问题

发布失败

  • npm 未登录
  • (带组织的包)package.json 增加配置 “publishConfig”: { “access”: “public” }
  • git未配置相关信息
    1. $ git config --global user.name "Your Name"
    2. $ git config --global user.email "email@example.com"