dotfiles/.fast-nvm.sh
Guilherme Rugai Freire 09a2f62c5d
fast-nvm: fix nvm use with no .nvmrc
script was trying to run ``nvm use`` when there was no .nvmrc (eg: /usr/share)
2021-08-23 09:04:27 -03:00

26 lines
746 B
Bash

locate_nvmrc() {
locate -w .nvmrc | xargs -I'{}' realpath --relative-to=. '{}' | awk '/^(\.\.\/)*\.nvmrc$/ {print $0}' | sort | xargs -I'{}' realpath '{}'
}
export PATH="$HOME/.nvm/versions/node/$(/bin/cat $HOME/.nvm/alias/default)/bin:$PATH"
nvm() {
. $HOME/.nvm/nvm.sh; nvm "$@"
}
DEFAULT=$(locate_nvmrc)
cd() {
if builtin cd "$@" 2>/dev/null; then
FOUND="$(locate_nvmrc)"
if [ "$FOUND" != "" ] && [ "$DEFAULT" != "$FOUND" ]; then
DEFAULT=$FOUND
nvm use
fi
else
if test -f "$@"; then echo "cd: not a directory: $*"
elif test -d "$@"; then echo "cd: can not change to $*"
else echo "cd: no such file or directory: $*"
fi
return 1
fi
}