nvm.plugin.zsh 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. # See https://github.com/nvm-sh/nvm#installation-and-update
  2. if [[ -z "$NVM_DIR" ]]; then
  3. if [[ -d "$HOME/.nvm" ]]; then
  4. export NVM_DIR="$HOME/.nvm"
  5. elif [[ -d "${XDG_CONFIG_HOME:-$HOME/.config}/nvm" ]]; then
  6. export NVM_DIR="${XDG_CONFIG_HOME:-$HOME/.config}/nvm"
  7. fi
  8. fi
  9. # Don't try to load nvm if command already available
  10. which nvm &> /dev/null && return
  11. if [[ -f "$NVM_DIR/nvm.sh" ]]; then
  12. # Load nvm if it exists in $NVM_DIR
  13. source "$NVM_DIR/nvm.sh" ${NVM_LAZY+"--no-use"}
  14. else
  15. # Otherwise try to load nvm installed via Homebrew
  16. # User can set this if they have an unusual Homebrew setup
  17. NVM_HOMEBREW="${NVM_HOMEBREW:-/usr/local/opt/nvm}"
  18. # Load nvm from Homebrew location if it exists
  19. if [[ -f "$NVM_HOMEBREW/nvm.sh" ]]; then
  20. source "$NVM_HOMEBREW/nvm.sh" ${NVM_LAZY+"--no-use"}
  21. else
  22. # Exit the plugin if we couldn't find nvm
  23. return
  24. fi
  25. fi
  26. # Call nvm when first using node, npm or yarn
  27. if (( $+NVM_LAZY )); then
  28. function node npm yarn $NVM_LAZY_CMD {
  29. unfunction node npm yarn $NVM_LAZY_CMD
  30. nvm use default
  31. command "$0" "$@"
  32. }
  33. fi
  34. # Autoload nvm when finding a .nvmrc file in the current directory
  35. # Adapted from: https://github.com/nvm-sh/nvm#zsh
  36. if (( $+NVM_AUTOLOAD )); then
  37. load-nvmrc() {
  38. local node_version="$(nvm version)"
  39. local nvmrc_path="$(nvm_find_nvmrc)"
  40. if [[ -n "$nvmrc_path" ]]; then
  41. local nvmrc_node_version=$(nvm version "$(cat "${nvmrc_path}")")
  42. if [[ "$nvmrc_node_version" = "N/A" ]]; then
  43. nvm install
  44. elif [[ "$nvmrc_node_version" != "$node_version" ]]; then
  45. nvm use
  46. fi
  47. elif [[ "$node_version" != "$(nvm version default)" ]]; then
  48. echo "Reverting to nvm default version"
  49. nvm use default
  50. fi
  51. }
  52. autoload -U add-zsh-hook
  53. add-zsh-hook chpwd load-nvmrc
  54. load-nvmrc
  55. fi
  56. # Load nvm bash completion
  57. for nvm_completion in "$NVM_DIR/bash_completion" "$NVM_HOMEBREW/etc/bash_completion.d/nvm"; do
  58. if [[ -f "$nvm_completion" ]]; then
  59. # Load bashcompinit
  60. autoload -U +X bashcompinit && bashcompinit
  61. # Bypass compinit call in nvm bash completion script. See:
  62. # https://github.com/nvm-sh/nvm/blob/4436638/bash_completion#L86-L93
  63. ZSH_VERSION= source "$nvm_completion"
  64. break
  65. fi
  66. done
  67. unset NVM_HOMEBREW NVM_LAZY NVM_AUTOLOAD nvm_completion