带有预提交的Pylint和带有沙哑的EsLlint package.json .pre-commit-config.yaml package.json local 执行

问题描述

我有一个项目,该项目的前端使用JS,后端使用Python。 前端已配置了沙哑的预提交钩子。 今天,我为Pylint配置了预提交库,但是此举已覆盖了繁琐的钩子。 是否可以合并预提交库和赫斯基库? 如果没有,那么解决问题的最佳方法是什么?

解决方法

pre-commit具有用于运行其他现有的hook框架的“迁移模式”。沙哑的钩子实现似乎不太聪明,无法检测到您正在运行的钩子-他们基于正在执行的文件名

预提交的迁移模式的工作方式是,它采用任何现有的挂钩脚本(在这种情况下,是由husky编写到 function reparse(){ let notify let promise = new Promise(async(resolve,reject)=>{ instanceOfjQueryDeferred.done(()=>{ resolve(100) }).progress((progress)=>{ notify(progress) }) }) // here is the monkey patch promise.progress = (handler)=>{ notify = handler return promise } return promise } 的挂钩脚本)并添加扩展名reparse().progress((p)=>{ console.log('progress',p) }).then((progress)=>{ console.log('done',progress) }) 。然后在执行期间运行该脚本。

但是令人讨厌的是,.git/hooks/pre-commit钩似乎正在运行(!)

一个小技巧是在.legacy中定义pre-commit.legacy,这似乎很有效:

package.json

pre-commit.legacy

.pre-commit-config.yaml

package.json

说,这似乎很脆弱。 pre-commit旨在支持许多不同的编程语言(尽管它是用python编写的,但它对10+ programming languages包括javascript )具有本机支持)


第一个替代方法可能是只从{ "husky": { "hooks": { "pre-commit.legacy": "echo hello world" } },"dependencies": { "husky": "^4.3.0" } } 预提交钩子调用赫斯基:

package.json

# 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(-)

local

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

执行

.pre-commit-config.yaml

但是,该解决方案没有利用pre-commit的js支持,只是调用了已经存在的哈斯基钩子


更原生的解决方案是直接使用预提交安装js钩子,例如,如果您使用的是eslint:

# 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


免责声明:我是预先提交的作者

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...