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,该版本的只需要husky,husky 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仓库,也会提交信息。

相关推荐
Java后端的Ai之路2 小时前
06-React-Props
前端·react.js·前端框架
Flynt3 小时前
开源"间谍卫星"说数据全是真的,我把它的免key数据源挨个试了一遍
前端·开源·数据可视化
Csvn3 小时前
渲染管线
前端
程序员大阳4 小时前
副队长CSS教程(33)–圆角与透明
前端·css·透明·圆角
志尊宝4 小时前
Vue3 零基础每日笔记(082):文件上传实战——el-upload 校验、自定义上传、头像预览
前端·javascript·vue.js·笔记·#javascript·#vue.js
鬼手点金4 小时前
opencode-rules 插件
java·开发语言·前端·人工智能·chrome·git·github
柳杉4 小时前
借助 Codex 和可视化工作流,从调研到完整实现,3D 大屏竟然可以这么简单
前端·ai编程·数据可视化
IMPYLH5 小时前
HTML 的 <wbr> 元素
前端·html
Dovis(誓平步青云)5 小时前
常用命令藏得太深?怎么做一个快捷命令坞
运维·前端·自动化·音视频·prometheus
IT_陈寒6 小时前
Vite的这个问题坑了我三天,原来如此解决!
前端·人工智能·后端