从0开始commitlint+husky配置,Commitizen的使用

257 阅读3分钟

commitlint 官网:commitlint.js.org/#/guides-lo…

创建一个空文件夹打开cmd命令框执行以下命令:

git init  // 初始化空 Git 仓库

npm init  //生成package.json文件记录项目流程

//安装commitlint-cli与config-conventional
npm install --save-dev @commitlint/config-conventional @commitlint/cli 

安装husky:

npm install husky@4.2.3 --save-dev

注意: 建议不要使用latest版本安装,可能hooks会失效;可能是一个官方的ISSUES;

(我在安装最新版本的时候,husky配置不生效,并且不能正常的添加commit-msg文件)

激活husky(生成.husky文件夹)

npx husky install

生成commit-msg文件:

npx husky add .husky/commit-msg 'npx --no-install commitlint --edit $1'

package.json文件中配置husky:

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

生成commitlint.config.js文件

echo "module.exports = {extends: ['@commitlint/config-conventional']}" > commitlint.config.js

添加预定义类型

/** 
* fix: 表示修复了一个 bug(这和语义化版本中的 PATCH 相对应)。 
* feat: 表示新增了一个功能(这和语义化版本中的 MINOR 相对应)。 
* docs: 表示修改了文档,没有影响代码。 * style: 表示修改了代码风格,不影响代码原功能。 
* refactor: 表示重构,不同于 style,是在设计层面的修改。 * perf: 表示提升性能。 
* test: 表示修改了测试代码。 * chore: 表示其他杂项修改,build、ci 之类的修改也在这里面。 
* merge: 表示合入代码,在手动提交时使用。Gitlab MR 合入时可以保持默认消息格式。 
* revert: 表示 revert 之前的代码,在手动提交时使用。Gitlab 提供的默认 Revert 消息可以保持。 
*/ 
module.exports = {   
 extends: [   
    '@commitlint/config-conventional' 
 ], 
 rules: {        
  'type-enum': [     
       2,           // 表示必须输入的           
      'always',
       [            
          'fix',          
          'feat',         
          'docs',        
          'style',       
          'refactor',       
          'perf',              
          'test',     
          'chore',             
          'merge',        
          'revert'          
      ]    
   
   ]     
 }
};

提交规范:注意冒号后面的空格

git commit -m <type>[optional scope]: <description> //scope字段为type字段提供上下文

scope字段配置

module.exports = {
  extends: ['@commitlint/config-conventional'],
  rules: {
    // Place your rules here
    'scope-enum': [2, 'always', ['a', 'b']], // 如果scope填写的信息不是列表所提供就会报错
  },
};

更多配置请参考官网文档:https://commitlint.js.org/#/reference-rules

测试:

错误测试:

正确提交:

最后package.json文件内容以及目录结构如下:

Commitizen

全局安装commitizen node模块

npm install -g commitizen

在项目目录下运行以下命令

commitizen init cz-conventional-changelog --save --save-exactr

 运行完以上的命令,commitizen就已经安装完毕,此时可以使用git cz来代替git commit 提交代码,通过选择生成符合适的commit message

package.json

还可以在package.json中进行自定义type的配置

引入cz-customizabletype类型、scope范围进行自定义配置

在根目录下创建.cz-config.js文件

module.exports = {  
//可选类型  
types: [  
  { value: 'feat', name: 'feat:   新功能' }, 
  { value: 'fix', name: 'fix:   修复' },
  { value: 'docs', name: 'docs:   文档变更' },   
  { value: 'style', name: 'style:   代码格式(不影响代码运行的变动)' },  
  {      
    value: 'refactor',     
    name: 'refactor:重构(既不是增加feature),也不是修复bug'
  }, 
  { value: 'perf', name: 'perf:   性能优化' }, 
  { value: 'test', name: 'test:   增加测试' },  
  { value: 'chore', name: 'chore:   构建过程或辅助功能的变动' },  
  { value: 'revert', name: 'revert:   回退' },  
  { value: 'build', name: 'build:   打包' },   
  { value: 'revert', name: 'revert:   回退' } 
],  
scopes: [  { name: "模块1" },  { name: "模块2" },  { name: "模块3" },  { name: "模块4" },],//消息步骤  
messages: {  
  type: '请选择提交类型',    
  customScope: '请输入修改范围(可选)', 
  subject: '请简要描述提交(必填)',  
  body: '请输入详细描述(可选)',  
  footer: '请输入要关闭的issue(可选)',  
  confirmCommit: '确认以上信息提交?(y/n)'  }, 
  //跳过问题 
  skipQuestion: ['body', 'footer'],  
  //subject文字长度默认是  
  subjectLimit: 72
}