From aa5c6e33370ad4a82a4ff6a0b54d9dd835a0907d Mon Sep 17 00:00:00 2001 From: Luke Childs Date: Tue, 3 Jan 2017 18:04:20 +0700 Subject: [PATCH] Add NVM_AUTO_USE functionality --- zsh-nvm.plugin.zsh | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/zsh-nvm.plugin.zsh b/zsh-nvm.plugin.zsh index 4bcc53d..0496c5f 100644 --- a/zsh-nvm.plugin.zsh +++ b/zsh-nvm.plugin.zsh @@ -147,6 +147,23 @@ _zsh_nvm_revert() { fi } +autoload -U add-zsh-hook +_zsh_nvm_auto_use() { + local node_version="$(nvm version)" + local nvmrc_path="$(nvm_find_nvmrc)" + + if [ -n "$nvmrc_path" ]; then + local nvmrc_node_version=$(nvm version "$(cat "${nvmrc_path}")") + + if [ "$nvmrc_node_version" != "N/A" ] && [ "$nvmrc_node_version" != "$node_version" ]; then + nvm install + fi + elif [ "$node_version" != "$(nvm version default)" ]; then + echo "Reverting to nvm default version" + nvm use default + fi +} + # Don't init anything if this is true (debug/testing only) if [[ "$ZSH_NVM_NO_LOAD" != true ]]; then @@ -158,6 +175,9 @@ if [[ "$ZSH_NVM_NO_LOAD" != true ]]; then # Load it [[ "$NVM_LAZY_LOAD" == true ]] && _zsh_nvm_lazy_load || _zsh_nvm_load + + # Auto use nvm on chpwd + [[ "$NVM_AUTO_USE" == true ]] && add-zsh-hook chpwd _zsh_nvm_auto_use && _zsh_nvm_auto_use fi fi