2012-10-30 07:03:01 +08:00
|
|
|
#!/bin/bash
|
|
|
|
|
2013-12-23 00:46:26 +08:00
|
|
|
set -e
|
|
|
|
|
2014-03-15 11:32:35 +08:00
|
|
|
has() {
|
|
|
|
type "$1" > /dev/null 2>&1
|
|
|
|
return $?
|
|
|
|
}
|
2013-12-23 01:23:59 +08:00
|
|
|
|
2014-03-15 11:32:35 +08:00
|
|
|
if [ -z "$NVM_DIR" ]; then
|
2013-12-23 01:23:59 +08:00
|
|
|
NVM_DIR="$HOME/.nvm"
|
|
|
|
fi
|
2012-10-30 07:03:01 +08:00
|
|
|
|
2014-03-26 16:29:05 +08:00
|
|
|
if ! has "curl"; then
|
|
|
|
if has "wget"; then
|
|
|
|
# Emulate curl with wget
|
|
|
|
curl() {
|
|
|
|
ARGS="$* "
|
|
|
|
ARGS=${ARGS/-s /-q }
|
|
|
|
ARGS=${ARGS/-o /-O }
|
|
|
|
wget $ARGS
|
|
|
|
}
|
2014-03-15 11:32:35 +08:00
|
|
|
fi
|
2014-03-26 16:29:05 +08:00
|
|
|
fi
|
2013-06-08 06:42:28 +08:00
|
|
|
|
2014-03-15 11:32:35 +08:00
|
|
|
install_from_git() {
|
|
|
|
if [ -z "$NVM_SOURCE" ]; then
|
|
|
|
NVM_SOURCE="https://github.com/creationix/nvm.git"
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [ -d "$NVM_DIR/.git" ]; then
|
2014-03-26 16:08:37 +08:00
|
|
|
echo "=> nvm is already installed in $NVM_DIR, trying to update"
|
2014-03-15 11:32:35 +08:00
|
|
|
echo -e "\r=> \c"
|
2014-03-26 15:36:41 +08:00
|
|
|
cd "$NVM_DIR" && git pull 2> /dev/null || {
|
|
|
|
echo >&2 "Failed to update nvm, run 'git pull' in $NVM_DIR yourself.."
|
|
|
|
}
|
2014-03-15 11:32:35 +08:00
|
|
|
else
|
|
|
|
# Cloning to $NVM_DIR
|
2014-03-26 16:08:37 +08:00
|
|
|
echo "=> Downloading nvm from git to '$NVM_DIR'"
|
|
|
|
echo -e "\r=> \c"
|
2014-03-15 11:32:35 +08:00
|
|
|
mkdir -p "$NVM_DIR"
|
|
|
|
git clone "$NVM_SOURCE" "$NVM_DIR"
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
install_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"
|
2014-03-15 11:32:35 +08:00
|
|
|
fi
|
|
|
|
|
|
|
|
# Downloading to $NVM_DIR
|
2013-12-23 01:23:59 +08:00
|
|
|
mkdir -p "$NVM_DIR"
|
2014-03-15 11:32:35 +08:00
|
|
|
if [ -d "$NVM_DIR/nvm.sh" ]; then
|
2014-03-26 16:08:37 +08:00
|
|
|
echo "=> nvm is already installed in $NVM_DIR, trying to update"
|
2014-03-15 11:32:35 +08:00
|
|
|
else
|
2014-03-26 16:08:37 +08:00
|
|
|
echo "=> Downloading nvm as script to '$NVM_DIR'"
|
2014-03-15 11:32:35 +08:00
|
|
|
fi
|
2014-03-26 16:29:05 +08:00
|
|
|
curl -s "$NVM_SOURCE" -o "$NVM_DIR/nvm.sh" || {
|
2014-03-26 16:08:37 +08:00
|
|
|
echo >&2 "Failed to download '$NVM_SOURCE'.."
|
2014-03-15 11:32:35 +08:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-26 16:08:37 +08:00
|
|
|
if [ -z "$METHOD" ]; then
|
2014-03-26 16:16:26 +08:00
|
|
|
# Autodetect install method
|
|
|
|
if has "git"; then
|
|
|
|
install_from_git
|
2014-03-26 16:29:05 +08:00
|
|
|
elif has "curl"; then
|
2014-03-26 16:16:26 +08:00
|
|
|
install_as_script
|
|
|
|
else
|
|
|
|
echo >&2 "You need git, curl or wget to install nvm"
|
|
|
|
exit 1
|
|
|
|
fi
|
2014-03-15 11:32:35 +08:00
|
|
|
else
|
2014-03-26 16:08:37 +08:00
|
|
|
if [ "$METHOD" = "git" ]; then
|
2014-03-26 16:29:05 +08:00
|
|
|
if ! has "git"; then
|
2014-03-26 16:08:37 +08:00
|
|
|
echo >&2 "You need git to install nvm"
|
|
|
|
exit 1
|
2014-03-26 16:29:05 +08:00
|
|
|
fi
|
|
|
|
install_from_git
|
2014-03-26 16:08:37 +08:00
|
|
|
fi
|
|
|
|
if [ "$METHOD" = "script" ]; then
|
2014-03-26 16:29:05 +08:00
|
|
|
if ! has "curl"; then
|
2014-03-26 16:08:37 +08:00
|
|
|
echo >&2 "You need curl or wget to install nvm"
|
|
|
|
exit 1
|
2014-03-26 16:29:05 +08:00
|
|
|
fi
|
|
|
|
install_as_script
|
2014-03-26 16:08:37 +08:00
|
|
|
fi
|
2012-10-30 07:03:01 +08:00
|
|
|
fi
|
|
|
|
|
2013-02-27 17:50:08 +08:00
|
|
|
echo
|
2012-10-30 07:03:01 +08:00
|
|
|
|
2013-12-23 00:56:42 +08:00
|
|
|
# Detect profile file if not specified as environment variable (eg: PROFILE=~/.myprofile).
|
2014-03-15 11:32:35 +08:00
|
|
|
if [ -z "$PROFILE" ]; then
|
2012-10-30 07:03:01 +08:00
|
|
|
if [ -f "$HOME/.bash_profile" ]; then
|
2013-12-23 00:56:42 +08:00
|
|
|
PROFILE="$HOME/.bash_profile"
|
2013-10-11 01:30:53 +08:00
|
|
|
elif [ -f "$HOME/.zshrc" ]; then
|
2013-12-23 00:56:42 +08:00
|
|
|
PROFILE="$HOME/.zshrc"
|
2012-10-30 07:03:01 +08:00
|
|
|
elif [ -f "$HOME/.profile" ]; then
|
2013-12-23 00:56:42 +08:00
|
|
|
PROFILE="$HOME/.profile"
|
2012-10-30 07:03:01 +08:00
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
2014-03-26 16:08:37 +08:00
|
|
|
SOURCE_STR="[ -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
|
2013-02-27 17:50:08 +08:00
|
|
|
if [ -z $PROFILE ]; then
|
2013-12-23 01:05:20 +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
|
2013-12-23 01:05:20 +08:00
|
|
|
echo "=> Profile $PROFILE not found"
|
|
|
|
echo "=> Create it (touch $PROFILE) and run this script again"
|
2012-10-30 07:03:01 +08:00
|
|
|
fi
|
2013-12-23 01:05:20 +08:00
|
|
|
echo " OR"
|
|
|
|
echo "=> Append the following line to the correct file yourself:"
|
2013-07-26 19:58:47 +08:00
|
|
|
echo
|
2013-12-23 01:05:20 +08:00
|
|
|
echo " $SOURCE_STR"
|
2013-07-26 19:58:47 +08:00
|
|
|
echo
|
2012-10-30 07:03:01 +08:00
|
|
|
else
|
2013-12-23 01:05:20 +08:00
|
|
|
if ! grep -qc 'nvm.sh' $PROFILE; then
|
|
|
|
echo "=> Appending source string to $PROFILE"
|
|
|
|
echo "" >> "$PROFILE"
|
|
|
|
echo $SOURCE_STR >> "$PROFILE"
|
|
|
|
else
|
|
|
|
echo "=> Source string already in $PROFILE"
|
|
|
|
fi
|
2012-10-30 07:03:01 +08:00
|
|
|
fi
|
|
|
|
|
2014-03-26 16:08:37 +08:00
|
|
|
echo "=> Close and reopen your terminal to start using nvm"
|
2014-03-16 02:41:50 +08:00
|
|
|
|