From 826f97c62b3f14b36db3d12944a4cf5976b45894 Mon Sep 17 00:00:00 2001 From: Matthias Kretschmann Date: Tue, 5 Sep 2023 23:02:15 +0100 Subject: [PATCH] run nvm use automatically in a directory with a .nvmrc file --- exports | 32 ++++++++++++++++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-) diff --git a/exports b/exports index f98c66a..64f930f 100644 --- a/exports +++ b/exports @@ -33,7 +33,35 @@ export CDPATH=$CDPATH:~/Code # nvm export NVM_DIR="$HOME/.nvm" -[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" +[ -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 + +# Calling nvm use automatically in a directory with a .nvmrc file +# https://github.com/nvm-sh/nvm#zsh +autoload -U add-zsh-hook + +load-nvmrc() { + local nvmrc_path + nvmrc_path="$(nvm_find_nvmrc)" + + if [ -n "$nvmrc_path" ]; then + local nvmrc_node_version + nvmrc_node_version=$(nvm version "$(cat "${nvmrc_path}")") + + if [ "$nvmrc_node_version" = "N/A" ]; then + nvm install + elif [ "$nvmrc_node_version" != "$(nvm version)" ]; then + nvm use + fi + elif [ -n "$(PWD=$OLDPWD nvm_find_nvmrc)" ] && [ "$(nvm version)" != "$(nvm version default)" ]; then + echo "Reverting to nvm default version" + nvm use default + fi +} + +add-zsh-hook chpwd load-nvmrc +load-nvmrc + # defer loading until needed for faster shell startup #declare -a NODE_GLOBALS=(`find ~/.nvm/versions/node -maxdepth 3 -type l -wholename '*/bin/*' | xargs -n1 basename | sort | uniq`) @@ -51,7 +79,7 @@ export NVM_DIR="$HOME/.nvm" #done # pip should only run if there is a virtualenv currently activated -export PIP_REQUIRE_VIRTUALENV=true +# export PIP_REQUIRE_VIRTUALENV=true # cache pip-installed packages to avoid re-downloading export PIP_DOWNLOAD_CACHE=$HOME/.pip/cache