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?
Pylint with pre-commit and EsLlint with husky
7.5k Views Asked by Aleksandr Zakharov At
2
There are 2 best solutions below
0

A solution that works better for me, with the latest version of pre-commit (2.14.0) and husky (7.0.2) is to integrate pre-commit into husky, rather than the other way around.
In the ./husky/_/pre-commit
file, I added pre-commit run
.
See below:
#!/bin/sh
. "$(dirname "$0")/_/husky.sh"
npx lint-staged
npx pretty-quick --staged
pre-commit run
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
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.but to husky, it looks like the
pre-commit.legacy
hook is running (!)a little hack is to define
pre-commit.legacy
inpackage.json
which seems to work:package.json
.pre-commit-config.yaml
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))
A first place replacement might be to just call husky from a
local
pre-commit hook:package.json
.pre-commit-config.yaml
execution
this solution however doesn't take advantage of pre-commit's js support, it just invokes the already-existing husky hooks
a more native solution would be to install js hooks directly with pre-commit, for example if you're using eslint:
disclaimer: I am the author of pre-commit