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仓库,也会提交信息。

相关推荐
卸任6 小时前
Vibe coding工具使用心得
前端·ai编程
brien96 小时前
为什么模型标称 1M,Google Antigravity 聊到 256K 却突然失忆?我们给它做了个原生行内监控圆环
前端
修炼的dance7 小时前
录屏关键帧间隔多少合适?六档实测,2秒到10秒小了75.6%
前端
Lank_M7 小时前
关键帧从1秒拉到10秒,录屏小87.3%,摄像头小10.8%
前端
补码缩写7 小时前
用 ffprobe 拆录屏,八成字节在 29 个关键帧上
前端
百思不得小李7 小时前
一个复杂的动态表单是如何实现的
前端·设计模式
陈随易7 小时前
Cmder 停更 17 个月,2.0 支持 Windows Terminal
前端·后端·程序员
风骏时光牛马7 小时前
前沿视界
前端
kyriewen7 小时前
我扫了 814 个依赖,97 个还敢要 Node 10
前端·javascript·程序员
陈随易8 小时前
3000 万人在用的翻译插件,重写一遍就是一门生意
前端·后端·程序员