v-moe
v-moe

Reputation: 1443

Conditionally running git hooks with husky

We have a frontend webpack powered project running that has husky (it's yorkie to be precise since we use lerna). We have a very big collection of unit test suites, so we would like to improve the pre-commit hooks to not run the tests when only non-code files have been changed, e.g. config.json or README.md. Does anybody know a ready-made solution for that or do we have to write our own shell script that checks the git status? Thankful for links, patterns or best practices advice.

Upvotes: 1

Views: 2607

Answers (1)

v-moe
v-moe

Reputation: 1443

Since we couldn't find any ready-made solution we solved it like this:

# check-files.sh

code_files_count=`git --no-pager diff --name-only --cached |
grep -E "packages\/(?:package1|package2)\/.+\.(?:js|ts|vue|scss|html)$" |
wc -l`
        
if [ $code_files_count == 0 ]
then
    exit 0
fi
        
exit 1

And then in package.json

"precommit": "./check-files.sh || npm run test:unit"

Upvotes: 3

Related Questions