[Fix] `install`: Detect correct profile based on $SHELL var

Thomas Geirhovd 2021-08-12 15:51:32 +02:00 committed by Jordan Harband
parent 812d6e4ab4
commit e98e9d9e4b
No known key found for this signature in database
GPG Key ID: 9F6A681E35EF8B56
2 changed files with 14 additions and 13 deletions

View File

@ -267,15 +267,17 @@ nvm_detect_profile() {
local DETECTED_PROFILE local DETECTED_PROFILE
DETECTED_PROFILE='' DETECTED_PROFILE=''
if [ -n "${BASH_VERSION-}" ]; then if [ "${SHELL#*bash}" != "$SHELL" ]; then
if [ -f "$HOME/.bashrc" ]; then if [ -f "$HOME/.bashrc" ]; then
DETECTED_PROFILE="$HOME/.bashrc" DETECTED_PROFILE="$HOME/.bashrc"
elif [ -f "$HOME/.bash_profile" ]; then elif [ -f "$HOME/.bash_profile" ]; then
DETECTED_PROFILE="$HOME/.bash_profile" DETECTED_PROFILE="$HOME/.bash_profile"
fi fi
elif [ -n "${ZSH_VERSION-}" ]; then elif [ "${SHELL#*zsh}" != "$SHELL" ]; then
if [ -f "$HOME/.zshrc" ]; then
DETECTED_PROFILE="$HOME/.zshrc" DETECTED_PROFILE="$HOME/.zshrc"
fi fi
fi
if [ -z "$DETECTED_PROFILE" ]; then if [ -z "$DETECTED_PROFILE" ]; then
for EACH_PROFILE in ".profile" ".bashrc" ".bash_profile" ".zshrc" for EACH_PROFILE in ".profile" ".bashrc" ".bash_profile" ".zshrc"

View File

@ -14,8 +14,7 @@ cleanup () {
unset HOME unset HOME
unset NVM_ENV unset NVM_ENV
unset NVM_DETECT_PROFILE unset NVM_DETECT_PROFILE
unset BASH_VERSION unset SHELL
unset ZSH_VERSION
unset -f setup cleanup die unset -f setup cleanup die
rm -f ".bashrc" ".bash_profile" ".zshrc" ".profile" "test_profile" > "/dev/null" 2>&1 rm -f ".bashrc" ".bash_profile" ".zshrc" ".profile" "test_profile" > "/dev/null" 2>&1
} }
@ -35,7 +34,7 @@ if [ -n "$NVM_DETECT_PROFILE" ]; then
fi fi
# .bashrc should be detected for bash # .bashrc should be detected for bash
NVM_DETECT_PROFILE="$(BASH_VERSION="1"; unset PROFILE; nvm_detect_profile)" NVM_DETECT_PROFILE="$(SHELL="/bin/bash"; unset PROFILE; nvm_detect_profile)"
if [ "$NVM_DETECT_PROFILE" != "$HOME/.bashrc" ]; then if [ "$NVM_DETECT_PROFILE" != "$HOME/.bashrc" ]; then
die "nvm_detect_profile didn't pick \$HOME/.bashrc for bash" die "nvm_detect_profile didn't pick \$HOME/.bashrc for bash"
fi fi
@ -47,7 +46,7 @@ if [ "$NVM_DETECT_PROFILE" != "test_profile" ]; then
fi fi
# .zshrc should be detected for zsh # .zshrc should be detected for zsh
NVM_DETECT_PROFILE="$(ZSH_VERSION="1"; unset PROFILE; unset BASH_VERSION; nvm_detect_profile)" NVM_DETECT_PROFILE="$(SHELL="/bin/zsh"; unset PROFILE; nvm_detect_profile)"
if [ "$NVM_DETECT_PROFILE" != "$HOME/.zshrc" ]; then if [ "$NVM_DETECT_PROFILE" != "$HOME/.zshrc" ]; then
die "nvm_detect_profile didn't pick \$HOME/.zshrc for zsh" die "nvm_detect_profile didn't pick \$HOME/.zshrc for zsh"
fi fi
@ -64,7 +63,7 @@ fi
# #
# $PROFILE is a valid file # $PROFILE is a valid file
NVM_DETECT_PROFILE="$(PROFILE="test_profile"; unset ZSH_VERSION; nvm_detect_profile)" NVM_DETECT_PROFILE="$(PROFILE="test_profile"; unset SHELL; nvm_detect_profile)"
if [ "$NVM_DETECT_PROFILE" != "test_profile" ]; then if [ "$NVM_DETECT_PROFILE" != "test_profile" ]; then
die "nvm_detect_profile didn't pick \$PROFILE when it was a valid file" die "nvm_detect_profile didn't pick \$PROFILE when it was a valid file"
fi fi
@ -83,35 +82,35 @@ fi
# #
# It should favor .profile if file exists # It should favor .profile if file exists
NVM_DETECT_PROFILE="$(unset BASH_VERSION; unset ZSH_VERSION; nvm_detect_profile)" NVM_DETECT_PROFILE="$(unset SHELL; nvm_detect_profile)"
if [ "$NVM_DETECT_PROFILE" != "$HOME/.profile" ]; then if [ "$NVM_DETECT_PROFILE" != "$HOME/.profile" ]; then
die "nvm_detect_profile should have selected .profile" die "nvm_detect_profile should have selected .profile"
fi fi
# Otherwise, it should favor .bashrc if file exists # Otherwise, it should favor .bashrc if file exists
rm ".profile" rm ".profile"
NVM_DETECT_PROFILE="$(unset BASH_VERSION; unset ZSH_VERSION; nvm_detect_profile)" NVM_DETECT_PROFILE="$(unset SHELL; nvm_detect_profile)"
if [ "$NVM_DETECT_PROFILE" != "$HOME/.bashrc" ]; then if [ "$NVM_DETECT_PROFILE" != "$HOME/.bashrc" ]; then
die "nvm_detect_profile should have selected .bashrc" die "nvm_detect_profile should have selected .bashrc"
fi fi
# Otherwise, it should favor .bash_profile if file exists # Otherwise, it should favor .bash_profile if file exists
rm ".bashrc" rm ".bashrc"
NVM_DETECT_PROFILE="$(unset BASH_VERSION; unset ZSH_VERSION; nvm_detect_profile)" NVM_DETECT_PROFILE="$(unset SHELL; nvm_detect_profile)"
if [ "$NVM_DETECT_PROFILE" != "$HOME/.bash_profile" ]; then if [ "$NVM_DETECT_PROFILE" != "$HOME/.bash_profile" ]; then
die "nvm_detect_profile should have selected .bash_profile" die "nvm_detect_profile should have selected .bash_profile"
fi fi
# Otherwise, it should favor .zshrc if file exists # Otherwise, it should favor .zshrc if file exists
rm ".bash_profile" rm ".bash_profile"
NVM_DETECT_PROFILE="$(unset BASH_VERSION; unset ZSH_VERSION; nvm_detect_profile)" NVM_DETECT_PROFILE="$(unset SHELL; nvm_detect_profile)"
if [ "$NVM_DETECT_PROFILE" != "$HOME/.zshrc" ]; then if [ "$NVM_DETECT_PROFILE" != "$HOME/.zshrc" ]; then
die "nvm_detect_profile should have selected .zshrc" die "nvm_detect_profile should have selected .zshrc"
fi fi
# It should be empty if none is found # It should be empty if none is found
rm ".zshrc" rm ".zshrc"
NVM_DETECT_PROFILE="$(unset BASH_VERSION; unset ZSH_VERSION; nvm_detect_profile)" NVM_DETECT_PROFILE="$(unset SHELL; nvm_detect_profile)"
if [ ! -z "$NVM_DETECT_PROFILE" ]; then if [ ! -z "$NVM_DETECT_PROFILE" ]; then
die "nvm_detect_profile should have returned an empty value" die "nvm_detect_profile should have returned an empty value"
fi fi