配置使得在git commit -m ‘xxxx’时,先执行1. eslint检测 2.commit规范检测,两个条件通过后才commit成功,才可以push代码。是前端工程化的一部分,使得代码及commit更加规范

    npm install @commitlint/config-conventional @commitlint/cli —save
    同时需要在项目目录下创建配置文件 .commitlintrc.js, 写入:
    module.exports={extends:[‘@commitlint/config-conventional’],rules:{}};

    husky 安装
    npm install —save-dev husky
    npx husky-init
    执行完毕后,会在目录中生产.husky文件夹
    image.png
    会进入npm test 命令, 如果npm test 执行失败,commit 会提交失败

    1. #!/bin/sh
    2. . "$(dirname "$0")/_/husky.sh"
    3. npm test

    image.png

    feat
    fix
    docs
    style
    refactor
    test
    chore

    1. // const fs = require('fs')
    2. const path = require('path')
    3. const c = require('ansi-colors')
    4. console.log('eslint --fix')
    5. const exec = require('child_process').exec
    6. const cmdStr = 'npm run auto-fix'
    7. exec(cmdStr, {
    8. cwd: path.join(__dirname, '../')
    9. }, (err, stdout, stderr) => {
    10. if (err) {
    11. // console.log(err)
    12. // console.log(stderr)
    13. // 忽略错误
    14. }
    15. console.log(c.red(stdout))
    16. })

    [

    ](https://blog.csdn.net/qq_33712668/article/details/97259151)