使用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

lerna import && add 使用&&常见问题解决的更多相关文章

  1. 接口测试之——Charles抓包及常见问题解决(转载自https://www.jianshu.com/p/831c0114179f)

    简介 Charles其实是一款代理服务器,通过成为电脑或者浏览器的代理,然后截取请求和请求结果达到分析抓包的目的.该软件是用Java写的,能够在Windows,Mac,Linux上使用,安装Charl ...

  2. springmvc环境搭建以及常见问题解决

    1.新建maven工程 a)  打开eclipse,file->new->project->Maven->Maven Project b)  下一步 c)   选择创建的工程为 ...

  3. OpenStack安装部署管理中常见问题解决方法

    一.网络问题-network 更多网络原理机制可以参考<OpenStack云平台的网络模式及其工作机制>. 1.1.控制节点与网络控制器区别 OpenStack平台中有两种类型的物理节点, ...

  4. C# .Net Framework4.5中配置和使用managedCUDA及常见问题解决办法

    主要参考英文帖子.我就不翻译了哈.很容易懂的. 先说明我的运行平台: 1.IDE:Visual Studio 2012 C# .Net Framework4.5,使用默认安装路径: 2.显卡类型:NV ...

  5. ubuntu安装navicat及常见问题解决

    1.安装navicat Step1: 下载Navicat ,网址:http://www.navicat.com/en/download/download.html Step2:进入下载目录,解压压缩包 ...

  6. Web Deploy发布网站及常见问题解决方法(图文)

    Web Deploy发布网站及常见问题解决方法(图文) Windows2008R2+IIs7.5 +Web Deploy 3.5 Web Deploy 3.5下载安装 http://www.iis.n ...

  7. charles抓包的安装,使用说明以及常见问题解决(windows)

    charles抓包的安装,使用说明以及常见问题解决(windows) https://blog.csdn.net/zhangxiang_1102/article/details/77855548

  8. Linux运维常见问题解决集锦【转】

    作为linux运维,多多少少会碰见这样那样的问题或故障,用点心,平时多注意积累,水平肯定越来越高. 下面就是常见问题解决集锦:   1.shell脚本不执行 问题:某天研发某同事找我说帮他看看他写的s ...

  9. Python爬虫编程常见问题解决方法

    Python爬虫编程常见问题解决方法: 1.通用的解决方案: [按住Ctrl键不送松],同时用鼠标点击[方法名],查看文档 2.TypeError: POST data should be bytes ...

随机推荐

  1. Java基础-类和对象

    类和对象 对象:对象是类的一个实例(对象不是找个女朋友),有状态和行为.例如,一条狗是一个对象,它的状态有:颜色.名字.品种:行为有:摇尾巴.叫.吃等. 类:类是一个模板,它描述一类对象的行为和状态. ...

  2. [rancher-net]

    ip rule命令 rancher网络全解读 arp命令查询 rancher managed network 实践 docker自定义网桥 iptables增删改查 shell脚本调试技术

  3. leetcode57:插入区间

    给出一个无重叠的 ,按照区间起始端点排序的区间列表. 在列表中插入一个新的区间,你需要确保列表中的区间仍然有序且不重叠(如果有必要的话,可以合并区间). 示例 1: 输入: intervals = [ ...

  4. 给定两个数组,这两个数组是排序好的,让你求这两个数组合到一起之后第K大的数。

    题目:给定两个数组,这两个数组是排序好的,让你求这两个数组合到一起之后第K大的数. 解题思路: 首先取得数组a的中位数a[aMid],然后在b中二分查找a[aMid],得到b[bMid],b[bSt] ...

  5. shell脚本实例-判断主机存活 以及企业备份方案

    1.上次写了一个脚本我那次考虑不是很周全,这次我将脚本改动了一下,这次是判断三次, 希望关注我的人可以经常交流哈.下面我写上代码. #!/usr/bin/bash while read ip do f ...

  6. Getting started with 3G | ip.access nano3G+OpenBSC+Osmocom-bb Part 1

    English Version could be find at Osmocom.org https://osmocom.org/projects/cellular-infrastructure/wi ...

  7. django-restful风格

    每一种求情都代表一种资源,它主要强调http应该一资源为中心,并且规范了url的风格, url:统一资源标志符,某一规定下能把资源独一无二的标示出来,好比每个人都有身份证号码. 它有四中对资源操作的请 ...

  8. java学习笔记20(Arraylist复习,Collection接口方法,迭代器,增强型for循环)

    集合:集合是Java提供的一种容器,可以用来存储多个数据: 集合与数组的区别:集合的长度是可变的,数组的长度是固定的 集合中存储的数据必须是引用类型数据: ArrayList回顾: public cl ...

  9. [学习日志]2018-11-18 主要: idea更改java版本

    idea更改java版本 问题: 解决办法:

  10. HDU 6066 17多校3 RXD's date(超水题)

    Problem Description As we all know that RXD is a life winner, therefore he always goes out, dating w ...