nvm/install.sh

132 lines
3.2 KiB
Bash
Raw Normal View History

2012-10-30 07:03:01 +08:00
#!/bin/bash
set -e
nvm_has() {
type "$1" > /dev/null 2>&1
return $?
}
if [ -z "$NVM_DIR" ]; then
NVM_DIR="$HOME/.nvm"
fi
2012-10-30 07:03:01 +08:00
nvm_curl() {
if nvm_has "curl"; then
curl $*
elif nvm_has "wget"; then
# Emulate curl with wget
ARGS="$*"
ARGS=${ARGS/--progress-bar /}
ARGS=${ARGS/-L /}
ARGS=${ARGS/-I /}
ARGS=${ARGS/-s /-q }
ARGS=${ARGS/-o /-O }
ARGS=${ARGS/-C /-c }
wget $ARGS
fi
}
2013-06-08 06:42:28 +08:00
install_nvm_from_git() {
if [ -z "$NVM_SOURCE" ]; then
NVM_SOURCE="https://github.com/creationix/nvm.git"
fi
if [ -d "$NVM_DIR/.git" ]; then
echo "=> nvm is already installed in $NVM_DIR, trying to update"
printf "\r=> "
2014-07-06 04:47:22 +08:00
cd "$NVM_DIR" && (git pull 2> /dev/null || {
2014-03-26 15:36:41 +08:00
echo >&2 "Failed to update nvm, run 'git pull' in $NVM_DIR yourself.."
2014-07-06 04:47:22 +08:00
})
else
# Cloning to $NVM_DIR
echo "=> Downloading nvm from git to '$NVM_DIR'"
printf "\r=> "
mkdir -p "$NVM_DIR"
git clone "$NVM_SOURCE" "$NVM_DIR"
fi
}
install_nvm_as_script() {
if [ -z "$NVM_SOURCE" ]; then
2014-04-26 15:48:06 +08:00
NVM_SOURCE="https://raw.githubusercontent.com/creationix/nvm/master/nvm.sh"
fi
# Downloading to $NVM_DIR
mkdir -p "$NVM_DIR"
if [ -d "$NVM_DIR/nvm.sh" ]; then
echo "=> nvm is already installed in $NVM_DIR, trying to update"
else
echo "=> Downloading nvm as script to '$NVM_DIR'"
fi
nvm_curl -s "$NVM_SOURCE" -o "$NVM_DIR/nvm.sh" || {
echo >&2 "Failed to download '$NVM_SOURCE'.."
return 1
}
}
if [ -z "$METHOD" ]; then
2014-03-26 16:16:26 +08:00
# Autodetect install method
if nvm_has "git"; then
install_nvm_from_git
elif nvm_has "nvm_curl"; then
install_nvm_as_script
2014-03-26 16:16:26 +08:00
else
2014-07-06 02:05:15 +08:00
echo >&2 "You need git, curl, or wget to install nvm"
2014-03-26 16:16:26 +08:00
exit 1
fi
else
if [ "$METHOD" = "git" ]; then
if ! nvm_has "git"; then
echo >&2 "You need git to install nvm"
exit 1
2014-03-26 16:29:05 +08:00
fi
install_nvm_from_git
fi
if [ "$METHOD" = "script" ]; then
if ! nvm_has "nvm_curl"; then
echo >&2 "You need curl or wget to install nvm"
exit 1
2014-03-26 16:29:05 +08:00
fi
install_nvm_as_script
fi
2012-10-30 07:03:01 +08:00
fi
echo
2012-10-30 07:03:01 +08:00
# Detect profile file if not specified as environment variable (eg: PROFILE=~/.myprofile).
if [ -z "$PROFILE" ]; then
2012-10-30 07:03:01 +08:00
if [ -f "$HOME/.bash_profile" ]; then
PROFILE="$HOME/.bash_profile"
elif [ -f "$HOME/.zshrc" ]; then
PROFILE="$HOME/.zshrc"
2012-10-30 07:03:01 +08:00
elif [ -f "$HOME/.profile" ]; then
PROFILE="$HOME/.profile"
2012-10-30 07:03:01 +08:00
fi
fi
2014-05-28 20:32:01 +08:00
SOURCE_STR="\nexport NVM_DIR=\"$NVM_DIR\"\n[ -s \"\$NVM_DIR/nvm.sh\" ] && . \"\$NVM_DIR/nvm.sh\" # This loads nvm"
2012-10-30 07:03:01 +08:00
if [ -z "$PROFILE" ] || [ ! -f "$PROFILE" ] ; then
2014-07-06 04:47:22 +08:00
if [ -z "$PROFILE" ]; then
2014-07-06 02:05:15 +08:00
echo "=> Profile not found. Tried ~/.bash_profile, ~/.zshrc, and ~/.profile."
echo "=> Create one of them and run this script again"
2012-10-30 07:03:01 +08:00
else
echo "=> Profile $PROFILE not found"
echo "=> Create it (touch $PROFILE) and run this script again"
2012-10-30 07:03:01 +08:00
fi
echo " OR"
2014-05-28 20:32:01 +08:00
echo "=> Append the following lines to the correct file yourself:"
2014-07-06 04:47:22 +08:00
printf "%s" "$SOURCE_STR"
2013-07-26 19:58:47 +08:00
echo
2012-10-30 07:03:01 +08:00
else
2014-07-06 04:47:22 +08:00
if ! grep -qc 'nvm.sh' "$PROFILE"; then
echo "=> Appending source string to $PROFILE"
2014-07-07 20:42:27 +08:00
printf "%s\n" "$SOURCE_STR" >> "$PROFILE"
else
echo "=> Source string already in $PROFILE"
fi
2012-10-30 07:03:01 +08:00
fi
echo "=> Close and reopen your terminal to start using nvm"