使用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. lxml简单用法 解析网页

    import requests s=requests.Session() re=s.get(lgurl,headers=headers)  #此处s可以直接换成requests the_page=re ...

  2. 关于Java的特点之封装

    抽象 1.简单理解 我们在前面去定义一个类时候,实际上就是把一类事物的共有的属性和行为提取出来,形成一个物理模型(模版).这种研究问题的方法称为抽象. 封装--什么是封装 封装就是把抽象出来的数据和对 ...

  3. 循环神经网络-LSTM

    LSTM(Long Short-Term Memory)是长短期记忆网络,是一种时间递归神经网络,适合于处理和预测时间序列中间隔和延迟相对较长的重要事件. LSTM能够很大程度上缓解长期依赖的问题. ...

  4. 基于Nutch Solr等基于搭建一体化的数据抓取平台

    参考链接:https://www.ibm.com/developerworks/cn/opensource/os-cn-BigInsightsNutchSolr/

  5. EffecJava Method

    坚持使用Overrider注解,可以预防我们并没有重载的情况出现. 除非使用者绝对安全,不然使用保护性拷贝,可以使程序安全. public class Period {//没有拷贝安全 private ...

  6. FMX取得屏分辨率

    procedure Tfrm_Main.FormCreate(Sender: TObject); var ScreenSvc: IFMXScreenService; Size: TPointF; be ...

  7. 下载从网页里面提取出来的图片(将url指向的图片下载并保存、从命名)

    import os #创建文件夹 from urllib import request #下载图片 if not os.path.exists('文件夹名字'): #创建文件夹名字 os.mkdir( ...

  8. 网络协议理论,http协议,数据结构,常用返回码

    一.网络协议理论 先是DNS协议 将域名转化成IP地址 这个你要知道 域名只是人记着方便 计算机记的是IP 然后是TCPIP协议 数据在传输过程中可能要经过陆游器 涉及到的是ARP协议 将IP地址转换 ...

  9. asp.net mvc webapi 实用的接口加密方法(转载)

    在很多项目中,因为webapi是对外开放的,这个时候,我们就要得考虑接口交换数据的安全性. 安全机制也比较多,如andriod与webapi 交换数据的时候,可以走双向证书方法,但是开发成本比较大, ...

  10. [转] Haproxy、Keepalived双主高可用负载均衡

    http://blog.chinaunix.net/uid-25266990-id-3989321.html 在测试了Nginx+Keepalived的负载均衡后,也对Haproxy+Keepaliv ...