Support deduction of nvm directory in zsh

In zsh nvm fails to deduce nvm's directory from nvm.sh, because
`$BASH_SOURCE` is not present in zsh. Use the directory of `$0`, if its
filename is `nvm.sh`. We use `$BASH_SOUCE` in bash, `$0` in zsh (and
similar) and fallback to `~/.nvm` otherwise.
This commit is contained in:
Jonas Dohse 2014-03-10 11:59:17 +01:00
parent 61b44097fe
commit 192b2ecb1b
2 changed files with 14 additions and 2 deletions

8
nvm.sh
View File

@ -19,8 +19,12 @@ fi
# Auto detect the NVM_DIR # Auto detect the NVM_DIR
if [ ! -d "$NVM_DIR" ]; then if [ ! -d "$NVM_DIR" ]; then
if [ -n "$BASH_SOURCE" ]; then if [ -n "${BASH_SOURCE}" ]; then
export NVM_DIR=$(cd $NVM_CD_FLAGS $(dirname ${BASH_SOURCE[0]:-$0}) > /dev/null && pwd) export NVM_DIR=$(cd $NVM_CD_FLAGS $(dirname $BASH_SOURCE) > /dev/null && pwd)
elif [ "$(basename $0)" = nvm.sh ]; then
# Works in shells that put the path of the sourcee into $0. Unfortunately
# dash is not among these.
export NVM_DIR=$(cd $NVM_CD_FLAGS $(dirname $0) > /dev/null && pwd)
else else
export NVM_DIR=$HOME/.nvm export NVM_DIR=$HOME/.nvm
fi fi

View File

@ -0,0 +1,8 @@
#!/bin/sh
echo Testing bash
bash -c '. ../../nvm.sh'
echo Testing zsh
zsh -c '. ../../nvm.sh'
echo Testing sh
sh -c '. ../../nvm.sh'