微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决带有预提交的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


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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。