Search code examples
node.jsgithuskygit-husky

Run Husky hooks only for specific folder changes


Is it possible to run only git hooks for when only files in certain folder are changed??

I have monorepo with backend and frontend. I would like to run husky hook only when the files in frontend got changed. Otherwise it should not be triggered and not run linting tests etc... It's irritating backend developers


Solution

  • With combination on lint-staged I managed to run husky hooks checks on only desired folder changes.

    module.exports = {
      '*': (files) => {
        const isAppFolderChanges = files.some((fileName) =>
          fileName.includes('/app/'),
        )
        const scripts = [
          'npm run lint-fix',
          './node_modules/.bin/pretty-quick --staged',
          'npm test',
        ]
    
        return isAppFolderChanges ? scripts : []
      },
    }