返回

使用changelog日志自动化插件进行内容自动生成

前端

对于一些需要手动输入更新日志的项目,如何实现自动化输出更新日志?并且对更新日志进行内容格式化,便于后期维护。

本文为大家介绍一款强大的工具,它可以帮助我们轻松实现changelog日志的自动生成和格式化。这款工具就是commitizen。

安装commitizen依赖包

npm install --global commitizen

在package.json中加入以下内容

{
  "scripts": {
    "commit": "git-cz"
  },
  "config": {
    "commitizen": {
      "path": "node_modules/cz-conventional-changelog"
    }
  }
}

在package.json中创建以commitlint开头的字段

{
  "commitlint": {
    "extends": ["@commitlint/config-conventional"]
  }
}

在package.json中创建以husky开头的字段

{
  "husky": {
    "hooks": {
      "commit-msg": "commitlint -E HUSKY_GIT_PARAMS"
    }
  }
}

完成以上配置后,就可以在命令行工具中使用git cz命令来生成changelog日志了。

命令行工具的使用

git cz

运行git cz命令后,commitizen工具会自动生成一个交互式界面,如下图所示:

[图片]

在这个交互式界面中,我们可以选择更新日志的类型,比如修复bug、添加新特性、重构代码等等。还可以输入更新日志的标题和。

输入完成后,按下回车键即可生成更新日志。

日志格式化

commitizen工具支持对更新日志进行格式化。我们可以通过在.cz-config.js文件中配置日志的格式。

module.exports = {
  types: [
    {value: 'feat', name: 'feat:     新特性'},
    {value: 'fix', name: 'fix:      修复bug'},
    {value: 'docs', name: 'docs:     文档更新'},
    {value: 'style', name: 'style:    代码风格调整'},
    {value: 'refactor', name: 'refactor: 代码重构'},
    {value: 'perf', name: 'perf:     性能优化'},
    {value: 'test', name: 'test:     测试用例更新'},
    {value: 'build', name: 'build:    构建系统更新'},
    {value: 'ci', name: 'ci:       持续集成更新'},
    {value: 'chore', name: 'chore:    其他修改'}
  ],
  scopes: [
    {name: 'ui'},
    {name: 'backend'},
    {name: 'tests'}
  ],
  allowCustomScopes: true,
  allowBreakingChanges: ['feat', 'fix'],
  subjectLimit: 100
};

在上面的配置中,我们定义了更新日志的类型和范围。还可以通过subjectLimit字段来限制更新日志标题的长度。

总结

通过使用commitizen工具,我们可以轻松实现changelog日志的自动生成和格式化。这不仅可以节省我们的时间,还可以确保changelog日志的完整性和准确性。

对于使用commitizen工具有任何疑问,可以随时在下方评论区留言。