lerna import && add 使用&&常见问题解决

时间:2024-06-11 23:04:02

使用lerna 的import 我们可以方便的将一个普通的npm 包倒入到lerna 管理的monorepo 中

环境准备

  • lerna init
    注意必须是一个git 项目,同时需要commit ,不然会报错
lerna init
  • 创建一个npm package
    不再lerna 初始化的目录, 注意必须是一个git 项目,同时需要commit
yarn init -y
index.js:
module.exports = {
  username:"dalong",
  age:333
}
  • 目录结构如下
├── platform
│ ├── lerna.json
│ ├── package.json
│ └── packages
└── userlogin
    ├── index.js
    └── package.json

使用import

  • import
    如何userlogin 不是git 项目,会有如下的错误提示,注意需要git init 并提交
 cli v3.8.0
lerna ERR! Error: Command failed: git log --format=%h
lerna ERR! fatal: Not a git repository (or any of the parent directories): .git
lerna ERR! 
lerna ERR! 
lerna ERR! at makeError (/usr/local/lib/node_modules/lerna/node_modules/execa/index.js:174:9)
lerna ERR! at Function.module.exports.sync (/usr/local/lib/node_modules/lerna/node_modules/execa/index.js:338:15)
lerna ERR! at Object.execSync (/usr/local/lib/node_modules/lerna/node_modules/@lerna/child-process/index.js:22:16)
lerna ERR! at ImportCommand.externalExecSync (/usr/local/lib/node_modules/lerna/node_modules/@lerna/import/index.js:137:34)
lerna ERR! at ImportCommand.initialize (/usr/local/lib/node_modules/lerna/node_modules/@lerna/import/index.js:82:25)
lerna ERR! at Promise.resolve.then (/usr/local/lib/node_modules/lerna/node_modules/@lerna/command/index.js:266:24)
lerna ERR! at <anonymous>
lerna ERR! lerna Command failed: git log --format=%h
lerna ERR! lerna fatal: Not a git repository (or any of the parent directories): .git
lerna ERR! lerna 

如何lerna 项目没有commit 会有如下提示,lerna 项目也必须完整的commit

Error: Command failed: git rev-parse HEAD
lerna ERR! fatal: ambiguous argument 'HEAD': unknown revision or path not in the working tree.
lerna ERR! Use '--' to separate paths from revisions, like this:
lerna ERR! 'git <command> [<revision>...] -- [<file>...]'
lerna ERR! 
lerna ERR! HEAD
lerna ERR! 
lerna ERR! at makeError (/usr/local/lib/node_modules/lerna/node_modules/execa/index.js:174:9)
lerna ERR! at Function.module.exports.sync (/usr/local/lib/node_modules/lerna/node_modules/execa/index.js:338:15)
lerna ERR! at Object.execSync (/usr/local/lib/node_modules/lerna/node_modules/@lerna/child-process/index.js:22:16)
lerna ERR! at ImportCommand.getCurrentSHA (/usr/local/lib/node_modules/lerna/node_modules/@lerna/import/index.js:129:34)
lerna ERR! at ImportCommand.initialize (/usr/local/lib/node_modules/lerna/node_modules/@lerna/import/index.js:98:31)
lerna ERR! at Promise.resolve.then (/usr/local/lib/node_modules/lerna/node_modules/@lerna/command/index.js:266:24)
lerna ERR! at <anonymous>
lerna ERR! lerna Command failed: git rev-parse HEAD
lerna ERR! lerna fatal: ambiguous argument 'HEAD': unknown revision or path not in the working tree.
lerna ERR! lerna Use '--' to separate paths from revisions, like this:
lerna ERR! lerna 'git <command> [<revision>...] -- [<file>...]'
lerna ERR! lerna 
lerna ERR! lerna HEAD

import 提示信息,同意即可

lerna import ../userlogin         
lerna notice cli v3.8.0
lerna info About to import 1 commits from ../userlogin into packages/userlogin
? Are you sure you want to import these commits onto the current branch? (ynH) 

信息如下:

lerna notice cli v3.8.0
lerna info About to import 1 commits from ../userlogin into packages/userlogin
? Are you sure you want to import these commits onto the current branch? Yes
lerna info 8a94131 
lerna success import finished
  • 效果
├── lerna.json
├── package.json
└── packages
    └── userlogin
        ├── index.js
        └── package.json
  • 使用倒入的package
    在platform 目录,按照提示操作即可
lerna create loginui 
cd loginui 
lerna add userlogin
  • 编写调用代码
loginui/lib/loginui.js
'use strict';
const userlogin = require("userlogin");
module.exports = loginui;
function loginui() {  
    return userlogin;
}
简单测试代码
loginui/__tests__/loginui.test.js
```code
'use strict';
const loginui = require('..');
console.log(loginui())

添加npm script
packages/loginui/package.json

+"scripts": {
+ "test": "node __tests__/loginui.test"
+}
  • 在lerna 根项目编写npm script 调用测试
    platform/package.json
+ "scripts": {
+ "test":"lerna run test"
+ }
  • 调用效果
    yarn test
yarn test
yarn run v1.10.1
$ lerna run test
lerna notice cli v3.8.0
lerna info Executing command in 1 package: "npm run test"
lerna info run Ran npm script 'test' in 'loginui' in 0.5s:
> loginui@0.0.0 test /Users/dalong/mylearning/lerna-project/dalongdemo/platform/packages/loginui
> node __tests__/loginui.test
{ username: 'dalong', age: 333 }
lerna success run Ran npm script 'test' in 1 package in 0.5s:
lerna success - loginui
✨ Done in 1.33s.

说明

lerna 对于我们确认大型项目配置的一致性有很大的帮助

参考资料

https://lernajs.io/#command-import