Gitmoji引领规范化:从提交到自动生成日志

2,205 阅读2分钟

下面是润色后的文章:

前言


  1. 创建 Git 仓库:git init
  2. Git 忽略规则:.gitignore - 排除 node_modules 文件夹
  3. 查看代码

1. Gitmoji Commit Message 规范


  • 格式
<emoji> <type>(<scope>): <subject>
<BLANK LINE>
<body>
<BLANK LINE>
<footer>
  • 示例
feat(blog): 添加评论部分

2. 撰写 Commit


  • cz-customizable:可自定义的 Commitizen 插件(或独立实用程序),有助于创建一致的提交消息。

  • 安装

npm i -D cz-customizable
  • 配置:在根目录创建 .cz-config.js 文件,配置如下:
module.exports = {
  types: [
    {
      value: ':sparkles: feat',
      name: '✨ feat:     新功能'
    },
    {
      value: ':bug: fix',
      name: '🐛 fix:      修复 bug'
    },
    {
      value: ':tada: init',
      name: '🎉 init:     初始化'
    },
    {
      value: ':pencil2: docs',
      name: '✏️  docs:     文档变更'
    },
    {
      value: ':lipstick: style',
      name: '💄 style:    代码样式美化'
    },
    {
      value: ':recycle: refactor',
      name: '♻️  refactor: 重构'
    },
    {
      value: ':zap: perf',
      name: '⚡️ perf:     性能优化'
    },
    {
      value: ':white_check_mark: test',
      name: '✅ test:     测试'
    },
    {
      value: ':rewind: revert',
      name: '⏪️ revert:   回退'
    },
    {
      value: ':package: build',
      name: '📦️ build:    打包'
    },
    {
      value: ':rocket: chore',
      name: '🚀 chore:    构建/工程依赖/工具'
    },
    {
      value: ':construction_worker: ci',
      name: '👷 ci:       CI 相关变更'
    }
  ],
  messages: {
    type: '请选择提交类型(必填)',
    customScope: '请输入文件修改范围(可选)',
    subject: '请简要描述提交(必填)',
    body: '请输入详细描述(可选)',
    breaking: '列出任何 BREAKING CHANGES(可选)',
    footer: '请输入要关闭的 issue(可选)',
    confirmCommit: '确定提交此说明吗?'
  },
  allowCustomScopes: true,
  allowBreakingChanges: [':sparkles: feat', ':bug: fix'],
  subjectLimit: 72
}
  • 将新的脚本添加到您的 package.json 文件中:
"scripts": {
  ...
  "commit": "git add . && cz-customizable"
}
  • 使用 npm run commit 代替 git commit 进行提交。

3. 校验 Commit


  • commitlint:用于检查提交是否符合特定规范的工具。

  • 安装

npm i -D @commitlint/cli commitlint-config-git-commit-emoji commitlint-config-cz
  • 配置:在根目录创建 commitlint.config.js 文件,配置如下:
module.exports = {
  extends: ['git-commit-emoji', 'cz']
}
  • husky:用于自动执行 commitlint 的 Git 流程钩子。

  • 安装

npm i -D husky
  • 配置
npm set-script prepare "husky install"
npm run prepare
npx husky add .husky/commit-msg 'npx --no-install commitlint --edit "$1"'

4. 生成日志


  • conventional-changelog:一系列工具,根据项目的提交历史和元数据信息自动生成变更日志和发布说明。

  • 安装

npm i -D conventional-changelog-cli conventional-changelog-gitmoji-config
  • 将以下脚本添加到您的 package.json 文件中:
"scripts": {
  ...
  "changelog": "conventional-changelog -p gitmoji-config -i CHANGELOG.md -s"
}
  • 运行 npm run changelog 以生成日志。

参考文章