derekstavis / plugin-nvm

Node version manager wrapper for Fish shell
MIT License
233 stars 12 forks source link

Question about lazy loading and `init_nvm` event #15

Closed stv8 closed 3 years ago

stv8 commented 4 years ago

I am a new fish user and first off thanks for the plugin!

I've been trying to find a way to lazy load on a new shell, but I can't seem to find where or how the event init_nvm is emitted in the function in init.sh.

I think I found a work-around where I can lazy load nvm in fish. What do you think?

function init -a path --on-event load_nvm
  if type -q fenv
    set -q NVM_DIR; or set -gx NVM_DIR ~/.nvm
    set -g nvm_prefix $NVM_DIR

    type -q brew;
      and test -e (brew --prefix)/Cellar/nvm;
        and set -g nvm_prefix (brew --prefix nvm)

    fenv source $nvm_prefix/nvm.sh >/dev/null ^&1
  end

end
function nvm -d "Node version manager"
  emit load_nvm
  if test -e $nvm_prefix/nvm.sh
    if not type -q fenv
      echo "You need to install foreign-env plugin"
      return 1
    end

    fenv source $nvm_prefix/nvm.sh --no-use ';' nvm $argv
  else
    echo "You need to install nvm itself (see https://github.com/creationix/nvm#installation)"
    return 1
  end
end