[Fix] install: detect user shell and try shellrc file first

This commit is contained in:
Jim Heald 2020-07-18 11:11:41 -07:00
parent 4b1100e515
commit b2a4377694
3 changed files with 33 additions and 8 deletions

View File

@ -233,7 +233,18 @@ nvm_detect_profile() {
local DETECTED_PROFILE
DETECTED_PROFILE=''
if [ -n "${BASH_VERSION-}" ]; then
# Detect the user's login shell
local DETECTED_SHELL
local SHELLRC
DETECTED_SHELL="${SHELL##*/}"
SHELLRC="$HOME/.${DETECTED_SHELL}rc"
if [ -n "${DETECTED_SHELL}" ]; then
if [ -f "$SHELLRC" ]; then
DETECTED_PROFILE="$SHELLRC"
fi
elif [ -n "${BASH_VERSION-}" ]; then
if [ -f "$HOME/.bashrc" ]; then
DETECTED_PROFILE="$HOME/.bashrc"
elif [ -f "$HOME/.bash_profile" ]; then

View File

@ -2,6 +2,7 @@
setup () {
HOME="."
SHELL='/bin/bash'
NVM_ENV=testing \. ../../install.sh
touch ".bashrc"
touch ".bash_profile"
@ -12,6 +13,7 @@ setup () {
cleanup () {
unset HOME
unset SHELL
unset NVM_ENV
unset NVM_DETECT_PROFILE
unset BASH_VERSION
@ -34,6 +36,12 @@ if [ -n "$NVM_DETECT_PROFILE" ]; then
die "nvm_detect_profile still detected a profile even though PROFILE=/dev/null"
fi
# .bashrc should be detected if the shell is bash
NVM_DETECT_PROFILE="$(unset PROFILE; nvm_detect_profile)"
if [ "$NVM_DETECT_PROFILE" != "$HOME/.bashrc" ]; then
die "nvm_detect_profile didn't pick \$HOME/.bashrc for bash"
fi
# .bashrc should be detected for bash
NVM_DETECT_PROFILE="$(BASH_VERSION="1"; unset PROFILE; nvm_detect_profile)"
if [ "$NVM_DETECT_PROFILE" != "$HOME/.bashrc" ]; then
@ -46,8 +54,14 @@ if [ "$NVM_DETECT_PROFILE" != "test_profile" ]; then
die "nvm_detect_profile ignored \$PROFILE"
fi
# .zshrc should be detected if the shell is zsh
NVM_DETECT_PROFILE="$(SHELL="/bin/zsh"; unset PROFILE; unset BASH_VERSION; nvm_detect_profile)"
if [ "$NVM_DETECT_PROFILE" != "$HOME/.zshrc" ]; then
die "nvm_detect_profile didn't pick \$HOME/.zshrc for zsh"
fi
# .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"; ZSH_VERSION="1"; unset PROFILE; unset BASH_VERSION; nvm_detect_profile)"
if [ "$NVM_DETECT_PROFILE" != "$HOME/.zshrc" ]; then
die "nvm_detect_profile didn't pick \$HOME/.zshrc for zsh"
fi
@ -82,29 +96,29 @@ fi
# return an empty value if everything fails
#
# It should favor .profile if file exists
NVM_DETECT_PROFILE="$(unset BASH_VERSION; unset ZSH_VERSION; nvm_detect_profile)"
# It should favor .profile if other detection methods fail and file exists and
NVM_DETECT_PROFILE="$(unset BASH_VERSION; unset ZSH_VERSION; unset SHELL; nvm_detect_profile)"
if [ "$NVM_DETECT_PROFILE" != "$HOME/.profile" ]; then
die "nvm_detect_profile should have selected .profile"
fi
# Otherwise, it should favor .bashrc if file exists
rm ".profile"
NVM_DETECT_PROFILE="$(unset BASH_VERSION; unset ZSH_VERSION; nvm_detect_profile)"
NVM_DETECT_PROFILE="$(unset BASH_VERSION; unset ZSH_VERSION; unset SHELL; nvm_detect_profile)"
if [ "$NVM_DETECT_PROFILE" != "$HOME/.bashrc" ]; then
die "nvm_detect_profile should have selected .bashrc"
fi
# Otherwise, it should favor .bash_profile if file exists
rm ".bashrc"
NVM_DETECT_PROFILE="$(unset BASH_VERSION; unset ZSH_VERSION; nvm_detect_profile)"
NVM_DETECT_PROFILE="$(unset BASH_VERSION; unset ZSH_VERSION; unset SHELL; nvm_detect_profile)"
if [ "$NVM_DETECT_PROFILE" != "$HOME/.bash_profile" ]; then
die "nvm_detect_profile should have selected .bash_profile"
fi
# Otherwise, it should favor .zshrc if file exists
rm ".bash_profile"
NVM_DETECT_PROFILE="$(unset BASH_VERSION; unset ZSH_VERSION; nvm_detect_profile)"
NVM_DETECT_PROFILE="$(unset BASH_VERSION; unset ZSH_VERSION; unset SHELL; nvm_detect_profile)"
if [ "$NVM_DETECT_PROFILE" != "$HOME/.zshrc" ]; then
die "nvm_detect_profile should have selected .zshrc"
fi

View File

@ -1,4 +1,4 @@
#!/bin/sh
#!/bin/bash
setup () {
shopt -s expand_aliases