tarsisexistence / monoreact

📦 React workspaces implementation
MIT License
19 stars 1 forks source link

Configure husky (git hooks) in submodules #192

Open tarsisexistence opened 4 years ago

tarsisexistence commented 4 years ago

Since it is a well-known issue, we need to come up with idea, how to make it work https://github.com/typicode/husky/issues/404

By the way, I do agree with this comment. This is not okay. https://github.com/typicode/husky/issues/404#issuecomment-457979940

witthong commented 4 years ago

find-path-to-git-hooks-directory-on-the-shell https://stackoverflow.com/questions/14073053/find-path-to-git-hooks-directory-on-the-shell

abrioy commented 4 years ago

Here's the workaround we use to trick Husky into working with our submodule (on linux) :

The problem is that every developer needs to run the node command. To work around that, we set a yarn postinstall script in our package.json : "postinstall": "(export GIT_DIR=$(cd src/app/commons && git rev-parse --git-dir) ; test -f $GIT_DIR/hooks/pre-commit || node node_modules/husky/husky.js install)"

bertho-zero commented 3 years ago

@abrioy It almost works, there is a ; after GIT_DIR=... in step 3 which makes GIT_DIR not available for the next command. You must either remove the ; or add an export in front of it.

Thanks for the solution, it works wonderfully !

bertho-zero commented 3 years ago

I just switched to husky@5 and it's different to enable hooks in the submodule, In the new version husky modifies the path of the hooks directory, we can do it directly with the git command:

cd public && git config core.hooksPath ../.husky

This script helps you add the hooks in the main repository and a submodule public:

"postinstall": "husky install && cd public && git config core.hooksPath ../.husky"
DiamondeX commented 3 years ago

Here's the workaround we use to trick Husky into working with our submodule (on linux) :

  • The submodule is located in src/app/submodule
  • Create an empty file in src/app/submodule/.huskyrc or else the husky.sh script won't work at all
  • run GIT_DIR=$(cd src/app/submodule && git rev-parse --git-dir) node node_modules/husky/husky.js install
  • The Husky files should be properly created in .git/modules/submodule/hooks and the hooks should work nicely from the submodule and use the configuration from the parent repository.

The problem is that every developer needs to run the node command. To work around that, we set a yarn postinstall script in our package.json : "postinstall": "(export GIT_DIR=$(cd src/app/commons && git rev-parse --git-dir) ; test -f $GIT_DIR/hooks/pre-commit || node node_modules/husky/husky.js install)"

It was a little bit different for me.

  1. By me it works without src/app/submodule/.huskyrc, but it didn't without one of these: .huskyrc.js, huskyrc.cjs, husky.config.js, husky.config.cjs. They are mentioned in the config checking block of husky.sh. I've choosed .huskyrc.js and then I had to put there something like:

    module.exports = {
    hooks: {
    'pre-commit': '<your-precommit-command>',
    },
    }
  2. I'd better check $GIT_DIR/hooks/husky.sh in "postinstall", because $GIT_DIR/hooks/pre-commit may be created in some other way, not by husky. And I think that path in "postinstall" should match submodule path. So, with all corrections it should be like this in your case: "postinstall": "(export GIT_DIR=$(cd src/app/submodule && git rev-parse --git-dir) ; test -f $GIT_DIR/hooks/husky.sh || node node_modules/husky/husky.js install)"

GuilhermeBorges3Ddev commented 2 years ago

Here's the workaround we use to trick Husky into working with our submodule (on linux) :

  • The submodule is located in src/app/submodule
  • Create an empty file in src/app/submodule/.huskyrc or else the husky.sh script won't work at all
  • run GIT_DIR=$(cd src/app/submodule && git rev-parse --git-dir) node node_modules/husky/husky.js install
  • The Husky files should be properly created in .git/modules/submodule/hooks and the hooks should work nicely from the submodule and use the configuration from the parent repository.

The problem is that every developer needs to run the node command. To work around that, we set a yarn postinstall script in our package.json : "postinstall": "(export GIT_DIR=$(cd src/app/commons && git rev-parse --git-dir) ; test -f $GIT_DIR/hooks/pre-commit || node node_modules/husky/husky.js install)"

How can i redo those commands using Windows?

cengit commented 2 years ago

I just switched to husky@5 and it's different to enable hooks in the submodule, In the new version husky modifies the path of the hooks directory, we can do it directly with the git command:

cd public && git config core.hooksPath ../.husky

This script helps you add the hooks in the main repository and a submodule public:

"postinstall": "husky install && cd public && git config core.hooksPath ../.husky"

this works for me very well, thank you very much!!!

if you have multiple submodlues you can add a shell script to do this. something like this:

package.json

"scripts": {
    "postinstall": "sh script/submodule-husky-hook-path.sh"
  }

submodule-husky-hook-path.sh

#!/usr/bin/env bash
cd module_a && git config core.hooksPath ../.husky
cd ..
cd module_b && git config core.hooksPath ../.husky
cd ..
cd module_c && git config core.hooksPath ../.husky