【问题标题】:Pylint with pre-commit and EsLlint with husky带有预提交的 Pylint 和带有 husky 的 EsLlint
【发布时间】:2020-09-22 00:22:54
【问题描述】:

我有一个项目,前端是 JS,后端是 Python。 前端已经配置了 husky 预提交钩子。 今天我已经用预提交库配置了 Pylint,但是 husky 钩子已经被那个动作覆盖了。 是否可以结合 pre-commit 和 husky 库? 如果不是...解决问题的最佳方法是什么?

【问题讨论】:

    标签: git eslint pylint pre-commit.com git-husky


    【解决方案1】:

    pre-commit 有一个“迁移模式”,用于运行其他现有的钩子框架。似乎 husky 在他们的钩子实现中有点太聪明了,无法检测你正在运行的钩子——它们基于正在执行的文件名

    pre-commit 的迁移模式的工作方式是它采用任何现有的钩子脚本(在本例中,是 husky 编写的钩子脚本到.git/hooks/pre-commit)并添加扩展名.legacy。然后在执行期间它运行该脚本。

    但是对于哈士奇来说,看起来pre-commit.legacy 挂钩正在运行(!)

    一个小技巧是在package.json 中定义pre-commit.legacy,这似乎可行:

    package.json

    {
      "husky": {
        "hooks": {
          "pre-commit.legacy": "echo hello world"
        }
      },
      "dependencies": {
        "husky": "^4.3.0"
      }
    }
    

    .pre-commit-config.yaml

    # See https://pre-commit.com for more information
    # See https://pre-commit.com/hooks.html for more hooks
    repos:
    -   repo: https://github.com/pre-commit/pre-commit-hooks
        rev: v3.2.0
        hooks:
        -   id: trailing-whitespace
        -   id: end-of-file-fixer
        -   id: check-yaml
        -   id: check-added-large-files
    $ git commit -m "both"
    husky > pre-commit.legacy (node v12.18.3)
    hello world
    Trim Trailing Whitespace.................................................Passed
    Fix End of Files.........................................................Passed
    Check Yaml...........................................(no files to check)Skipped
    Check for added large files..............................................Passed
    [master 7bd8807] both
     1 file changed, 1 insertion(+), 1 deletion(-)
    

    也就是说,这似乎很脆弱。 pre-commit 旨在支持许多不同的编程语言(即使它是用 python 编写的,它也原生支持10+ programming languages包括 javascript))


    第一个替换可能是从local pre-commit hook 中调用 husky:

    package.json

    {
      "husky": {
        "hooks": {
          "pre-commit": "echo hello world"
        }
      },
      "dependencies": {
        "husky": "^4.3.0"
      }
    }
    

    .pre-commit-config.yaml

    # See https://pre-commit.com for more information
    # See https://pre-commit.com/hooks.html for more hooks
    repos:
    -   repo: https://github.com/pre-commit/pre-commit-hooks
        rev: v3.2.0
        hooks:
        -   id: trailing-whitespace
        -   id: end-of-file-fixer
        -   id: check-yaml
        -   id: check-added-large-files
    -   repo: local
        hooks:
        -   id: husky-run-pre-commit
            name: husky
            language: system
            entry: node_modules/.bin/husky-run pre-commit
            pass_filenames: false
            always_run: true
    

    执行

    $ pre-commit run --all-files --verbose
    Trim Trailing Whitespace.................................................Passed
    - hook id: trailing-whitespace
    - duration: 0.03s
    Fix End of Files.........................................................Passed
    - hook id: end-of-file-fixer
    - duration: 0.03s
    Check Yaml...............................................................Passed
    - hook id: check-yaml
    - duration: 0.05s
    Check for added large files..............................................Passed
    - hook id: check-added-large-files
    - duration: 0.05s
    husky....................................................................Passed
    - hook id: husky-run-pre-commit
    - duration: 0.07s
    
    husky > pre-commit (node v12.18.3)
    hello world
    
    

    然而,这个解决方案并没有利用 pre-commit 的 js 支持,它只是调用了已经存在的 husky 钩子


    更原生的解决方案是使用 pre-commit 直接安装 js 挂钩,例如,如果您使用的是 eslint:

    repos:
    -   repo: https://github.com/pre-commit/mirrors-eslint
        rev: 'v7.9.0'  # Use the sha / tag you want to point at
        hooks:
        -   id: eslint
    
    $ pre-commit  run --all-files
    [INFO] Initializing environment for https://github.com/pre-commit/mirrors-eslint.
    [INFO] Initializing environment for https://github.com/pre-commit/mirrors-eslint:eslint@7.9.0.
    [INFO] Installing environment for https://github.com/pre-commit/mirrors-eslint.
    [INFO] Once installed this environment will be reused.
    [INFO] This may take a few minutes...
    eslint...................................................................Passed
    

    免责声明:我是 pre-commit 的作者

    【讨论】:

    • 呵呵 :)) 感谢您提供非常有用和全面的答案。我尝试通过预提交直接安装 eslint,但它的抱怨很奇怪......就像Configuration for rule "react/jsx-boolean-value" is invalid...这就是为什么我决定从架子上拿一个旧的好鸭带:D
    • 啊你可能想include your plugins
    • 如何获得本地解决方案(第 3 个选项)以从 package.json 运行命令?在 husky 中,我运行 lint 和 lint:fix 以在您签入时自动更正 lint 问题。本机解决方案是否也使用项目的 .eslintrc.json 进行 linting?
    【解决方案2】:

    使用最新版本的 pre-commit (2.14.0) 和 husky (7.0.2) 更适合我的解决方案是将 pre-commit 集成到 husky 中,而不是相反。

    ./husky/_/pre-commit 文件中,我添加了pre-commit run

    见下文:

    #!/bin/sh
    . "$(dirname "$0")/_/husky.sh"
    
    npx lint-staged
    npx pretty-quick --staged
    pre-commit run
    

    【讨论】:

      猜你喜欢
      • 2022-01-07
      • 2020-12-06
      • 2019-07-28
      • 2021-04-15
      • 2021-10-21
      • 1970-01-01
      • 1970-01-01
      • 2020-07-28
      • 2023-01-17
      相关资源
      最近更新 更多