打赏

gitHooks使用

gitHooks使用

package.json配置:

{
  "name": "eslint",
  "version": "0.1.0",
  "private": true,
  "scripts": {
    "serve": "vue-cli-service serve",
    "build": "vue-cli-service build",
    "lint": "vue-cli-service lint"
  },
  "dependencies": {
    "core-js": "^3.6.4",
    "vue": "^2.6.11",
    "vue-router": "^3.1.5"
  },
  "devDependencies": {
    "@vue/cli-plugin-babel": "^4.2.0",
    "@vue/cli-plugin-eslint": "^4.2.0",
    "@vue/cli-plugin-router": "^4.2.0",
    "@vue/cli-service": "^4.2.0",
    "@vue/eslint-config-standard": "^5.1.0",
    "babel-eslint": "^10.0.3",
    "eslint": "^6.7.2",
    "eslint-plugin-import": "^2.20.1",
    "eslint-plugin-node": "^11.0.0",
    "eslint-plugin-promise": "^4.2.1",
    "eslint-plugin-standard": "^4.0.0",
    "eslint-plugin-vue": "^6.1.2",
    "lint-staged": "^9.5.0",
    "vue-template-compiler": "^2.6.11"
  },
  "gitHooks": {
    "pre-commit": "lint-staged"
  },
  "lint-staged": {
    "*.{js,jsx,vue}": [
      "vue-cli-service lint",
      "git add"
    ]
  }
}

核心配置:

"gitHooks": {
    "pre-commit": "lint-staged"
  },
  "lint-staged": {
    "*.{js,jsx,vue}": [
      "vue-cli-service lint",
      "git add"
    ]
  }

执行说明:
(1)eslint校验标准:@vue/standard
(2)当开发者执行 git add 操作将代码提交到暂存区后,再执行 git commit 操作时:

  • pre-commit钩子在 git commit 执行时被触发,执行npm run precommit脚本(即lint-staged命令);
  • lint-staged的配置,就是利用linters对暂存区的文件路径应用过滤规则,匹配的文件将执行后面配置的任务,这里的任务就是调用项目中的eslint指令检查文件,如果报错则先自动修复--fix,最后把没有问题的代码加入暂存区git add。
  • 如果最终还有报错,则流程终止,无法执行 commit 操作。
posted @ 2020-02-09 12:06  孟繁贵  阅读(5536)  评论(0编辑  收藏  举报
TOP