当前位置:   article > 正文

husky+ prettier + commitlint 提交前代码检查和提交信息规范

no staged files match any of provided globs

 

 

一、安装相关的包

  1. npm install -D husky
  2. npm install -D lint-staged // lint钩子
  3. npm install -D prettiernpm install -g @commitlint/cli @commitlint/config-conventional // commit 规范

husky npm地址:https://www.npmjs.com/package/husky

lint-staged npm/github地址: https://www.npmjs.com/package/lint-staged / https://github.com/okonet/lint-staged

prettier npm地址:https://www.npmjs.com/package/prettier

二、新增配置文件

1、添加.prettierrc.js文件

  1. // .prettierrc.jsmodule.exports = {
  2. printWidth: 80,
  3. semi: false, // 在每个语句的末尾添加分号
  4. singleQuote: false, // 使用单引号而不是双引号
  5. trailingComma: "none", // 多行时尽可能打印尾随逗号<none|es5|all>
  6. bracketSpacing: true, // 对象文字中打印括号之间的空格
  7. jsxBracketSameLine: true, // 将>多行JSX元素放在最后一行的末尾,而不是单独放在下一行
  8. arrowParens: "avoid", // 在单个箭头函数参数周围加上括号<avoid|always>
  9. requirePragma: false,
  10. proseWrap: "preserve"
  11. };

  其他配置可以查阅相关文档:https://prettier.io/docs/en/options.html

2、添加commitlint配置文件

在项目根路径执行

echo "" > commitlint.config.js

复制下面代码到文件中

  1. // commitlint.config.jsmodule.exports = {
  2. extends: ["@commitlint/config-conventional"],
  3. rules: {
  4. "type-enum": [
  5. 2,
  6. "always",
  7. ["feat", "fix", "docs", "style", "refactor", "test", "chore", "revert"]
  8. ],
  9. "subject-full-stop": [0, "never"],
  10. "subject-case": [0, "never"]
  11. }
  12. }

  

用于说明 commit 的类别,只允许使用下面7个标识。

  • feat:新功能(feature)
  • fix:修补bug
  • docs:文档(documentation)
  • style: 格式(不影响代码运行的变动)
  • refactor:重构(即不是新增功能,也不是修改bug的代码变动)
  • test:增加测试
  • chore:构建过程或辅助工具的变动

3、修改package.json文件

增加如下配置

  1. // package.json
  2. {
  3. "lint-staged": {
  4. "src/**/*.{js,ts,tsx}": [
  5. "prettier --write",
  6. "tslint --project tsconfig.json",
  7. "git add ."
  8. ]
  9. },
  10. "husky": {
  11. "hooks": {
  12. "pre-commit": "lint-staged",
  13. "commit-msg": "commitlint -e $GIT_PARAMS"
  14. }
  15. },
    "config": {
      "commitizen": {
        "path": "cz-customizable"
      }
    },
}

tslint 相关配置:https://palantir.github.io/tslint/rules/

4、如需配置eslint

(1)新增eslint相关的插件

npm install -D eslint eslint-config-ali eslint-plugin-import babel-eslint eslint-plugin-prettier eslint-config-prettier eslint-plugin-react

(2)新增.eslintrc.js文件,文件中写入以下配置

  1. module.exports = {
  2. root: true,
  3. env: {
  4. node: true
  5. },
  6. 'extends': [
  7. "eslint-config-ali",
  8. "prettier",
  9. "plugin:prettier/recommended",
  10. 'plugin:react/recommended',
  11. 'eslint:recommended'
  12. ],
  13. rules: {
  14. 'no-console': process.env.NODE_ENV === 'production' ? 'error' : 'off',
  15. 'no-debugger': process.env.NODE_ENV === 'production' ? 'error' : 'off',
  16. 'no-mixed-spaces-and-tabs': 'off',
  17. 'no-unused-vars': 'off',
  18. "prettier/prettier": "error",
  19. "strict": "off",
  20. "import/no-dynamic-require": "off",
  21. "global-require": "off",
  22. "require-yield": "off",
  23. },
  24. plugins: ["prettier"],
  25. parserOptions: {
  26. parser: 'babel-eslint'
  27. }
  28. }

 eslint相关配置规则: https://cloud.tencent.com/developer/section/1135842

