diff --git a/zsh-nvm.plugin.zsh b/zsh-nvm.plugin.zsh index a672ed7..c14ebee 100644 --- a/zsh-nvm.plugin.zsh +++ b/zsh-nvm.plugin.zsh @@ -38,11 +38,20 @@ _zsh_nvm_global_binaries() { _zsh_nvm_load() { + if [[ "$NVM_CACHE_LOAD" == true ]]; then + # Strip out the old cached version of node that we were using. + export PATH="$(sed -e 's|[^:]*versions/node[^:]*[:]||g' <<< "$PATH")" + fi + # Source nvm (check if `nvm use` should be ran after load) if [[ "$NVM_NO_USE" == true ]]; then source "$NVM_DIR/nvm.sh" --no-use else source "$NVM_DIR/nvm.sh" + if [[ "$NVM_CACHE_LOAD" == true ]]; then + export NVM_CACHE_LOAD_PATH_NVM="$(awk -F ':' '{ print $1 }' <<< "$PATH")" + echo "$NVM_CACHE_LOAD_PATH_NVM" > "${HOME}/.zsh_nvm_cache" + fi fi # Rename main nvm function @@ -72,6 +81,14 @@ _zsh_nvm_load() { } _zsh_nvm_lazy_load() { + if [[ "$NVM_CACHE_LOAD" == true ]] && [[ -s "${HOME}/.zsh_nvm_cache" ]]; then + export NVM_CACHE_LOAD_PATH_NVM="$(cat "${HOME}/.zsh_nvm_cache")" + + # Add it to path if it doesn't already exist. + if [ -d "$NVM_CACHE_LOAD_PATH_NVM" ] && [[ ":$PATH:" != *":$NVM_CACHE_LOAD_PATH_NVM:"* ]]; then + export PATH="${NVM_CACHE_LOAD_PATH_NVM}${PATH:+":$PATH"}" + fi + fi # Get all global node module binaries including node # (only if NVM_NO_USE is off)