From 5e00422a28700083d1de108d2d480228d836f85b Mon Sep 17 00:00:00 2001 From: Ismo Vuorinen Date: Wed, 24 May 2023 14:22:08 +0300 Subject: [PATCH] feat(zsh): Updated nvm-auto-use Source: https://github.com/nvm-sh/nvm#zsh --- config/functions | 2 +- .../nvm-auto-use/nvm-auto-use.plugin.zsh | 27 +++++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) create mode 100644 config/zsh/plugins/nvm-auto-use/nvm-auto-use.plugin.zsh diff --git a/config/functions b/config/functions index d52d118..5b167eb 100755 --- a/config/functions +++ b/config/functions @@ -89,7 +89,6 @@ x-default-antigen-bundles() antigen bundle command-not-found antigen bundle ssh-agent antigen bundle MichaelAquilina/zsh-you-should-use - antigen bundle Sparragus/zsh-auto-nvm-use antigen bundle jreese/zsh-titles antigen bundle unixorn/autoupdate-antigen.zshplugin antigen bundle zsh-users/zsh-completions @@ -101,6 +100,7 @@ x-default-antigen-bundles() have docker-compose && antigen bundle sroze/docker-compose-zsh-plugin have jq && antigen bundle reegnz/jq-zsh-plugin have nvm && antigen bundle nvm + have nvm && antigen bundle $DOTFILES/config/zsh/plugins/nvm-auto-use have php && antigen bundle php have python && antigen bundle MichaelAquilina/zsh-autoswitch-virtualenv have rvm && antigen bundle unixorn/rvm-plugin diff --git a/config/zsh/plugins/nvm-auto-use/nvm-auto-use.plugin.zsh b/config/zsh/plugins/nvm-auto-use/nvm-auto-use.plugin.zsh new file mode 100644 index 0000000..72ffc48 --- /dev/null +++ b/config/zsh/plugins/nvm-auto-use/nvm-auto-use.plugin.zsh @@ -0,0 +1,27 @@ +# shellcheck shell=zsh +# Source: https://github.com/nvm-sh/nvm#zsh +# place this after nvm initialization! +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 +