Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add NVM node precedence if available #164

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

verebes1
Copy link

Feature

This PR makes use of NVM if it is available first before trying other node binaries. For example if you use a different node version based on your project using .nvmrc then the pre-commit hook will automatically pick that up first and use that node version for running the pre-commit commands.

How to test

  1. Create a new project using npm init or use an existing project
  2. Add a new .nvmrc file with the desired node version for example 18.16.0 you can use the following command:
    echo 18.16.0 > .nvmrc
  3. Install pre-commit npm i pre-commit -D
  4. Navigate to the node_modules/pre-commit folder and edit the hook file and add the following contents
#!/usr/bin/env bash

HAS_NODE=`which node 2> /dev/null || which nodejs 2> /dev/null || which iojs 2> /dev/null`

#
# There are some issues with Source Tree because paths are not set correctly for
# the given environment. Sourcing the bash_profile seems to resolve this for bash users,
# sourcing the zshrc for zshell users.
#
# https://answers.atlassian.com/questions/140339/sourcetree-hook-failing-because-paths-don-t-seem-to-be-set-correctly
#
function source_home_file {
  file="$HOME/$1"
  [[ -f "${file}" ]] && source "${file}"
}

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`
LOCAL="/usr/local/bin/node"
BINARY=

#
# Figure out which binary we need to use for our script execution.
#
if [[ -n "$NVMNODE" ]]; then
  BINARY="$NVMNODE"
elif [[ -n "$NODE" ]]; then
  BINARY="$NODE"
elif [[ -n "$NODEJS" ]]; then
  BINARY="$NODEJS"
elif [[ -n "$IOJS" ]]; then
  BINARY="$IOJS"
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 affects
# and see if it works in the current environment
#
if [[ $* == *--dry-run* ]]; then
  if [[ -z "$BINARY" ]]; then
    exit 1
  fi
else
  "$BINARY" "$("$BINARY" -e "console.log(require.resolve('pre-commit'))")"
fi

Add pre-commit hooks to your package.json file as per the Readme of the repo and it should use your desired node version.

Added the NVM node as the first one to try if NVM is available.
It uses .nvmrc if it is available
@LecrisUT LecrisUT mentioned this pull request Jul 6, 2023
13 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant