2021-09-09 Lerna

初始化项目

  1. lerna init
  2. lerna ls 查看有多少个包
    1. 先看这四个库: lerna @lerna/cli @lerna/add @lerna/init
  3. 创建子包
    1. lerna create lerna2 --registry http://localhost:4873

yargs/yargs的使用 (解析命令行参数的)

  1. 安装lerna add yargs --scope=@lerna2/cli

如何链接包

  1. 在lerna2包中引入了cli, create, init

    {
       "devDependencies": {
          "@lerna2/cli": "^0.0.0",
          "@lerna2/create": "^0.0.0",
          "@lerna2/init": "^0.0.0"
       }
    }
    

    然后在lerna2目录下执行 lerna link

  2. 方式2: package.json添加依赖后, 直接执行lerna bootstrap

  3. 方式3: 根路径下执行:

    lerna add <moduleName>  // 所有子包都添加这个依赖
    lerna add <moduleName> --scope=<pkgName> // 给scope后的包添加依赖
    lerna add <pkgName1> --scope=<pkgName2> // 给pkgName2中添加pkgName1,包内的互相引用,会复制pkgName1到pkgName2中
    

调试

  1. 在lerna2目录下: 执行npm link, 将lerna2链接到全局
  2. 查看连接是否成功: npm root -g 然后在此目录下查看

lerna bootstrap

默认是npm i,因为我们指定过yarn,so,run yarn install,会把所有包的依赖安装到根node_modules.

lerna list

列出所有的包,如果与你文夹里面的不符,进入那个包运行yarn init -y解决

lerna diff [package?]

列出所有或者软件包自上次发布以来的修改情况

lerna import <path-to-external-repository>

导入本地已经存在的包

lerna add packageName --scope=workspacePackageName

lerna add lodash --scope=my-component

lerna run

运行npm script,可以指定具体的package。

lerna run <script> -- [..args] # 运行所有包里面的有这个script的命令
$ lerna run --scope my-component test

$ lerna run <script> -- [..args] # 在所有包下运行指定

# 例如
$ lerna run test # 运行所有包的 test 命令
$ lerna run build # 运行所有包的 build 命令
$ lerna run --parallel watch # 观看所有包并在更改时发报,流式处理前缀输出

$ lerna run --scope my-component test # 运行 my-component 模块下的 test

lerna exec

运行任意命令在每个包

$ lerna exec -- < command > [..args] # runs the command in all packages
$ lerna exec -- rm -rf ./node_modules
$ lerna exec -- protractor conf.js
lerna exec --scope my-component -- ls -la

lerna link

项目包建立软链,类似npm link

lerna clean

删除所有包的node_modules目录

lerna changed

列出下次发版lerna publish 要更新的包。

原理:
需要先git add,git commit 提交。
然后内部会运行git diff --name-only v版本号 ,搜集改动的包,就是下次要发布的。并不是网上人说的所有包都是同一个版全发布。

如果forcePublish为true 那么所有的包都会change发布
 "command": {
    "version": {
      "forcePublish": true
    },
 }

lerna publish

会打tag,上传git,上传npm。
如果你的包名是带scope的例如:"name": "@gp0320/gpwebpack",
那需要在packages.json添加

"publishConfig": {
  "access": "public"
},

提交对项目的更新

运行该命令会执行如下的步骤:

  1. 运行lerna updated来决定哪一个包需要被publish
  2. 如果有必要,将会更新lerna.json中的version
  3. 将所有更新过的的包中的package.json的version字段更新
  4. 将所有更新过的包中的依赖更新
  5. 为新版本创建一个git commit或tag
  6. 将包publish到npm上
$ lerna publish  用于发布更新 
$ lerna publish --skip-git     # 不会创建git commit或tag 
$ lerna publish --skip-npm   # 不会把包publish到npm上 |

lerna ls

显示packages下的各个package的version

lerna publish 相关参数

lerna publish [--dist-tag=tag名]

image.png

如图所示,控制台会让用户选择要发版的版本号(最后一个可以自定义)

dist-tag选项可以发版一个其他分支的包,在测试时比较有用!

image.png

测试时可以发布一个beta版(--dist-tag=beta),通过手动@版本号安装(npm install 默认安装latest)

参考

http://www.sosout.com/2018/07/21/lerna-repo.html

https://www.bilibili.com/read/cv4948266

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容