nvm.plugin.zsh 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. elif (( $+commands[brew] )); then
  8. NVM_HOMEBREW="${NVM_HOMEBREW:-${HOMEBREW_PREFIX:-$(brew --prefix)}/opt/nvm}"
  9. if [[ -d "$NVM_HOMEBREW" ]]; then
  10. export NVM_DIR="$NVM_HOMEBREW"
  11. fi
  12. fi
  13. fi
  14. # Don't try to load nvm if command already available
  15. # Note: nvm is a function so we need to use `which`
  16. which nvm &>/dev/null && return
  17. # TODO: 2022-11-11: Remove soft-deprecate options
  18. if (( ${+NVM_LAZY} + ${+NVM_LAZY_CMD} + ${+NVM_AUTOLOAD} )); then
  19. # Get list of NVM_* variable settings defined
  20. local -a used_vars
  21. used_vars=(${(o)parameters[(I)NVM_(AUTOLOAD|LAZY|LAZY_CMD)]})
  22. # Nicely print the list in the style `var1, var2 and var3`
  23. echo "${fg[yellow]}[nvm plugin] Variable-style settings are deprecated. Instead of ${(j:, :)used_vars[1,-2]}${used_vars[-2]+ and }${used_vars[-1]}, use:\n"
  24. if (( $+NVM_AUTOLOAD )); then
  25. echo " zstyle ':omz:plugins:nvm' autoload yes"
  26. zstyle ':omz:plugins:nvm' autoload yes
  27. fi
  28. if (( $+NVM_LAZY )); then
  29. echo " zstyle ':omz:plugins:nvm' lazy yes"
  30. zstyle ':omz:plugins:nvm' lazy yes
  31. fi
  32. if (( $+NVM_LAZY_CMD )); then
  33. echo " zstyle ':omz:plugins:nvm' lazy-cmd $NVM_LAZY_CMD"
  34. zstyle ':omz:plugins:nvm' lazy-cmd $NVM_LAZY_CMD
  35. fi
  36. echo "$reset_color"
  37. unset used_vars NVM_AUTOLOAD NVM_LAZY NVM_LAZY_CMD
  38. fi
  39. if zstyle -t ':omz:plugins:nvm' lazy; then
  40. # Call nvm when first using nvm, node, npm, pnpm, yarn or other commands in lazy-cmd
  41. zstyle -a ':omz:plugins:nvm' lazy-cmd nvm_lazy_cmd
  42. eval "
  43. function nvm node npm pnpm yarn $nvm_lazy_cmd {
  44. unfunction nvm node npm pnpm yarn $nvm_lazy_cmd
  45. # Load nvm if it exists in \$NVM_DIR
  46. [[ -f \"\$NVM_DIR/nvm.sh\" ]] && source \"\$NVM_DIR/nvm.sh\"
  47. \"\$0\" \"\$@\"
  48. }
  49. "
  50. unset nvm_lazy_cmd
  51. elif [[ -f "$NVM_DIR/nvm.sh" ]]; then
  52. # Load nvm if it exists in $NVM_DIR
  53. source "$NVM_DIR/nvm.sh"
  54. else
  55. return
  56. fi
  57. # Autoload nvm when finding a .nvmrc file in the current directory
  58. # Adapted from: https://github.com/nvm-sh/nvm#zsh
  59. if zstyle -t ':omz:plugins:nvm' autoload; then
  60. function load-nvmrc {
  61. local node_version="$(nvm version)"
  62. local nvmrc_path="$(nvm_find_nvmrc)"
  63. local nvm_silent=""
  64. zstyle -t ':omz:plugins:nvm' silent-autoload && nvm_silent="--silent"
  65. if [[ -n "$nvmrc_path" ]]; then
  66. local nvmrc_node_version=$(nvm version $(cat "$nvmrc_path" | tr -dc '[:print:]'))
  67. if [[ "$nvmrc_node_version" = "N/A" ]]; then
  68. nvm install
  69. elif [[ "$nvmrc_node_version" != "$node_version" ]]; then
  70. nvm use $nvm_silent
  71. fi
  72. elif [[ "$node_version" != "$(nvm version default)" ]]; then
  73. if [[ -z $nvm_silent ]]; then
  74. echo "Reverting to nvm default version"
  75. fi
  76. nvm use default $nvm_silent
  77. fi
  78. }
  79. autoload -U add-zsh-hook
  80. add-zsh-hook chpwd load-nvmrc
  81. load-nvmrc
  82. fi
  83. # Load nvm bash completion
  84. for nvm_completion in "$NVM_DIR/bash_completion" "$NVM_HOMEBREW/etc/bash_completion.d/nvm"; do
  85. if [[ -f "$nvm_completion" ]]; then
  86. # Load bashcompinit
  87. autoload -U +X bashcompinit && bashcompinit
  88. # Bypass compinit call in nvm bash completion script. See:
  89. # https://github.com/nvm-sh/nvm/blob/4436638/bash_completion#L86-L93
  90. ZSH_VERSION= source "$nvm_completion"
  91. break
  92. fi
  93. done
  94. unset NVM_HOMEBREW nvm_completion