Avoid Husky from being triggered for changes on every unrelated directory in monorepo project

Viewed 1030

I'm using husky in my lerna monorepo project which has more than one directory (project)

.husky
front
├── package.json
app
└── package.json

on hook prepush I launch my test on front/ and app/ with yarn test.

I would like to have the test for a folder (project) only when the code inside change. Like github Actions.

on:
    paths:
      - 'front/**'

do there is a way in Husky ?

1 Answers

I found a hack

  • I get all the file change in the all commits
  • I check for my folders
  • And trigger a command contextually
// ~/.husky/pre-push

#!/bin/sh
. "$(dirname $0)/_/husky.sh"

declare IS_APP_FOLDER_CHANGE=$(git show --name-only -r --stat --oneline HEAD^^..HEAD | grep 'app/')

echo "IS_APP_FOLDER_CHANGE  --> + $IS_APP_FOLDER_CHANGE"

if [[ $IS_APP_FOLDER_CHANGE ]]; then cd app && yarn test --watchAll=false && cd ..; fi
Related