tool

lerna 项目搭建

2021-06-25  本文已影响0人  努力学习的小丸子

项目结构

fip-tools
├── commands
|  ├── lint
|  |  ├── command.js
|  |  ├── package.json
|  |  ├── README.md
|  |  └── __tests__
|  |     └── lint.test.js
|  └── version
├── core
|  ├── cli
|  |  ├── lib
|  |  |  └── cli.js
|  |  ├── package-lock.json
|  |  ├── package.json
|  |  ├── README.md
|  |  └── __tests__
|  |     └── cli.test.js
|  └── test
├── lerna.json
├── package-lock.json
├── package.json
└── packages

文件详情

// commands/lint/command.js
exports.command = "lint";
exports.describe = "Create a new Lerna repo or upgrade an existing repo to the current version of Lerna.";
exports.builder = {
    exact: {
        describe: "Specify lerna dependency version in package.json without a caret (^)",
        type: "boolean",
    },
    independent: {
        describe: "Version packages independently",
        alias: "i",
        type: "boolean",
    },
};
exports.handler = function handler(argv) {
    console.log('lint789...');
};
// core/cli/lib/cli.js
#!/usr/bin/env node
const yargs = require("yargs/yargs");
const lintCmd = require('lint/command')
function cli() {
    yargs(process.argv.slice(2)).command(lintCmd).argv;
}
cli()
yarn global add lerna
// 在项目根目录执行
lerna init
// lerna.json
{
  "useWorkspaces": true,
  "version": "0.0.10"
}

// package.json
{
  "name": "root",
  "private": true,
  "workspaces": [
    "commands/*",
    "core/*"
  ],
  "devDependencies": {
    "lerna": "^4.0.0"
  }
}
// 在core 下目录下建立cli 包
lerna create cli core
lerna create commands lint
// 将lint包添加为cli的依赖
lerna add lint  --scope=cli
lerna add module-1 --scope=module-2
lerna publish
npm i cli
npx fip lint => lint789...

总结:
1、各个包之间的版本管理:初始化时使用lerna init --independent,生成的lerna.json中的是'version':'independent',这表示各个包之间的版本互相独立。
2、自定义存放包的位置:自定义lerna.json中的packages字段,如packages: [ "commands/*", "core/*" ],.。
3、搭配yarn使用workspaces环境,需指定npmClient:'yarn'

// 查看工作区
yarn workspaces info
// 给B包安装依赖A
yarn workspace packageB add packageA 

4、lerna 命令

lerna init
lerna create
lerna add
lerna bootstrop
lerna publish
上一篇下一篇

猜你喜欢

热点阅读