配置 standard-version 自动生成 CHANGELOG

2,035 阅读1分钟
  1. 借助 commitizen 规范 git commit 信息

  2. 配置 standard-version 自动生成 CHANGELOG

1. commitizen 规范 git commit 信息

  • 下载依赖包
npm i commitizen -D # git commit 交互工具
npx commitizen init cz-conventional-changelog --save-dev --save-exact
    # 自动配置 commitizen 规则包:
    # 1. 下载了 cz-conventional-changelog 规则包
    # 2. 在 package.json 添加规则包的配置
  • 修改package.json
// package.json
{
    "scripts":{
        "commit": "cz", // 使用 npm run cz 代替 git commit, 交互式填写 commit 信息
    },
    "config": {
        "commitizen": {
          "path": "./node_modules/cz-conventional-changelog" // 配置规则包
        }
    }
}

2. 使用 standard-version 自动生成 CHNAGELOG

npm install -D standard-version
    # standard-version 作用:
    # 1. 更新 package.json 的 version: 根据 commit 信息中的 feat,fix,BREACK-CHANGE 决定版本号
    # 2. git 打标签
    # 3. 更新 CHANGELOG: 只会在 CHANGELOG.md 中记录 feat,fix,BREACK-CHANGE 类型的 commit 记录
// package.json
"scripts": {
  "commit" : "cz",
  "release": "standard-version -t $(date +release-%Y%m%d-v)",
    // -t: 规范tag名, 如 release-20211223-v1.0.1
  "release:first": "standard-version --first-release -t $(date +release-%Y%m%d-v)", 
    // 第一次发版用
}

使用流程

  1. 开发完成...

  2. 提交代码: git add . >> npm run cz(代替git commit) >> git push

  3. 发版: npm run release

    • 内部流程: 更新package.jsonversion >> 更新CHANGELOG.md >> git tag xxx >> git add . >> git commit
    • 注意: 第一次发版用npm run release:first, 它不会更新版本号
  4. 推送标签到远程: git push origin --tags

  5. 触发CI/CD 部署...