husky,commit规范,生成CHANGELOG.md,npm发版

项目git提交工程化(钩子,提交信息commit message),npm修改版本,需要涉及到的包:

  • husky,允许在git钩子中执行不同的脚步,如commitlint,eslint,prettier,lint-staged各种工具
  • @commitlint/cli,@commitlint/config-conventional,commitizen,cz-conventional-changelog, 验证commit信息,以及采取的规范,以及帮助生产commit message的工具
  • changelog-cli,changelog-conventional,生成CHANGELOG.md文件,以及根据什么规范来生成CHANGELOG.md
  • standard-version,生成CHANGELOG.md,修改package.json的version,commit其新增的文件,最后的git push和npm publish需要自己手动敲。

先贴一份包的版本。

json 复制代码
{
  "name": "commit-lint",
  "version": "1.3.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "prepare": "husky",
    "commit": "cz",
    "commitlint": "commitlint --edit",
    "changelog": "conventional-changelog -p angular -i CHANGELOG.md -s -r 0",
    "release": "standard-version"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "@commitlint/cli": "^19.6.0",
    "@commitlint/config-conventional": "^19.6.0",
    "@commitlint/prompt-cli": "^19.6.0",
    "commitizen": "^4.3.1",
    "conventional-changelog": "^6.0.0",
    "conventional-changelog-cli": "^5.0.0",
    "cz-conventional-changelog": "^3.3.0",
    "husky": "^9.1.7",
    "standard-version": "^9.5.0"
  },
  "config": {
    "commitizen": {
      "path": "./node_modules/cz-conventional-changelog"
    }
  }
}

安装husky

  • 安装
bash 复制代码
npm i husky -D

package.json 添加脚本 prepare:husky,该版本的只需要huskyhusky install命令已废弃

  • 执行
    在根目录生成 .husky 文件夹
bash 复制代码
npm run prepare
  • 添加 .husky/commit-msg
bash 复制代码
# npm run commitlint 这里会报错 node 找不到,只能通过这种来暂时解决
./node_modules/.bin/commitlint --edit

commitlint提交信息相关包安装

bash 复制代码
npm i @commitlint/cli @commitlint/config-conventional -D
  • 配置 commintlint.config.js 使用 @commitlint/config-conventional 校验信息

  • 添加 commitlint 脚本 "commitlint": "commitlint --edit"

    这样就会在 git commit -m "xxx"的时候校验,可以通过 --no-verify 选项来跳过校验

  • 安装 commitizen 来帮助生成 commit 信息

bash 复制代码
npm i commitzen -D

初始化项目使用 cz-conventional-changelog 该commit规范,该命令会安装该包并且在 package.json 中新增 配置

或者可以自己手动添加

bash 复制代码
# npm
commitizen init cz-conventional-changelog --save-dev --save-exact

# yarn
commitizen init cz-conventional-changelog --yarn --dev --exact

# pnpm
commitizen init cz-conventional-changelog --pnpm --save-dev --save-exact

此时,就可以通过 npx cz 或者配置好 sciript.comit:cz在来获取 commit 提示

安装 conventional-log conventional-changelog-cli

bash 复制代码
npm i conventional-log conventional-changelog-cli  -D

初次生成可以使用命令 conventional-changelog -p angular -i CHANGELOG.md -s -r 0

后续只往 CHANGELOG.md 文件append内容 conventional-changelog -p angular -i CHANGELOG.md -s

安装standard-version

安装包并添加脚本,可以通过npx standard-verion 【参数】 来或者 npm run release -- 【参数】来生成 CHANGELOG.md,提交新增和修改的文件,修改本项目的 version 以及 git tag 打标签

bash 复制代码
npm i standard-version

参数

bash 复制代码
npm run release -- --first-release
npm run release -- --prerelease alpha # 1.0.1-alpha.0
npm run release # 默认增加patch版本号
npm run release -- --release-as minor
npm run release -- --release-as 1.1.0
npm run release -- --no-verify # 不触发git钩子,如commit-msg钩子
npm run release -- --dry-run # 只显示命令,不执行
npm run release -- -t vv # 自定义 git tag 时的标签前缀,-t不加参数,默认是v前缀

如果使用了 standard-version,那么CHANGELOG.md就可以由他帮我们生成,底层也是用的conventional-changelog,最后 git push 和 npm publish要自己去执行, standard-version不做此操作。

npm version patch -m "提交的信息" ,npm自带的该命令就是来修改 version 同时如果项目有git仓库,也会提交信息。

相关推荐
wearegogog12312 小时前
基于 MATLAB 的卡尔曼滤波器实现,用于消除噪声并估算信号
前端·算法·matlab
Drawing stars12 小时前
JAVA后端 前端 大模型应用 学习路线
java·前端·学习
品克缤13 小时前
Element UI MessageBox 增加第三个按钮(DOM Hack 方案)
前端·javascript·vue.js
小二·13 小时前
Python Web 开发进阶实战:性能压测与调优 —— Locust + Prometheus + Grafana 构建高并发可观测系统
前端·python·prometheus
小沐°13 小时前
vue-设置不同环境的打包和运行
前端·javascript·vue.js
qq_4198540513 小时前
CSS动效
前端·javascript·css
烛阴13 小时前
3D字体TextGeometry
前端·webgl·three.js
桜吹雪14 小时前
markstream-vue实战踩坑笔记
前端
C_心欲无痕14 小时前
nginx - 实现域名跳转的几种方式
运维·前端·nginx
花哥码天下14 小时前
恢复网站console.log的脚本
前端·javascript·vue.js