带有预提交的Pylint和带有沙哑的EsLlint [英] Pylint with pre-commit and EsLlint with husky
问题描述
我有一个项目,该项目的前端使用JS,后端使用Python.前端已配置了沙哑的预提交钩子.今天,我为Pylint配置了预提交库,但是此举已覆盖了繁琐的钩子.是否可以合并预提交库和赫斯基库?如果没有,那么解决问题的最佳方法是什么?
I have a project with a frontend in JS and backend in Python. Frontend had been configured with husky pre-commit hook. Today I've configured Pylint with pre-commit library but husky hooks had been overwritten by that move. Is it possible to combine pre-commit and husky libraries? If not... what's the best way to solve the problem?
推荐答案
预提交具有迁移模式";用于运行其他现有的钩子框架.沙哑的钩子实现似乎不太聪明,无法检测到您正在运行的钩子-他们基于正在执行的文件名
pre-commit has a "migration mode" for running other existing hook frameworks. It seems that husky is slightly too clever in their hook implementation for detecting what hook you're running -- they base it on the filename that's being executed
预提交的迁移模式的工作方式是将任何现有的钩子脚本(在这种情况下,是由赫斯基写到 .git/hooks/pre-commit
的钩子脚本)并添加扩展名 .legacy
.然后在执行期间运行该脚本.
the way pre-commit's migration mode works is it takes any existing hook script (in this case, the hook script written by husky to .git/hooks/pre-commit
) and adds the extension .legacy
. then during execution it runs that script.
但是令人讨厌的是,它似乎正在运行 pre-commit.legacy
钩子(!)
but to husky, it looks like the pre-commit.legacy
hook is running (!)
一个小技巧是在 package.json
中定义 pre-commit.legacy
,这似乎可行:
a little hack is to define pre-commit.legacy
in package.json
which seems to work:
{
"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编写的,它也具有对(包括javascript ))
that said, this seems fragile. pre-commit is designed to support many different programming languages (even though it is written in python, it has native support for 10+ programming languages (including javascript))
第一个替代方法可能是只从 local
提交前的钩子调用沙哑:
A first place replacement might be to just call husky from a local
pre-commit hook:
{
"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支持,只是调用了已经存在的哈斯基钩子
this solution however doesn't take advantage of pre-commit's js support, it just invokes the already-existing husky hooks
更本地化的解决方案是直接使用pre-commit安装js钩子,例如,如果您使用的是eslint:
a more native solution would be to install js hooks directly with pre-commit, for example if you're using 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的作者
disclaimer: I am the author of pre-commit
这篇关于带有预提交的Pylint和带有沙哑的EsLlint的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!