Development
Git hooks

Git hooks

This page offers a lsit of hooks that can optionally be added to the .git/hooks folder as file to run QoL hooks while working on the development of shophub.

Adding a new script

As already mentioned, all scripts should be added with the correct name into the .git/hooks folder. The file itself should also have no suffix. To give permissions to the file to be executed you need to run the following command while being in the same directory as the file:

chmod +x <file_name>

List of currently available scripts

post-checkout

The post-checkout script is used to check the code each time a branch is changed via git checkout. If the script detects any changes in the types package, the user is prompted with a question, if the types package should be rebuild.

This prevents the necessity, that the user manually needs to run yarn types:build after changing the current branch locally.

post-checkout
#!/bin/bash
 
folder="./packages/types/src/"
 
echo_green() {
  echo -e "\033[0;32m$1\033[0m"
}
 
echo_blue() {
  echo -e "\033[0;34m$1\033[0m"
}
 
ask_to_rebuild() {
    if [ -z $SSH_ASKPASS ]
    then
    exec < /dev/tty
    read -p $'\033[0;34mRebuild types package? (Y/n)\033[0m ' CHOICE
    else
    CHOICE=$($SSH_ASKPASS "Rebuild types package? (Y/n)")
    fi
    echo $CHOICE
    if [ "$CHOICE" = "y" ] || [ "$CHOICE" = "Y" ] || [ "$CHOICE" = "" ]; then
        # Replace `your-command-here` with the command you want to run
        npm run types:build
    fi
 
    exit 0
}
 
# Check if types package changed between checkout
if [[ $(git diff HEAD@{1}..HEAD@{0} -- "${folder}" | wc -l) -gt 0 ]]; then
  echo_blue "Detected changes in types package."
  ask_to_rebuild
else
  echo_green "No changes detected in types package."
  exit 0
fi