Fixes bug where NVM init script is appended to the wrong startup file

The existence of .bashrc, .bash_profile or .zshrc doesn't prove that the file is the currently utilized startup file or that it is the only file sourced. Because the current code operates under this assumption, the code:

```bash
  export NVM_DIR="/Users/<USERNAME>/.nvm"
  [ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh"  # This loads nvm
```
is getting appended to the wrong profile for zsh users.

1) `[ -d ~/.nvm ] && rm -rfv ~/.nvm `
2) Install Zsh via https://github.com/robbyrussell/oh-my-zsh
3) Run nvm install script with cURL.
4) Run `which nvm`. Observe it's not in `$PATH`
5) Open a new shell session. Observe the issue persists.

To fix this, the code in this PR checks:

- Is `$PROFILE` set? If so, echo it.
- What is the active shell that is executing this script? (the parent shell)
- If the shell is zsh, echo the zshrc file
- If the shell is bash:
	- Is it login or non-login? Echo the neccessary file
	- Is it interactive or non interactive? Echo the neccessary
	  file.

This logic should address the primary bug and also prevent future bugs.

Note: bash by default reads ~/.profile last so it makes sense to write to ~/.bashrc or ~/.bash_profile instead of ~/.profile

See: https://www.gnu.org/software/bash/manual/html_node/Bash-Startup-Files.html

Passed Manual Tests:
  zsh
  bash from within script
  bash as default shell
  bash -l from within zsh
This commit is contained in:
Michael Martinez 2015-01-15 01:35:36 -08:00
parent 448bd38a55
commit 11507c3f38
1 changed files with 20 additions and 13 deletions

View File

@ -100,26 +100,33 @@ install_nvm_as_script() {
} }
} }
#
# Detect profile file if not specified as environment variable # Detect profile file if not specified as environment variable
# (eg: PROFILE=~/.myprofile) # (eg: PROFILE=~/.myprofile)
# The echo'ed path is guaranteed to be an existing file # The echo'ed path is guaranteed to be an existing file
# Otherwise, an empty string is returned # Otherwise, an empty string is returned
# #
# Auto detects the appropriate shell by looking at the shell of
# the caller process and login/interactive status.
nvm_detect_profile() { nvm_detect_profile() {
if [ -f "$PROFILE" ]; then local shell=$(ps $PPID | tail -n 1 | awk '{print $(NF-0)}')
echo "$PROFILE" if [ -f "$PROFILE" ]; then
elif [ -f "$HOME/.bashrc" ]; then echo "$PROFILE"
echo "$HOME/.bashrc" elif echo $shell | grep -q "zsh"; then
elif [ -f "$HOME/.bash_profile" ]; then echo "$HOME/.zshrc"
echo "$HOME/.bash_profile" elif echo $shell | grep -q "bash"; then
elif [ -f "$HOME/.zshrc" ]; then local login_status=$(shopt -q login_shell)
echo "$HOME/.zshrc" local interactive_status=$([[ $- == *i* ]])
elif [ -f "$HOME/.profile" ]; then if [[ $login_status -eq 0 ]]; then
echo "$HOME/.profile" echo "$HOME/.bashrc"
fi elif [[ $interactive_status -eq 0 ]]; then
echo "$HOME/.bash_profile"
fi
else
echo ""
fi
} }
nvm_do_install() { nvm_do_install() {
if [ -z "$METHOD" ]; then if [ -z "$METHOD" ]; then
# Autodetect install method # Autodetect install method