前言
原生脚手架开发痛点
- 重复性操作
- 多package本地link
- 多package依赖安装
- 多package单元测试
- 多package代码提交
- 多package代码发布
- 版本一致性
- 发布时版本一致性
- 发布后相互依赖版本升级
package 越多管理难度越大
lerna
的优点
- 大幅度减少重复
- 提升操作的标准化
lerna是架构优化的产物,它揭示了一个架构的真理:项目复杂度提升后就需要对项目进行架构优化。架构优化的主要目标往往以效能为核心
所以,我选择了lerna作为包管理工具,但在发包的过程中遇到了很多问题,想和大家分享下,我是如何解决这些问题
如何发布至私有npm库
项目结构如下图所示:
我希望的结果:用户后面直接npm install @sf/map-service --save
安装所需的业务库,或npm install @sf/map-graph
安装所需的图表库,通过lerna publish
实现两个npm
包的发布。
可是却不断遇到下图的错误:
应是认证无效,需修改.npmrc
文件
//companydomain/artifactory/api/npm/npm/:_auth=MDE0MDM1NTg6Wmh1d2Vua2FpMDAz
_auth=MDE0MDM1NTg6Wmh1d2Vua2FpMDAz
always-auth = false
# 邮箱账号
email=**@yourEmail.com
# 公司的私有npm仓库
registry=http://companydomain/artifactory/api/npm/npm/
sass-binary-site=http://npm.taobao.org/mirrors/node-sass
上面的auth
如何得到,在git bash
里输入以下命令
echo -n 'myuser:mypassword' | openssl base64
即可得到当前用户的auth
认证
可是,按照上面信息配置了之后,再次运行lerna publish
,出现以下错误
这个错误,表示你本地有修改未commit
,每次lerna publish
之前,必须保证所有修改都commit
于是,git add .
,git commit -m 'modify version'
之后再次执行lerna publish
,仍然遇到下面的错误提示:
lerna notice Skipping all user and access validation due to third-party registry
lerna notice Make sure you're authenticated properly ¯\_(ツ)_/¯
lerna http fetch PUT 400 http://artifactory.sf-express.com/artifactory/api/npm/npm/sf-map-version-marker 227ms
lerna ERR! E400 400 Bad Request - PUT http://artifactory.sf-express.com/artifactory/api/npm/npm/sf-map-version-marker
绝不气馁,翻墙去google
找解决办法
在根级目录下的package.json
里添加
"publishConfig": {
"registry": "http://companydomain/artifactory/api/npm/npm-sf-local/"
},
"repository": {
"type": "git",
"url": "git+http://companydomain/scm/gis-acc/gis-acc-core-sf-view-vue.git"
}
添加根级LICENSE
文件
The MIT License (MIT)
Copyright (c) 2021-present, yourname (youremail)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
在packages
的两个包里面的package.json
里添加
"publishConfig": {
"registry": "http://companydomain/artifactory/api/npm/npm-sf-local/"
},
每个包的根目录下也添加LICENSE
文件
再次执行lerna publish
已发包成功!
去公司内的npm仓库查看,确有这些包,不过我发现了一个问题,我的@sf/map-service
需先执行本包的npm run pub
才能发包,如何通过lerna控制命令的执行呢?敬请期待下文分解