发布 npm 库时,使用release-it自动化管理版本号、生成 changelog、tag 等

201 阅读1分钟

准备

  1. 一个可以push到远程的git仓库
  2. 注册并登录npm账号

安装release-it

1.安装模块:

npm i release-it -D

2.添加脚本

{
  "scripts": {
    "release": "release-it"
  }
}

3.运行release-it

npm run release

image-20230109211259151.png

规范生成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

image-20230109211505659.png

npm包发布流程

  1. 登录
npm login

登录不成功需检查镜像是不是用的npm的,若不是可使用nrm工具更换

  1. 发布
npm publish
  1. 删除
npm unpublish <name> -force
  1. 更新
第一步:执行npm version <版本号类型>
第二步:执行 npm publish

nrm的使用

  1. 安装
npm i -g nrm
  1. 常用命令
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/