mirror of
https://github.com/woocommerce/woocommerce.git
synced 2025-08-18 10:21:16 +08:00
39 lines
2.1 KiB
Bash
Executable file
39 lines
2.1 KiB
Bash
Executable file
#!/usr/bin/env bash
|
|
. "$(dirname "$0")/_/husky.sh"
|
|
|
|
# The hook documentation: https://git-scm.com/docs/githooks.html#_post_checkout
|
|
CHECKOUT_TYPE=$3
|
|
HEAD_NEW=$2
|
|
HEAD_PREVIOUS=$1
|
|
|
|
whiteColoured='\033[0m'
|
|
orangeColoured='\033[1;33m'
|
|
|
|
# '1' is a branch checkout
|
|
if [ "$CHECKOUT_TYPE" = '1' ]; then
|
|
# Auto-refresh dependencies when switching between branches.
|
|
changedManifests=$( ( git diff --name-only $HEAD_NEW $HEAD_PREVIOUS | grep -E '(pnpm-lock.yaml|composer.lock)$' ) || echo '' )
|
|
if [ -n "$changedManifests" ]; then
|
|
printf "${whiteColoured}The following file(s) in the new branch differs from the original one, dependencies might need to be refreshed:\n"
|
|
printf "${whiteColoured} %s\n" $changedManifests
|
|
printf "${orangeColoured}If you are working on something in this branch, ensure to refresh dependencies with 'pnpm install --frozen-lockfile'\n"
|
|
fi
|
|
|
|
# Auto-switch pnpm versions when switching between branches.
|
|
currentPnpmVersion=$( ( command -v pnpm > /dev/null && pnpm --version 2>/dev/null ) || echo 'n/a' )
|
|
targetPnpmVersion=$( grep packageManager package.json | sed -nr 's/.+packageManager.+pnpm@([[:digit:].]+).+/\1/p' )
|
|
if [ "$currentPnpmVersion" != "$targetPnpmVersion" ]; then
|
|
printf "${orangeColoured}pnpm versions mismatch: in use '$currentPnpmVersion', needed '$targetPnpmVersion'. Enabling corepack for automatic pnpm version switching (see https://nodejs.org/api/corepack.html#corepack for more details).\n"
|
|
command -v corepack > /dev/null && corepack enable pnpm
|
|
fi
|
|
|
|
currentComposerDate=$( ( command -v composer > /dev/null && ( composer --version 2>/dev/null | awk '{print $4}' ) ) || echo '' )
|
|
if [ -n "$currentComposerDate" ]; then
|
|
current=$( php -r "echo date( 'Ymd', strtotime( '$currentComposerDate' ) );" )
|
|
threshold=$( php -r "echo date( 'Ymd', strtotime( '-12 months' ) );" )
|
|
if [ $current -lt $threshold ]; then
|
|
version=$( ( composer --version 2>/dev/null | awk '{print $3}' ) || echo '' )
|
|
printf "${orangeColoured}composer version (v$version build at $currentComposerDate) is older than twelve months, ensure to update it with 'composer self-update'.\n"
|
|
fi
|
|
fi
|
|
fi
|