2024-09-17 10:34:40 +00:00
#!/usr/bin/env bash
. "$(dirname "$0")/_/husky.sh"
2024-09-18 09:10:11 +00:00
# The hook documentation: https://git-scm.com/docs/githooks.html#_post_checkout
2024-09-17 10:34:40 +00:00
CHECKOUT_TYPE=$3
2024-09-18 09:10:11 +00:00
HEAD_NEW=$2
HEAD_PREVIOUS=$1
2024-09-17 10:34:40 +00:00
whiteColoured='\033[0m'
2024-09-18 09:10:11 +00:00
orangeColoured='\033[1;33m'
2024-09-17 10:34:40 +00:00
2024-09-18 09:10:11 +00:00
# '1' is a branch checkout
2024-09-17 10:34:40 +00:00
if [ "$CHECKOUT_TYPE" = '1' ]; then
# Prompt about pnpm versions mismatch when switching between branches.
2024-09-18 09:10:11 +00:00
currentPnpmVersion=$( ( command -v pnpm > /dev/null && pnpm -v 2>/dev/null ) || echo 'n/a' )
2024-09-17 10:34:40 +00:00
targetPnpmVersion=$( grep packageManager package.json | sed -nr 's/.+packageManager.+pnpm@([[:digit:].]+).+/\1/p' )
if [ "$currentPnpmVersion" != "$targetPnpmVersion" ]; then
2024-09-18 09:10:11 +00:00
printf "${orangeColoured}pnpm versions mismatch: in use '$currentPnpmVersion', needed '$targetPnpmVersion'. If you are working on something in this branch, here are some hints on how to solve this:\n"
printf "${orangeColoured}* actualize environment: 'nvm use && pnpm -v' (the most common case)\n"
printf "${orangeColoured}* install: 'npm install -g pnpm@$targetPnpmVersion'\n"
2024-09-17 10:34:40 +00:00
fi
# Auto-refresh dependencies when switching between branches.
2024-09-18 09:10:11 +00:00
changedManifests=$( ( git diff --name-only $HEAD_NEW $HEAD_PREVIOUS | grep -E '(package.json|pnpm-lock.yaml|pnpm-workspace.yaml|composer.json|composer.lock)$' ) || echo '' )
2024-09-17 10:34:40 +00:00
if [ -n "$changedManifests" ]; then
2024-09-18 09:10:11 +00:00
printf "${whiteColoured}The following file(s) in the new branch differs from the original one, dependencies might need to be refreshed:\n"
2024-09-17 10:34:40 +00:00
printf "${whiteColoured} %s\n" $changedManifests
2024-09-18 09:10:11 +00:00
printf "${orangeColoured}If you are working on something in this branch, ensure to refresh dependencies with 'pnpm install --frozen-lockfile'\n"
2024-09-17 10:34:40 +00:00
fi
fi