- 本文参加了由公众号@若川视野 发起的每周源码共读活动, 点击了解详情一起参与。
- 第39期 | 如何发布一个 npm 库,自动化管理版本号、生成 changelog、tag 等
准备
- 一个可以push到远程的git仓库
- 注册并登录npm账号
安装release-it
1.安装模块:
npm i release-it -D
2.添加脚本
{
"scripts": {
"release": "release-it"
}
}
3.运行release-it
npm run release
规范生成changelog
1.安装规范生成changelog的生成规则和changelog 插件
npm i -D git-cz
npm i @release-it/conventional-changelog -D
2.添加脚本
{
"scripts": {
"commit": "git-cz"
}
}
3.创建.release-it.json
{
"github": {
"release": false
},
"git": {
"commitMessage": "release: v${version}"
},
"npm": {
"publish": false
},
"hooks": {
"after:bump": "echo 更新版本成功"
},
"plugins": {
"@release-it/conventional-changelog": {
"preset": "angular",
"infile": "CHANGELOG.md"
}
}
}
4.运行
# 运行前需要将文件暂存一下
npm run commit
npm包发布流程
- 登录
npm login
登录不成功需检查镜像是不是用的npm的,若不是可使用nrm工具更换
- 发布
npm publish
- 删除
npm unpublish <name> -force
- 更新
第一步:执行npm version <版本号类型>
第二步:执行 npm publish
nrm的使用
- 安装
npm i -g nrm
- 常用命令
nrm -V
nrm -h
nrm current
nrm use <name>
nrm add <name> <url>
nrm del <name>
nrm ls
nrm test <name>
例:
nrm ls
# npm ---------- https://registry.npmjs.org/
# yarn --------- https://registry.yarnpkg.com/
# tencent ------ https://mirrors.cloud.tencent.com/npm/
# cnpm --------- https://r.cnpmjs.org/
# taobao ------- https://registry.npmmirror.com/
# npmMirror ---- https://skimdb.npmjs.com/registry/
nrm use taobao
# Registry has been set to: https://registry.npmmirror.com/