# 常用命令

# 创建与查看

# 初始化
npx lerna init

# 创建新包
npx lerna create @my-scope/tools

# 查看变更包
npx lerna changed
1
2
3
4
5
6
7
8

# 安装依赖

新版本 Lerna 更推荐配合 workspace(npm/pnpm/yarn)管理依赖。

# 在指定 package 下安装依赖(示例:pnpm)
pnpm add axios --filter @my-scope/tools
1
2

# 批量执行命令

# 在所有包执行脚本
npx lerna run build

# 在所有包执行 shell 命令
npx lerna exec -- npm test
1
2
3
4
5

# 按范围执行

# 只在某个包执行
npx lerna run build --scope @my-scope/tools

# 排除某个包
npx lerna run test --ignore @my-scope/demo
1
2
3
4
5

# 只跑受影响的包

npx lerna run build --since
1

这个命令在 CI 中很常用,能减少无效构建。

# 发布相关

# 版本变更(不发布)
npx lerna version

# 版本变更 + 发布
npx lerna publish

# 从已有 git tag 发布
npx lerna publish from-git
1
2
3
4
5
6
7
8

# 命令使用建议

  • 本地开发多用:run、exec、--scope
  • CI 多用:changed、--since
  • 发布阶段多用:version、publish from-git