From 8b7050f9b1a276b99e3ccb941fd6566f783ff1bf Mon Sep 17 00:00:00 2001 From: Vladimir Reznichenko Date: Tue, 17 Sep 2024 12:34:40 +0200 Subject: [PATCH] Monorepo: git post-checkout hook to verify pnpm version and update dependecies. (#51382) --- .husky/post-checkout | 35 +++++++++++++++++++++++++++++++++++ .npmrc | 2 ++ 2 files changed, 37 insertions(+) create mode 100755 .husky/post-checkout diff --git a/.husky/post-checkout b/.husky/post-checkout new file mode 100755 index 00000000000..cddb5753bc3 --- /dev/null +++ b/.husky/post-checkout @@ -0,0 +1,35 @@ +#!/usr/bin/env bash +. "$(dirname "$0")/_/husky.sh" + +# '1' is branch +CHECKOUT_TYPE=$3 +redColoured='\033[0;31m' +whiteColoured='\033[0m' + +if [ "$CHECKOUT_TYPE" = '1' ]; then + canUpdateDependencies='no' + + # Prompt about pnpm versions mismatch when switching between branches. + currentPnpmVersion=$( ( command -v pnpm > /dev/null && pnpm -v ) || echo 'n/a' ) + targetPnpmVersion=$( grep packageManager package.json | sed -nr 's/.+packageManager.+pnpm@([[:digit:].]+).+/\1/p' ) + if [ "$currentPnpmVersion" != "$targetPnpmVersion" ]; then + printf "${redColoured}pnpm versions mismatch: in use '$currentPnpmVersion', needed '$targetPnpmVersion'. Here some hints how to solve this:\n" + printf "${redColoured}* actualize environment: 'nvm use && pnpm -v' (the most common case)\n" + printf "${redColoured}* install: 'npm install -g pnpm@$targetPnpmVersion'\n" + else + canUpdateDependencies='yes' + fi + + # Auto-refresh dependencies when switching between branches. + changedManifests=$( ( git diff --name-only HEAD ORIG_HEAD | grep -E '(package.json|pnpm-lock.yaml|pnpm-workspace.yaml|composer.json|composer.lock)$' ) || echo '' ) + if [ -n "$changedManifests" ]; then + printf "${whiteColoured}It was a change in the following file(s) - refreshing dependencies:\n" + printf "${whiteColoured} %s\n" $changedManifests + + if [ "$canUpdateDependencies" = 'yes' ]; then + pnpm install --frozen-lockfile + else + printf "${redColoured}Skipping dependencies refresh. Please actualize pnpm version and execute 'pnpm install --frozen-lockfile' manually.\n" + fi + fi +fi diff --git a/.npmrc b/.npmrc index 9d43c15d3cc..a140ea6b576 100644 --- a/.npmrc +++ b/.npmrc @@ -1,3 +1,5 @@ ; adding this as npm 7 automatically installs peer dependencies but pnpm does not auto-install-peers=true strict-peer-dependencies=false +; See https://github.com/pnpm/pnpm/pull/8363 (we adding the setting now, to not miss when migrating to pnpm 9.7+) +manage-package-manager-versions=true