├── tests ├── loading │ ├── teardown │ ├── Check zsh-nvm wraps nvm │ ├── Check zsh-nvm loads nvm │ ├── Check zsh-nvm installs nvm if it isn't already installed │ ├── Check zsh-nvm is loaded │ ├── Check zsh-nvm exports NVM_DIR if it doesn't exist │ ├── Check zsh-nvm doesn't install nvm if it's already installed │ └── Check zsh-nvm loads node ├── options │ ├── teardown │ ├── NVM_NO_USE │ ├── NVM_LAZY_LOAD adds lazy loader for external yarn │ ├── NVM_LAZY_LOAD │ ├── NVM_LAZY_LOAD_EXTRA_COMMANDS │ ├── NVM_AUTO_USE │ ├── NVM_LAZY_LOAD && NVM_NO_USE │ └── NVM_LAZY_LOAD works when global modules has same name as alias ├── wrapper commands │ ├── teardown │ ├── nvm install │ ├── nvm revert │ └── nvm upgrade └── common.sh ├── .gitignore ├── .github └── FUNDING.yml ├── .travis.yml ├── LICENSE ├── zsh-nvm.plugin.zsh └── README.md /tests/loading/teardown: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | rm -rf ../.nvm 3 | -------------------------------------------------------------------------------- /tests/options/teardown: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | rm -rf ../.nvm 3 | -------------------------------------------------------------------------------- /tests/wrapper commands/teardown: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | rm -rf ../.nvm 3 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | **/.urchin.log 2 | **/.urchin_stdout 3 | .nvm 4 | previous_version 5 | -------------------------------------------------------------------------------- /tests/loading/Check zsh-nvm wraps nvm: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | source ../common.sh 3 | 4 | # Load zsh-nvm 5 | load_zsh_nvm 6 | 7 | # Check nvm is wrapped with our revert command 8 | nvm revert || die "nvm isn't wrapped with our revert command" 9 | -------------------------------------------------------------------------------- /tests/loading/Check zsh-nvm loads nvm: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | source ../common.sh 3 | 4 | # Check nvm isn't already loaded 5 | nvm --version && die "nvm is already loaded" 6 | 7 | # Load zsh-nvm 8 | load_zsh_nvm 9 | 10 | # Check nvm is now loaded 11 | nvm --version || die "nvm wasn't loaded" 12 | -------------------------------------------------------------------------------- /tests/common.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | export test_dir=$(CDPATH= cd -- "$(dirname -- "$0")" && pwd) 3 | export repo_dir="$test_dir/.." 4 | export NVM_DIR="$test_dir/.nvm" 5 | 6 | die () { 7 | echo $@ 8 | exit 1 9 | } 10 | 11 | load_zsh_nvm() { 12 | source "$repo_dir/zsh-nvm.plugin.zsh" 13 | } 14 | -------------------------------------------------------------------------------- /.github/FUNDING.yml: -------------------------------------------------------------------------------- 1 | github: 2 | - 'lukechilds' 3 | custom: 4 | - 'https://blockstream.info/address/1LukeQU5jwebXbMLDVydeH4vFSobRV9rkj' 5 | - 'https://blockstream.info/address/3Luke2qRn5iLj4NiFrvLBu2jaEj7JeMR6w' 6 | - 'https://blockstream.info/address/bc1qlukeyq0c69v97uss68fet26kjkcsrymd2kv6d4' 7 | - 'https://tippin.me/@lukechilds' 8 | -------------------------------------------------------------------------------- /tests/loading/Check zsh-nvm installs nvm if it isn't already installed: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | source ../common.sh 3 | 4 | # Check nvm isn't already installed 5 | [[ ! -f "$NVM_DIR/nvm.sh" ]] || die "nvm is already installed $NVM_DIR/nvm.sh" 6 | 7 | # Load zsh-nvm 8 | load_zsh_nvm 9 | 10 | # Check nvm is now installed 11 | [[ -f "$NVM_DIR/nvm.sh" ]] || die "nvm wasn't installed" 12 | -------------------------------------------------------------------------------- /tests/loading/Check zsh-nvm is loaded: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | source ../common.sh 3 | 4 | # We don't need to actually load nvm for this test 5 | export ZSH_NVM_NO_LOAD=true 6 | 7 | # Check ZSH_NVM_DIR isn't already set 8 | [[ -z ${ZSH_NVM_DIR+x} ]] || die "ZSH_NVM_DIR already set" 9 | 10 | # Load zsh-nvm 11 | load_zsh_nvm 12 | 13 | # Check ZSH_NVM_DIR is now set 14 | [[ ! -z ${ZSH_NVM_DIR+x} ]] || die "ZSH_NVM_DIR wasn't set" 15 | -------------------------------------------------------------------------------- /tests/loading/Check zsh-nvm exports NVM_DIR if it doesn't exist: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | source ../common.sh 3 | 4 | # We don't need to actually load nvm for this test 5 | export ZSH_NVM_NO_LOAD=true 6 | 7 | # Unset NVM_DIR 8 | unset NVM_DIR 9 | 10 | # Check NVM_DIR is unset 11 | [[ -z "$NVM_DIR" ]] || die "NVM_DIR already set" 12 | 13 | # Load zsh-nvm 14 | load_zsh_nvm 15 | 16 | # Check NVM_DIR is now set to our default 17 | [[ "$NVM_DIR" == "$HOME/.nvm" ]] || die "NVM_DIR wasn't set to \$HOME/.nvm" 18 | -------------------------------------------------------------------------------- /tests/loading/Check zsh-nvm doesn't install nvm if it's already installed: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | source ../common.sh 3 | 4 | nvm_placeholder="nvm placeholder" 5 | 6 | # Fake nvm installation 7 | mkdir "$NVM_DIR" 8 | echo "$nvm_placeholder" > "$NVM_DIR/nvm.sh" 9 | 10 | # Check fake nvm file is there 11 | [[ -f "$NVM_DIR/nvm.sh" ]] || die "Fake nvm file wasn't created" 12 | 13 | # Load zsh-nvm 14 | load_zsh_nvm 15 | 16 | # Check fake nvm installation wasn't overwritten 17 | [[ $(cat "$NVM_DIR/nvm.sh") == "$nvm_placeholder" ]] || die "Existing nvm installation was overwritten" 18 | -------------------------------------------------------------------------------- /tests/wrapper commands/nvm install: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | source ../common.sh 3 | 4 | # Load zsh-nvm 5 | load_zsh_nvm 6 | 7 | # Fallback to default nvm install 8 | nvm install 5 && [[ "$(node --version)" == "v5."* ]] || die "nvm install 5 didn't fall back to default nvm install" 9 | 10 | # Install Nightly 11 | nvm install nightly && [[ "$(node --version)" == *"nightly"* ]] || die "nvm install nightly didn't install a nightly" 12 | 13 | # Install Release Candidate 14 | nvm install rc && [[ "$(node --version)" == *"rc"* ]] || die "nvm install rc didn't install a release candidate" 15 | -------------------------------------------------------------------------------- /tests/loading/Check zsh-nvm loads node: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | source ../common.sh 3 | 4 | # Node.js version to install 5 | node_version=v5.11.0 6 | 7 | # Load zsh-nvm and install Node.js in subshell 8 | (load_zsh_nvm && nvm install "$node_version" && [[ "$(node --version)" == "$node_version" ]]) || die "node wasn't installed" 9 | 10 | # Check node isn't available 11 | [[ "$(node --version)" != "$node_version" ]] || die "node shouldn't be available $(node --version)" 12 | 13 | # Load zsh-nvm 14 | load_zsh_nvm 15 | 16 | # Check node is automatically loaded 17 | [[ "$(node --version)" == "$node_version" ]] || die "node wasn't loaded" 18 | -------------------------------------------------------------------------------- /tests/wrapper commands/nvm revert: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | source ../common.sh 3 | 4 | # Version to revert nvm to 5 | revert_version=v0.32.0 6 | 7 | # Load zsh-nvm 8 | load_zsh_nvm 9 | 10 | # Check current version isn't revert version 11 | [[ "v$(nvm --version)" != "$revert_version" ]] || die "Revert version ($revert_version) already installed" 12 | 13 | # Fake revert file 14 | echo "$revert_version" > "$ZSH_NVM_DIR/previous_version" 15 | 16 | # Attempt to revert 17 | nvm revert 18 | 19 | # Check current version is now revert version 20 | [[ "v$(nvm --version)" == "$revert_version" ]] || die "Couldn't revert to $revert_version" 21 | -------------------------------------------------------------------------------- /tests/wrapper commands/nvm upgrade: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | source ../common.sh 3 | 4 | # Version to revert nvm to 5 | revert_version=v0.32.0 6 | 7 | # Load zsh-nvm 8 | load_zsh_nvm 9 | 10 | # Save latest version 11 | latest_version=$(nvm --version) 12 | 13 | # Roll back to an old version 14 | echo "$revert_version" > "$ZSH_NVM_DIR/previous_version" 15 | nvm revert 16 | 17 | # Check rollback worked 18 | [[ "v$(nvm --version)" == "$revert_version" ]] || die "Couldn't rollback nvm version" 19 | 20 | # Attempt to upgrade 21 | nvm upgrade 22 | 23 | # Check upgrade worked 24 | [[ "$(nvm --version)" == "$latest_version" ]] || die "Couldn't upgrade nvm to latest version ($latest_version)" 25 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: generic 2 | os: 3 | - linux 4 | - osx 5 | addons: 6 | apt: 7 | packages: 8 | - zsh 9 | before_install: 10 | - zsh --version 11 | - if [[ "$TRAVIS_OS_NAME" == "osx" ]]; then brew uninstall node; fi 12 | - sudo rm -rf /usr/local/{lib/node{,/.npm,_modules},bin,share/man}/{npm*,node*,man1/node*} 13 | - if [[ $(command -v nvm) == "nvm" ]]; then nvm deactivate && nvm unload && echo "Deactivated Travis nvm"; fi 14 | install: 15 | - (mkdir /tmp/urchin && cd /tmp/urchin && curl -s "$(curl -s https://registry.npmjs.com/urchin | grep -Eo '"tarball":\s*"[^"]+"' | tail -n 1 | awk -F\" '{ print $4 }')" -O && tar -x -f urchin*) 16 | - chmod +x /tmp/urchin/package/urchin 17 | script: 18 | - /tmp/urchin/package/urchin -s zsh tests 19 | -------------------------------------------------------------------------------- /tests/options/NVM_NO_USE: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | source ../common.sh 3 | 4 | # Node.js version to install 5 | node_version=v5.11.0 6 | 7 | # Load zsh-nvm and install Node.js in subshell 8 | (load_zsh_nvm && nvm install "$node_version" && [[ "$(node --version)" == "$node_version" ]]) || die "node wasn't installed" 9 | 10 | # Check node isn't available 11 | [[ "$(node --version)" != "$node_version" ]] || die "node shouldn't be available $(node --version)" 12 | 13 | # Set NVM_NO_USE to true 14 | export NVM_NO_USE=true 15 | 16 | # Load zsh-nvm 17 | load_zsh_nvm 18 | 19 | # Check node still isn't available 20 | [[ "$(node --version)" != "$node_version" ]] || die "node shouldn't be available $(node --version)" 21 | 22 | # Manually load node 23 | nvm use "$node_version" 24 | 25 | # Check node is a binary 26 | [[ "$(command -v node)" == "$(nvm_version_path $node_version)/bin/node" ]] || die "node should now be a binary" 27 | -------------------------------------------------------------------------------- /tests/options/NVM_LAZY_LOAD adds lazy loader for external yarn: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | source ../common.sh 3 | 4 | # Node.js version to install 5 | node_version=v5.11.0 6 | 7 | # Load zsh-nvm and install Node.js in subshell 8 | (load_zsh_nvm && nvm install "$node_version" && [[ "$(node --version)" == "$node_version" ]]) || die "node wasn't installed" 9 | 10 | # Check node isn't available 11 | [[ "$(node --version)" != "$node_version" ]] || die "node shouldn't be available $(node --version)" 12 | 13 | # Set NVM_LAZY_LOAD to true 14 | export NVM_LAZY_LOAD=true 15 | 16 | # Create placeholder yarn 17 | yarn() { echo yarn } 18 | 19 | # Check yarn is a placeholder function 20 | [[ $(which yarn) != *"_zsh_nvm_load"* ]] || die "yarn should be a placeholder function" 21 | 22 | # Load zsh-nvm 23 | load_zsh_nvm 24 | 25 | # Check yarn is a lazy load function 26 | [[ $(which yarn) == *"_zsh_nvm_load"* ]] || die "yarn should be a lazy load function" 27 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2016 Luke Childs 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /tests/options/NVM_LAZY_LOAD: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | source ../common.sh 3 | 4 | # Node.js version to install 5 | node_version=v5.11.0 6 | 7 | # Load zsh-nvm and install Node.js in subshell 8 | (load_zsh_nvm && nvm install "$node_version" && [[ "$(node --version)" == "$node_version" ]]) || die "node wasn't installed" 9 | 10 | # Check node isn't available 11 | [[ "$(node --version)" != "$node_version" ]] || die "node shouldn't be available $(node --version)" 12 | 13 | # Set NVM_LAZY_LOAD to true 14 | export NVM_LAZY_LOAD=true 15 | 16 | # Load zsh-nvm 17 | load_zsh_nvm 18 | 19 | # Check nvm is a lazy load function 20 | [[ $(which nvm) == *"_zsh_nvm_load"* ]] || die "nvm should be a lazy load function" 21 | 22 | # Check node is a lazy load function 23 | [[ $(command -v node) == "node" ]] || die "node should be a shell function" 24 | 25 | # Check npm is a lazy load function 26 | [[ $(command -v npm) == "npm" ]] || die "npm should be a shell function" 27 | 28 | # Init lazy loader 29 | node --version || die "couldn't run lazy loader" 30 | 31 | # Check nvm is not a lazy load function 32 | [[ $(which nvm) != *"_zsh_nvm_load"* ]] || die "nvm should not be a lazy load function" 33 | 34 | # Check node is a binary 35 | [[ "$(command -v node)" == "$(nvm_version_path $node_version)/bin/node" ]] || die "node should now be a binary" 36 | 37 | # Check npm is a binary 38 | [[ "$(command -v npm)" == "$(nvm_version_path $node_version)/bin/npm" ]] || die "npm should now be a binary" 39 | -------------------------------------------------------------------------------- /tests/options/NVM_LAZY_LOAD_EXTRA_COMMANDS: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | source ../common.sh 3 | 4 | # Node.js version to install 5 | node_version=v5.11.0 6 | 7 | # Load zsh-nvm and install Node.js in subshell 8 | (load_zsh_nvm && nvm install "$node_version" && [[ "$(node --version)" == "$node_version" ]]) || die "node wasn't installed" 9 | 10 | # Check node isn't available 11 | [[ "$(node --version)" != "$node_version" ]] || die "node shouldn't be available $(node --version)" 12 | 13 | # Set NVM_LAZY_LOAD to true 14 | export NVM_LAZY_LOAD=true 15 | export NVM_LAZY_LOAD_EXTRA_COMMANDS=('hostname' 'whoami') 16 | 17 | # Load zsh-nvm 18 | load_zsh_nvm 19 | 20 | # Check nvm is a lazy load function 21 | [[ $(which nvm) == *"_zsh_nvm_load"* ]] || die "nvm should be a lazy load function" 22 | 23 | # Check node is a lazy load function 24 | [[ $(command -v node) == "node" ]] || die "node should be a shell function" 25 | 26 | # Check npm is a lazy load function 27 | [[ $(command -v npm) == "npm" ]] || die "npm should be a shell function" 28 | 29 | # Init lazy loader 30 | whoami || die "couldn't run lazy loader" 31 | 32 | # Check nvm is not a lazy load function 33 | [[ $(which nvm) != *"_zsh_nvm_load"* ]] || die "nvm should not be a lazy load function" 34 | 35 | # Check node is a binary 36 | [[ "$(command -v node)" == "$(nvm_version_path $node_version)/bin/node" ]] || die "node should now be a binary" 37 | 38 | # Check npm is a binary 39 | [[ "$(command -v npm)" == "$(nvm_version_path $node_version)/bin/npm" ]] || die "npm should now be a binary" 40 | -------------------------------------------------------------------------------- /tests/options/NVM_AUTO_USE: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | source ../common.sh 3 | 4 | # Setup .nvmrc dir 5 | local nvmrc_dir="$test_dir/nvmrc" 6 | local no_nvmrc_dir="$test_dir/no-nvmrc" 7 | local nvmrc="$nvmrc_dir/.nvmrc" 8 | mkdir "$no_nvmrc_dir" 9 | mkdir "$nvmrc_dir" 10 | touch "$nvmrc" 11 | 12 | # Set NVM_AUTO_USE to true 13 | export NVM_AUTO_USE=true 14 | 15 | # Load zsh-nvm 16 | load_zsh_nvm 17 | 18 | # Install Node.js 5 19 | nvm install 5 && [[ "$(node --version)" == "v5."* ]] || die "node 5 wasn't installed" 20 | 21 | # Install Node.js 6 22 | nvm install 6 && [[ "$(node --version)" == "v6."* ]] || die "node 5 wasn't installed" 23 | 24 | # Check cd into folder with .nvmrc uses v5 25 | echo 5 > "$nvmrc" 26 | (cd "$nvmrc_dir" && [[ "$(node --version)" == "v5."* ]]) || die "Didn't auto switch to node 5" 27 | 28 | # Check cd into folder with .nvmrc keeps v6 29 | echo 6 > "$nvmrc" 30 | (cd "$nvmrc_dir" && [[ "$(node --version)" == "v6."* ]]) || die "Didn't keep node 5" 31 | 32 | # Check cd into folder with .nvmrc installs v7 33 | echo 7 > "$nvmrc" 34 | (cd "$nvmrc_dir" && [[ "$(node --version)" == "v7."* ]]) || die "Didn't install node 7" 35 | nvm alias default 6 # Make sure 6 is still default not 7 36 | 37 | # Check cd into folder with no .nvmrc keeps manually set version 38 | (nvm use 5 && cd "$no_nvmrc_dir" && [[ "$(node --version)" == "v5."* ]]) || die "Reverted to default node version after manual use and cd" 39 | 40 | # Check cd into folder with no .nvmrc reverts to default version after auto use 41 | echo 5 > "$nvmrc" 42 | (cd "$nvmrc_dir" && [[ "$(node --version)" == "v5."* ]] && cd "$no_nvmrc_dir" && [[ "$(node --version)" == "v6."* ]]) || die "Didn't revert to default node version after auto use and cd" 43 | -------------------------------------------------------------------------------- /tests/options/NVM_LAZY_LOAD && NVM_NO_USE: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | source ../common.sh 3 | 4 | # Node.js version to install 5 | node_version=v5.11.0 6 | 7 | # Load zsh-nvm and install Node.js in subshell 8 | (load_zsh_nvm && nvm install "$node_version" && [[ "$(node --version)" == "$node_version" ]]) || die "node wasn't installed" 9 | 10 | # Check node isn't available 11 | [[ "$(node --version)" != "$node_version" ]] || die "node shouldn't be available $(node --version)" 12 | 13 | # Set NVM_LAZY_LOAD to true 14 | export NVM_LAZY_LOAD=true 15 | 16 | # Set NVM_NO_USE to true 17 | export NVM_NO_USE=true 18 | 19 | # Load zsh-nvm 20 | load_zsh_nvm 21 | 22 | # Check nvm is a lazy load function 23 | [[ $(which nvm) == *"_zsh_nvm_load"* ]] || die "nvm should be a lazy load function" 24 | 25 | # Check node is not available 26 | [[ "$(command -v node)" == "" ]] || die "node should not be available" 27 | 28 | # Check npm is not available 29 | [[ "$(command -v npm)" == "" ]] || die "npm should not be available" 30 | 31 | # Init lazy loader 32 | nvm --version || die "couldn't run lazy loader" 33 | 34 | # Check nvm is not a lazy load function 35 | [[ $(which nvm) != *"_zsh_nvm_load"* ]] || die "nvm should not be a lazy load function" 36 | 37 | # Check node is still not available 38 | [[ "$(command -v node)" == "" ]] || die "node should not be available" 39 | 40 | # Check npm is still not available 41 | [[ "$(command -v npm)" == "" ]] || die "npm should not be available" 42 | 43 | # Manually load node 44 | nvm use "$node_version" 45 | 46 | # Check node is a binary 47 | [[ "$(command -v node)" == "$(nvm_version_path $node_version)/bin/node" ]] || die "node should now be a binary" 48 | 49 | # Check npm is a binary 50 | [[ "$(command -v npm)" == "$(nvm_version_path $node_version)/bin/npm" ]] || die "npm should now be a binary" 51 | -------------------------------------------------------------------------------- /tests/options/NVM_LAZY_LOAD works when global modules has same name as alias: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | source ../common.sh 3 | 4 | # This test will hang if it fails 5 | 6 | # Node.js version to install 7 | node_version=v5.11.0 8 | 9 | # Load zsh-nvm and install Node.js in subshell 10 | (load_zsh_nvm && nvm install "$node_version" && [[ "$(node --version)" == "$node_version" ]]) || die "node wasn't installed" 11 | 12 | # Load zsh-nvm and install htconvert in subshell 13 | (load_zsh_nvm && npm install -g htconvert@1.0.4 && [[ "$(htconvert --version)" == "1.0.4" ]]) || die "htconvert wasn't installed" 14 | 15 | # Check node isn't available 16 | [[ "$(node --version)" != "$node_version" ]] || die "node shouldn't be available $(node --version)" 17 | 18 | # Set NVM_LAZY_LOAD to true 19 | export NVM_LAZY_LOAD=true 20 | 21 | # Set htconvert alias 22 | alias htconvert='echo htconvert alias' 23 | 24 | # Load zsh-nvm 25 | load_zsh_nvm 26 | 27 | # Check nvm is a lazy load function 28 | [[ $(which nvm) == *"_zsh_nvm_load"* ]] || die "nvm should be a lazy load function" 29 | 30 | # Check node is a lazy load function 31 | [[ $(command -v node) == "node" ]] || die "node should be a shell function" 32 | 33 | # Check npm is a lazy load function 34 | [[ $(command -v npm) == "npm" ]] || die "npm should be a shell function" 35 | 36 | # Init lazy loader 37 | node --version || die "couldn't run lazy loader" 38 | 39 | # Check nvm is not a lazy load function 40 | [[ $(which nvm) != *"_zsh_nvm_load"* ]] || die "nvm should not be a lazy load function" 41 | 42 | # Check node is a binary 43 | [[ "$(command -v node)" == "$(nvm_version_path $node_version)/bin/node" ]] || die "node should now be a binary" 44 | 45 | # Check npm is a binary 46 | [[ "$(command -v npm)" == "$(nvm_version_path $node_version)/bin/npm" ]] || die "npm should now be a binary" 47 | -------------------------------------------------------------------------------- /zsh-nvm.plugin.zsh: -------------------------------------------------------------------------------- 1 | ZSH_NVM_DIR=${0:a:h} 2 | 3 | [[ -z "$NVM_DIR" ]] && export NVM_DIR="$HOME/.nvm" 4 | 5 | _zsh_nvm_rename_function() { 6 | test -n "$(declare -f $1)" || return 7 | eval "${_/$1/$2}" 8 | unset -f $1 9 | } 10 | 11 | _zsh_nvm_has() { 12 | type "$1" > /dev/null 2>&1 13 | } 14 | 15 | _zsh_nvm_latest_release_tag() { 16 | echo $(builtin cd "$NVM_DIR" && git fetch --quiet --tags origin && git describe --abbrev=0 --tags --match "v[0-9]*" $(git rev-list --tags --max-count=1)) 17 | } 18 | 19 | _zsh_nvm_install() { 20 | echo "Installing nvm..." 21 | git clone https://github.com/nvm-sh/nvm.git "$NVM_DIR" 22 | $(builtin cd "$NVM_DIR" && git checkout --quiet "$(_zsh_nvm_latest_release_tag)") 23 | } 24 | 25 | _zsh_nvm_global_binaries() { 26 | 27 | # Look for global binaries 28 | local global_binary_paths="$(echo "$NVM_DIR"/v0*/bin/*(N) "$NVM_DIR"/versions/*/*/bin/*(N))" 29 | 30 | # If we have some, format them 31 | if [[ -n "$global_binary_paths" ]]; then 32 | echo "$NVM_DIR"/v0*/bin/*(N) "$NVM_DIR"/versions/*/*/bin/*(N) | 33 | xargs -n 1 basename | 34 | sort | 35 | uniq 36 | fi 37 | } 38 | 39 | _zsh_nvm_load() { 40 | 41 | # Source nvm (check if `nvm use` should be ran after load) 42 | if [[ "$NVM_NO_USE" == true ]]; then 43 | source "$NVM_DIR/nvm.sh" --no-use 44 | else 45 | source "$NVM_DIR/nvm.sh" 46 | fi 47 | 48 | # Rename main nvm function 49 | _zsh_nvm_rename_function nvm _zsh_nvm_nvm 50 | 51 | # Wrap nvm in our own function 52 | nvm() { 53 | case $1 in 54 | 'upgrade') 55 | _zsh_nvm_upgrade 56 | ;; 57 | 'revert') 58 | _zsh_nvm_revert 59 | ;; 60 | 'use') 61 | _zsh_nvm_nvm "$@" 62 | export NVM_AUTO_USE_ACTIVE=false 63 | ;; 64 | 'install' | 'i') 65 | _zsh_nvm_install_wrapper "$@" 66 | ;; 67 | *) 68 | _zsh_nvm_nvm "$@" 69 | ;; 70 | esac 71 | } 72 | } 73 | 74 | _zsh_nvm_completion() { 75 | 76 | # Add provided nvm completion 77 | [[ -r $NVM_DIR/bash_completion ]] && source $NVM_DIR/bash_completion 78 | } 79 | 80 | _zsh_nvm_lazy_load() { 81 | 82 | # Get all global node module binaries including node 83 | # (only if NVM_NO_USE is off) 84 | local global_binaries 85 | if [[ "$NVM_NO_USE" == true ]]; then 86 | global_binaries=() 87 | else 88 | global_binaries=($(_zsh_nvm_global_binaries)) 89 | fi 90 | 91 | # Add yarn lazy loader if it's been installed by something other than npm 92 | _zsh_nvm_has yarn && global_binaries+=('yarn') 93 | 94 | # Add nvm 95 | global_binaries+=('nvm') 96 | global_binaries+=($NVM_LAZY_LOAD_EXTRA_COMMANDS) 97 | 98 | # Remove any binaries that conflict with current aliases 99 | local cmds 100 | cmds=() 101 | local bin 102 | for bin in $global_binaries; do 103 | [[ "$(which $bin 2> /dev/null)" = "$bin: aliased to "* ]] || cmds+=($bin) 104 | done 105 | 106 | # Create function for each command 107 | local cmd 108 | for cmd in $cmds; do 109 | 110 | # When called, unset all lazy loaders, load nvm then run current command 111 | eval "$cmd(){ 112 | unset -f $cmds > /dev/null 2>&1 113 | _zsh_nvm_load 114 | $cmd \"\$@\" 115 | }" 116 | done 117 | } 118 | 119 | nvm_update() { 120 | echo 'Deprecated, please use `nvm upgrade`' 121 | } 122 | _zsh_nvm_upgrade() { 123 | 124 | # Use default upgrade if it's built in 125 | if [[ -n "$(_zsh_nvm_nvm help | grep 'nvm upgrade')" ]]; then 126 | _zsh_nvm_nvm upgrade 127 | return 128 | fi 129 | 130 | # Otherwise use our own 131 | local installed_version=$(builtin cd "$NVM_DIR" && git describe --tags) 132 | echo "Installed version is $installed_version" 133 | echo "Checking latest version of nvm..." 134 | local latest_version=$(_zsh_nvm_latest_release_tag) 135 | if [[ "$installed_version" = "$latest_version" ]]; then 136 | echo "You're already up to date" 137 | else 138 | echo "Updating to $latest_version..." 139 | echo "$installed_version" > "$ZSH_NVM_DIR/previous_version" 140 | $(builtin cd "$NVM_DIR" && git fetch --quiet && git checkout "$latest_version") 141 | _zsh_nvm_load 142 | fi 143 | } 144 | 145 | _zsh_nvm_previous_version() { 146 | cat "$ZSH_NVM_DIR/previous_version" 2>/dev/null 147 | } 148 | 149 | _zsh_nvm_revert() { 150 | local previous_version="$(_zsh_nvm_previous_version)" 151 | if [[ -n "$previous_version" ]]; then 152 | local installed_version=$(builtin cd "$NVM_DIR" && git describe --tags) 153 | if [[ "$installed_version" = "$previous_version" ]]; then 154 | echo "Already reverted to $installed_version" 155 | return 156 | fi 157 | echo "Installed version is $installed_version" 158 | echo "Reverting to $previous_version..." 159 | $(builtin cd "$NVM_DIR" && git checkout "$previous_version") 160 | _zsh_nvm_load 161 | else 162 | echo "No previous version found" 163 | fi 164 | } 165 | 166 | autoload -U add-zsh-hook 167 | _zsh_nvm_auto_use() { 168 | _zsh_nvm_has nvm_find_nvmrc || return 169 | 170 | local node_version="$(nvm version)" 171 | local nvmrc_path="$(nvm_find_nvmrc)" 172 | 173 | if [[ -n "$nvmrc_path" ]]; then 174 | local nvmrc_node_version="$(nvm version $(cat "$nvmrc_path"))" 175 | 176 | if [[ "$nvmrc_node_version" = "N/A" ]]; then 177 | nvm install && export NVM_AUTO_USE_ACTIVE=true 178 | elif [[ "$nvmrc_node_version" != "$node_version" ]]; then 179 | nvm use && export NVM_AUTO_USE_ACTIVE=true 180 | fi 181 | elif [[ "$node_version" != "$(nvm version default)" ]] && [[ "$NVM_AUTO_USE_ACTIVE" = true ]]; then 182 | echo "Reverting to nvm default version" 183 | nvm use default 184 | fi 185 | } 186 | 187 | _zsh_nvm_install_wrapper() { 188 | case $2 in 189 | 'rc') 190 | NVM_NODEJS_ORG_MIRROR=https://nodejs.org/download/rc/ nvm install node && nvm alias rc "$(node --version)" 191 | echo "Clearing mirror cache..." 192 | nvm ls-remote > /dev/null 2>&1 193 | echo "Done!" 194 | ;; 195 | 'nightly') 196 | NVM_NODEJS_ORG_MIRROR=https://nodejs.org/download/nightly/ nvm install node && nvm alias nightly "$(node --version)" 197 | echo "Clearing mirror cache..." 198 | nvm ls-remote > /dev/null 2>&1 199 | echo "Done!" 200 | ;; 201 | *) 202 | _zsh_nvm_nvm "$@" 203 | ;; 204 | esac 205 | } 206 | 207 | # Don't init anything if this is true (debug/testing only) 208 | if [[ "$ZSH_NVM_NO_LOAD" != true ]]; then 209 | 210 | # Install nvm if it isn't already installed 211 | [[ ! -f "$NVM_DIR/nvm.sh" ]] && _zsh_nvm_install 212 | 213 | # If nvm is installed 214 | if [[ -f "$NVM_DIR/nvm.sh" ]]; then 215 | 216 | # Load it 217 | [[ "$NVM_LAZY_LOAD" == true ]] && _zsh_nvm_lazy_load || _zsh_nvm_load 218 | 219 | # Enable completion 220 | [[ "$NVM_COMPLETION" == true ]] && _zsh_nvm_completion 221 | 222 | # Auto use nvm on chpwd 223 | [[ "$NVM_AUTO_USE" == true ]] && add-zsh-hook chpwd _zsh_nvm_auto_use && _zsh_nvm_auto_use 224 | fi 225 | 226 | fi 227 | 228 | # Make sure we always return good exit code 229 | # We can't `return 0` because that breaks antigen 230 | true 231 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # zsh-nvm [![Build Status](https://travis-ci.org/lukechilds/zsh-nvm.svg?branch=master)](https://travis-ci.org/lukechilds/zsh-nvm) 2 | 3 | > Zsh plugin for installing, updating and loading `nvm` 4 | 5 | [![GitHub Donate](https://badgen.net/badge/GitHub/Sponsor/D959A7?icon=github)](https://github.com/sponsors/lukechilds) 6 | [![Bitcoin Donate](https://badgen.net/badge/Bitcoin/Donate/F19537?icon=bitcoin)](https://lu.ke/tip/bitcoin) 7 | [![Lightning Donate](https://badgen.net/badge/Lightning/Donate/F6BC41?icon=bitcoin-lightning)](https://lu.ke/tip/lightning) 8 | 9 | [`nvm`](https://github.com/nvm-sh/nvm) is an awesome tool but it can be kind of a pain to install and keep up to date. This zsh plugin allows you to quickly setup `nvm` once, save it in your dotfiles, then never worry about it again. 10 | 11 | The plugin will install the latest stable release of `nvm` if you don't already have it, and then automatically `source` it for you. You can upgrade `nvm` to the latest version whenever you want without losing your installed `node` versions by running `nvm upgrade`. 12 | 13 | Although this is written as a zsh plugin, it also works with bash if you follow the [manual installation instructions](#manually). 14 | 15 | ## Usage 16 | 17 | Once the plugin's installed `nvm` will be available. You'll probably want to load this as one of your first plugins so `node`/`npm` is available for any other plugins that may require them. 18 | 19 | `zsh-nvm` also wraps `nvm` in some additional functionality. 20 | 21 | ### Upgrade 22 | 23 | If you want to upgrade to the latest release of `nvm`: 24 | 25 | ```shell 26 | % nvm upgrade 27 | Installed version is v0.31.0 28 | Checking latest version of nvm... 29 | Updating to v0.31.3... 30 | Previous HEAD position was 2176894... v0.31.0 31 | HEAD is now at 56417f8... v0.31.3 32 | ``` 33 | 34 | ### Revert 35 | 36 | If an upgrade breaks something don't worry, reverting back to the previously installed version is simple: 37 | 38 | ```shell 39 | % nvm revert 40 | Installed version is v0.31.3 41 | Reverting to v0.31.0... 42 | Previous HEAD position was 56417f8... v0.31.3 43 | HEAD is now at 2176894... v0.31.0 44 | ``` 45 | 46 | ### Install 47 | 48 | You can install the latest Node.js nightlies or release candidates with `nvm install nightly|rc`. Aliases will automatically be created so you can easily `nvm use nightly|rc` in the future: 49 | 50 | ``` 51 | % nvm install rc 52 | Downloading and installing node v8.0.0-rc.1... 53 | Downloading https://nodejs.org/download/rc//v8.0.0-rc.1/node-v8.0.0-rc.1-darwin-x64.tar.xz... 54 | ######################################################################## 100.0% 55 | Computing checksum with shasum -a 256 56 | Checksums matched! 57 | Now using node v8.0.0-rc.1 (npm v5.0.0-beta.56) 58 | rc -> v8.0.0-rc.1 59 | Clearing mirror cache... 60 | Done! 61 | ``` 62 | 63 | > **Note:** This is a bit of a hack and leaving rc|nightly versions installed may break nvm when it eventually supports them itself. It's recommended that you don't leave the these versions of Node.js installed. Install them, test/play with them and then uninstall them when you're done. 64 | 65 | ## Options 66 | 67 | ### Custom Directory 68 | 69 | You can specify a custom directory to use with `nvm` by exporting the `NVM_DIR` environment variable. It must be set before `zsh-nvm` is loaded. 70 | 71 | For example, if you are using antigen, you would put the following in your `.zshrc`: 72 | 73 | ```shell 74 | export NVM_DIR="$HOME/.custom-nvm-dir" 75 | antigen bundle lukechilds/zsh-nvm 76 | ``` 77 | 78 | Note: If `nvm` doesn't exist in this directory it'll be automatically installed when you start a session. 79 | 80 | ### Nvm Completion 81 | 82 | `nvm` comes with a default bash_completion profile. If you want to enable it, you can do it by exporting the `NVM_COMPLETION` environment variable and setting it to `true`. It must be set before `zsh-nvm` is loaded. 83 | 84 | For example, if you are using antigen, you would put the following in your `.zshrc`: 85 | 86 | ```bash 87 | # Export nvm completion settings for zsh-nvm plugin 88 | export NVM_COMPLETION=true 89 | antigen bundle lukechilds/zsh-nvm 90 | ``` 91 | 92 | ### Lazy Loading 93 | 94 | If you find `nvm` adds too much lag to your shell startup you can enable lazy loading by exporting the `NVM_LAZY_LOAD` environment variable and setting it to `true`. It must be set before `zsh-nvm` is loaded. 95 | 96 | Lazy loading is around 70x faster (874ms down to 12ms for me), however the first time you run `nvm`, `npm`, `node` or a global module you'll get a slight delay while `nvm` loads first. You'll only get this delay once per session. 97 | 98 | For example, if you are using antigen, you would put the following in your `.zshrc`: 99 | 100 | ```shell 101 | export NVM_LAZY_LOAD=true 102 | antigen bundle lukechilds/zsh-nvm 103 | ``` 104 | 105 | Performance comparison: 106 | 107 | ```shell 108 | % time (source "$NVM_DIR/nvm.sh") 109 | ( source "$NVM_DIR/nvm.sh"; ) 0.58s user 0.37s system 109% cpu 0.874 total 110 | 111 | % time (_zsh_nvm_lazy_load) 112 | ( _zsh_nvm_lazy_load; ) 0.01s user 0.01s system 168% cpu 0.012 total 113 | ``` 114 | 115 | #### Extra commands to trigger lazy loading 116 | By default lazy loading nvm is triggered by running the `nvm`, `node`, `npm` commands or any installed npm global binaries. 117 | If you want to trigger the lazy loading via extra arbitrary commands you can define `NVM_LAZY_LOAD_EXTRA_COMMANDS` and set it to an array of commands as strings. 118 | This can be usefull if programs are not in the above list of binaries but do depend on the availability of `node`, e.g. a vim plugin. 119 | 120 | ```shell 121 | export NVM_LAZY_LOAD_EXTRA_COMMANDS=('vim') 122 | vim --version 123 | #node is now loaded 124 | ``` 125 | 126 | ### Don't autoload node 127 | 128 | By default when `nvm` is loaded it'll automatically run `nvm use default` and load your default `node` version along with `npm` and any global modules. You can disable this behaviour by exporting the `NVM_NO_USE` environment variable and setting it to `true`. It must be set before `zsh-nvm` is loaded. 129 | 130 | If you enable this option you will then need to manually run `nvm use ` before you can use `node`. 131 | 132 | For example, if you are using antigen, you would put the following in your `.zshrc`: 133 | 134 | ```shell 135 | export NVM_NO_USE=true 136 | antigen bundle lukechilds/zsh-nvm 137 | ``` 138 | 139 | ### Auto use 140 | 141 | If you have lots of projects with an `.nvmrc` file you may find the auto use option helpful. If it's enabled, when you `cd` into a directory with an `.nvmrc` file, `zsh-nvm` will automatically load or install the required node version in `.nvmrc`. You can enable it by exporting the `NVM_AUTO_USE` environment variable and setting it to `true`. It must be set before `zsh-nvm` is loaded. 142 | 143 | If you enable this option and don't have `nvm` loaded in the current session (`NVM_LAZY_LOAD` or `NVM_NO_USE`) it won't work until you've loaded `nvm`. 144 | 145 | For example, if you are using antigen, you would put the following in your `.zshrc`: 146 | 147 | ```shell 148 | export NVM_AUTO_USE=true 149 | antigen bundle lukechilds/zsh-nvm 150 | ``` 151 | 152 | ## Installation 153 | 154 | ### Using [Antigen](https://github.com/zsh-users/antigen) 155 | 156 | Bundle `zsh-nvm` in your `.zshrc` 157 | 158 | ```shell 159 | antigen bundle lukechilds/zsh-nvm 160 | ``` 161 | 162 | ### Using [zplug](https://github.com/b4b4r07/zplug) 163 | Load `zsh-nvm` as a plugin in your `.zshrc` 164 | 165 | ```shell 166 | zplug "lukechilds/zsh-nvm" 167 | 168 | ``` 169 | ### Using [zgen](https://github.com/tarjoilija/zgen) 170 | 171 | Include the load command in your `.zshrc` 172 | 173 | ```shell 174 | zgen load lukechilds/zsh-nvm 175 | ``` 176 | 177 | ### As an [Oh My ZSH!](https://github.com/robbyrussell/oh-my-zsh) custom plugin 178 | 179 | Clone `zsh-nvm` into your custom plugins repo 180 | 181 | ```shell 182 | git clone https://github.com/lukechilds/zsh-nvm ~/.oh-my-zsh/custom/plugins/zsh-nvm 183 | ``` 184 | Then load as a plugin in your `.zshrc` 185 | 186 | ```shell 187 | plugins+=(zsh-nvm) 188 | ``` 189 | 190 | Keep in mind that plugins need to be added before `oh-my-zsh.sh` is sourced. 191 | 192 | ### Manually 193 | Clone this repository somewhere (`~/.zsh-nvm` for example) 194 | 195 | ```shell 196 | git clone https://github.com/lukechilds/zsh-nvm.git ~/.zsh-nvm 197 | ``` 198 | Then source it in your `.zshrc` (or `.bashrc`) 199 | 200 | ```shell 201 | source ~/.zsh-nvm/zsh-nvm.plugin.zsh 202 | ``` 203 | 204 | ## Tests 205 | 206 | To run the tests you'll need to install [Urchin](https://github.com/tlevine/urchin#install). You'll also need to run the tests in an environment that doesn't already have `node` or `nvm` loaded. 207 | 208 | You can remove `nvm` from the existing session with: 209 | 210 | ```shell 211 | nvm deactivate && nvm unload 212 | ``` 213 | 214 | Run the tests with: 215 | 216 | ```shell 217 | urchin -s zsh tests 218 | ``` 219 | 220 | 221 | 222 | ## Related 223 | 224 | - [`zsh-better-npm-completion`](https://github.com/lukechilds/zsh-better-npm-completion) - Better completion for `npm` 225 | 226 | ## License 227 | 228 | MIT © Luke Childs 229 | --------------------------------------------------------------------------------