From 5918e5df237a8f2fd2b2f13a2d50260cd663fde9 Mon Sep 17 00:00:00 2001 From: Florian Sihler Date: Fri, 24 Nov 2023 14:29:55 +0100 Subject: [PATCH] Robustify Git-Hooks for use with GitHub Desktop (#499) git: try to make pre-push hook more robust with sourcing npm --- .githooks/pre-push | 29 +++++++++++++++++++++++++---- 1 file changed, 25 insertions(+), 4 deletions(-) diff --git a/.githooks/pre-push b/.githooks/pre-push index e8a9974cbd..111e89281c 100755 --- a/.githooks/pre-push +++ b/.githooks/pre-push @@ -2,9 +2,30 @@ set -eu -if ! [ -x "$(command -v npm)" ]; then - echo 'Error: npm not found. Make it available to the host shell (e.g., with "nvm use --lts").' - exit 2 +NPM_CMD="npm" + +if ! (type $NPM_CMD >> /dev/null); then + echo "npm not found, trying to make it available using nvm..." + if type nvm >> /dev/null; then + echo "nvm found, using it to install the latest lts node" + nvm use --lts + else + echo "nvm not found, trying to make it available using the nvm.sh" + # try to make it available based on https://github.com/typicode/husky/issues/912#issuecomment-817522060 + export NVM_DIR="$HOME/.nvm/nvm.sh" + . "$(dirname $NVM_DIR)/nvm.sh" + + export NVM_DIR="$HOME/.nvm" + a=$(nvm ls --no-colors | grep 'node') + v=$(echo "$a" | sed -E 's/.*\(-> ([^ ]+).*/\1/') + + export PATH="$NVM_DIR/versions/node/$v/bin:$PATH" + + if ! (type $NPM_CMD >> /dev/null); then + echo "no variant of npm or nvm found, trying to use the npm.cmd" + NPM_CMD="npm.cmd" + fi + fi fi @@ -37,7 +58,7 @@ if [ -n "$(git status --porcelain)" ]; then fi echo "Linting project (local mode)..." -npm run lint-local +$NPM_CMD run lint-local # shellcheck disable=SC2124 # we want the argument splitting