commitLint

npm install -g commitizen

commitizen 提供了适配器用于扩展自身的能力(可以简单理解为插件),开发者可以自己设计适配器,也可以使用别人设计好的适配器。主要介绍几个常用的适配器:

cz-conventional-changelog

commitizen init cz-conventional-changelog --save --save-exact
 
"devDependencies": {
 "cz-conventional-changelog": "^2.1.0"
},
"config": {
  "commitizen": {
    "path": "./node_modules/cz-conventional-changelog"
  }
}

cz-customizable

npm install cz-customizable --save-dev
 
"devDependencies": {
  "cz-customizable": "^5.3.0"
},
"config": {
  "commitizen": {
    "path": "node_modules/cz-customizable"
  }
}
 

.cz-config.js

 
'use strict';
 
module.exports = {
 
  types: [
    { value: 'feat', name: '特性:    一个新的特性' },
    { value: 'fix', name: '修复:    修复一个Bug' },
    { value: 'docs', name: '文档:    变更的只有文档' },
    { value: 'style', name: '格式:    空格, 分号等格式修复' },
    { value: 'refactor', name: '重构:    代码重构,注意和特性、修复区分开' },
    { value: 'perf', name: '性能:    提升性能' },
    { value: 'test', name: '测试:    添加一个测试' },
    { value: 'chore', name: '工具:    开发工具变动(构建、脚手架工具等)' },
    { value: 'revert', name: '回滚:    代码回退' },
    { value: 'WIP', name: 'WIP:     正在进行工作' }
  ],
 
  // scopes: [
  //   { name: 'accounts' },
  //   { name: 'admin' },
  //   { name: 'exampleScope' },
  //   { name: 'changeMe' }
  // ],
 
  // it needs to match the value for field type. Eg.: 'fix'
  /*
  scopeOverrides: {
    fix: [
      {name: 'merge'},
      {name: 'style'},
      {name: 'e2eTest'},
      {name: 'unitTest'}
    ]
  },
  */
  // override the messages, defaults are as follows
  messages: {
    type: 'Select the type of change that you\'re committing:',
    scope: '\nDenote the SCOPE of this change (optional):',
    // used if allowCustomScopes is true
    customScope: 'Denote the SCOPE of this change:',
    subject: 'Write a SHORT, IMPERATIVE tense description of the change:\n',
    body: 'Provide a LONGER description of the change (optional). Use "|" to break new line:\n',
    breaking: 'List any BREAKING CHANGES (optional):\n',
    footer: 'List any ISSUES CLOSED by this change (optional). E.g.: #31, #34:\n',
    confirmCommit: 'Are you sure you want to proceed with the commit above?'
  },
 
  allowCustomScopes: true,
  allowBreakingChanges: ['feat', 'fix'],
 
  // limit subject length
  subjectLimit: 100
 
};
 

Commitizen 校验

npm install --save-dev @commitlint/cli
npm install --save-dev @commitlint/config-conventional
 
// commitlint.config.js
module.exports = {
  extends: ['@commitlint/config-conventional']
};
 
 
 

husky

npm install husky --save-dev
 
// package.json
"husky": {
  "hooks": {
    "commit-msg": "commitlint -E HUSKY_GIT_PARAMS"
  }
}

变更日志

使用了 commitizen(cz-customizable) + commitlint(commitlint-config-cz)的组合之后,可以确保团队协作的过程中都生成符合 Augular 规范的提交说明。生成了规范的提交说明之后可以使用 conventional-changelog-cli 自动生成变更日志:

npm install --save-dev conventional-changelog-cli

安装完成后在 package.json 中新增 CLI 命令:

"scripts": {
  "changelog": "conventional-changelog -p angular -i CHANGELOG.md -s -r 0"
}

温馨提示:可以查看 Vue 代码的 package.json,使用的也是 conventional-changelog 工具,注意该工具的 CLI 参数,有些参数是增量式生成日志,这里是对日志进行全量覆盖处理。

执行 npm run changelog 之后会在根目录下自动生成 CHANGELOG.md 文件