From 1388f58a628a83c131190f7448411a47c2b837a5 Mon Sep 17 00:00:00 2001 From: David Verbenyi Date: Mon, 22 May 2023 09:03:56 +0100 Subject: [PATCH] Add NVM node precedence if available Added the NVM node as the first one to try if NVM is available. It uses .nvmrc if it is available --- hook | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/hook b/hook index 1bf23b8..2470287 100755 --- a/hook +++ b/hook @@ -18,6 +18,15 @@ if [[ -z "$HAS_NODE" ]]; then source_home_file ".bash_profile" || source_home_file ".zshrc" || source_home_file ".bashrc" || true fi +[ "$NVM_DIR" = "" ] && export NVM_DIR="$HOME/.nvm" +if [ -f "$NVM_DIR/nvm.sh" ]; then + [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm + [ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion" # This loads nvm bash_completion + nvm install + nvm use +fi + +NVMNODE=`nvm which node 2> /dev/null` NODE=`which node 2> /dev/null` NODEJS=`which nodejs 2> /dev/null` IOJS=`which iojs 2> /dev/null` @@ -27,7 +36,9 @@ BINARY= # # Figure out which binary we need to use for our script execution. # -if [[ -n "$NODE" ]]; then +if [[ -n "$NVMNODE" ]]; then + BINARY="$NVMNODE" +elif [[ -n "$NODE" ]]; then BINARY="$NODE" elif [[ -n "$NODEJS" ]]; then BINARY="$NODEJS" @@ -37,8 +48,12 @@ elif [[ -x "$LOCAL" ]]; then BINARY="$LOCAL" fi +if [ ! -f "$BINARY" ]; then + echo "something wrong: node not exist" + exit 1 +fi # -# Add --dry-run cli flag support so we can execute this hook without side effects +# Add --dry-run cli flag support so we can execute this hook without side affects # and see if it works in the current environment # if [[ $* == *--dry-run* ]]; then @@ -47,4 +62,4 @@ if [[ $* == *--dry-run* ]]; then fi else "$BINARY" "$("$BINARY" -e "console.log(require.resolve('pre-commit'))")" -fi +fi \ No newline at end of file