(3)修改package.json 

  1. "scripts": {
  2. ...,
  3. "lint": "eslint . --ext .js,.ts --ignore-path .gitignore",
  4. "fix": "npm run lint -- --fix"
  5. },
  6. "lint-staged": {
  7. "src/**/*.{js,ts,tsx}": [
  8. "prettier --write",
  9. "eslint --fix --ext .js",
  10. "git add ."
  11. ]
  12. },
  13. "husky": {
  14. "hooks": {
  15. "pre-commit": "lint-staged",
  16. "commit-msg": "commitlint -e $GIT_PARAMS"
  17. }
  18. },

三、使用结果

1、任意修改一个文件不符合ts要求

执行结果

  1. detanxdeMacBook-Pro:kyc_flatform detanx$ sudo git commit -m 'feat: add format'
  2. husky > pre-commit (node v12.4.0)
  3. ↓ Stashing changes... [skipped]
  4. → No partially staged files found...
  5. ❯ Running tasks...
  6. ❯ Running tasks for src/**/*.{js,ts,tsx}
  7. ✔ prettier --write
  8. ✖ tslint --project tsconfig.json
  9. git add .
  10. ✖ tslint --project tsconfig.json found some errors. Please fix them and try committing again.
  11. ERROR: /Users/detanx/Desktop/kyc_flatform/src/components/MoneyManage/GoldenIn/index.tsx:92:7 - Forbidden 'var' keyword, use 'let' or 'const' instead
  12. husky > pre-commit hook failed (add --no-verify to bypass)

  

如果想忽略这个提示可以在执行命令时加 --no-verify

例如:

git commit -m 'feat: add format' --no-verify

2、当代码格式和tslint校验通过后,提交信息不规范时

  1. detanxdeMacBook-Pro:kyc_flatform detanx$ sudo git commit -m 'feat:add format'
  2. husky > pre-commit (node v12.4.0)
  3. No staged files match any of provided globs.
  4. husky > commit-msg (node v12.4.0)
  5. ⧗ input: feat:add format
  6. ✖ subject may not be empty [subject-empty]
  7. ✖ type may not be empty [type-empty]
  8. ✖ found 2 problems, 0 warnings
  9. ⓘ Get help: https://github.com/conventional-changelog/commitlint/#what-is-commitlint
  10. husky > commit-msg hook failed (add --no-verify to bypass)

 注: 提交信息必须是规定的7个标识开始,并跟随一个英文输入法下的冒号':'和一个空格,接着是提交的内容

举例:

 git commit -m 'feat: add format'

3、提交成功 

  1. [detanxdeMacBook-Pro:kyc_flatform detanx$ git commit -m 'feat: add format'
  2. husky > pre-commit (node v12.4.0)
  3. ↓ Stashing changes... [skipped]
  4. → No partially staged files found...
  5. ✔ Running tasks...
  6. husky > commit-msg (node v12.4.0)
  7. [master 3b341a99] feat: add forma

四、配置过程中遇到的一些问题

1、pre-commit 放置在scripts对象中会报一个waring

  1. Warning: Setting commit-msg script in package.json > scripts will be deprecated
  2. Please move it to husky.hooks in package.json, a .huskyrc file, or a husky.config.js file
  3. Or run ./node_modules/.bin/husky-upgrade for automatic update
  4. See https://github.com/typicode/husky for usage

意思就是这个命令需要放置在husky对象的hooks中,或者配置在.huskyrc的配置文件中,类似下面这样

  1. {
  2. "husky": {
  3. "hooks": {
  4. "pre-commit": "lint-staged",
  5. "commit-msg": "commitlint -e $GIT_PARAMS"
  6. }
  7. },
  8. }

  

参考链接:https://blog.csdn.net/y491887095/article/details/80594043 

https://blog.csdn.net/weixin_34416649/article/details/87948338

转载于:https://www.cnblogs.com/detanx/p/codeFormat.html

声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号