代码风格统一:commitlint & eslint & standard-version

Commitlint 配置

  • 添加包

    yarn add @commitlint/cli @commitlint/config-conventional husky -D
  • 配置package.json

    "husky": {
        "hooks": {
          // 此处如果不使用husky 需要将HUSKY_GIT_PARAMS 替换为 GIT_PARAMS
          "commit-msg": "commitlint -E HUSKY_GIT_PARAMS"
        }
      },
      "commitlint": {
        "extends": [
          "@commitlint/config-conventional"
        ]
      }
  • 效果: 代码提交的格式不符合标准 就会直接被拒绝。在团队协作时提交历史的回溯需要有良好的提交历史

版本管理

yarn add standard-version -D

配置package.json

"scripts": {
    "release": "standard-version"
  }

执行 yarn release 生成CHANGELOG.md 类似

# Change Log

All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.

<a name="1.0.3"></a>
## 1.0.3 (2018-12-05)


### Features

* **lint:** 添加commitlint配置 ([faee26d](http://url/commits/faee26d))
* **lint:** 简化commitlint配置 ([affeb7d](http://url/commits/affeb7d))

ESLint配置

yarn add lint-staged -D

配置package.json

"husky": {
    "hooks": {
      // 代码提交前 执行lint 也可以配合prettier将代码直接格式化后提交
      "pre-commit": "lint-staged"
    }
  },
"lint-staged": {
    "linters": {
      "/src/**/*.js": [
        "eslint --fix",
        "git add"
      ]
    },
    "ignore": [
      "/**/*.min.js"
    ]
  }

代码提交之前执行lint 保证代码格式统一

相关推荐