Browse Source

node: Cleanup and optimize 'node' module

Changes:
- Simplify nodenv and nvm initialization
- Check for availability of `nodenv` or `nvm` function instead of command
- Unset local variables outside condition block
master
Indrajit Raychaudhuri 4 years ago committed by Indrajit Raychaudhuri
parent
commit
887ed84619
  1. 36
      modules/node/init.zsh

36
modules/node/init.zsh

@ -8,33 +8,33 @@
# Indrajit Raychaudhuri <irc@indrajit.com> # Indrajit Raychaudhuri <irc@indrajit.com>
# #
# Possible lookup locations. # Possible lookup locations for manually installed nodenv and nvm.
local_nodenv_paths=({$NODENV_ROOT,{$XDG_CONFIG_HOME/,$HOME/.}nodenv}/bin/nodenv(N)) local_nodenv_paths=({$NODENV_ROOT,{$XDG_CONFIG_HOME/,$HOME/.}nodenv}/bin/nodenv(N))
local_nvm_paths=({$NVM_DIR,{$XDG_CONFIG_HOME/,$HOME/.}nvm}/nvm.sh(N)) local_nvm_paths=({$NVM_DIR,{$XDG_CONFIG_HOME/,$HOME/.}nvm}/nvm.sh(N))
# Load manually installed nodenv into the shell session. # Load manually installed or package manager installed nodenv into the shell
if [[ -s ${local_nodenv::=$local_nodenv_paths[1]} ]]; then # session.
path=("$local_nodenv:h" $path) if (( $#local_nodenv_paths || $+commands[nodenv] )); then
eval "$(nodenv init - --no-rehash zsh)"
unset local_nodenv{,_paths}
# Load package manager installed nodenv into the shell session. # Ensure manually installed nodenv is added to path when present.
elif (( $+commands[nodenv] )); then [[ -s $local_nodenv_paths[1] ]] && path=($local_nodenv_paths[1]:h $path)
eval "$(nodenv init - --no-rehash zsh)"
# Load manually installed NVM into the shell session. eval "$(nodenv init - zsh)"
elif [[ -s ${local_nvm::=$local_nvm_paths[1]} ]]; then
source "$local_nvm" --no-use
unset local_nvm{,_paths}
# Load package manager installed NVM into the shell session. # Load manually installed nvm into the shell session.
elif (( $#local_nvm_paths )); then
source "$local_nvm_paths[1]" --no-use
# Load package manager installed nvm into the shell session.
elif (( $+commands[brew] )) \ elif (( $+commands[brew] )) \
&& [[ -d "${nvm_prefix::="$(brew --prefix nvm 2> /dev/null)"}" ]]; then && [[ -d "${nvm_path::="$(brew --prefix 2> /dev/null)"/opt/nvm}" ]]; then
source "$nvm_prefix/nvm.sh" --no-use source "$nvm_path/nvm.sh" --no-use
unset nvm_prefix fi
unset local_n{odenv,vm}_paths nvm_path
# Return if requirements are not found. # Return if requirements are not found.
elif (( ! $+commands[node] )); then if (( ! $+commands[node] && ! $#functions[(i)n(odenv|vm)] )); then
return 1 return 1
fi fi

Loading…
Cancel
Save