Merge pull request #566 from xcambar/nvm_install_dir

[install script] Refactors NVM_DIR; adds tests
Jordan Harband 2016-06-28 23:17:27 -07:00 committed by GitHub
commit 39c119c5db
2 changed files with 55 additions and 22 deletions

View File

@ -6,9 +6,9 @@ nvm_has() {
type "$1" > /dev/null 2>&1
}
if [ -z "$NVM_DIR" ]; then
NVM_DIR="$HOME/.nvm"
fi
nvm_install_dir() {
echo ${NVM_DIR:-"$HOME/.nvm"}
}
nvm_latest_version() {
echo "v0.31.2"
@ -56,45 +56,50 @@ nvm_download() {
}
install_nvm_from_git() {
if [ -d "$NVM_DIR/.git" ]; then
echo "=> nvm is already installed in $NVM_DIR, trying to update using git"
local INSTALL_DIR
INSTALL_DIR="$(nvm_install_dir)"
if [ -d "$INSTALL_DIR/.git" ]; then
echo "=> nvm is already installed in $INSTALL_DIR, trying to update using git"
printf "\r=> "
cd "$NVM_DIR" && (command git fetch 2> /dev/null || {
echo >&2 "Failed to update nvm, run 'git fetch' in $NVM_DIR yourself." && exit 1
cd "$INSTALL_DIR" && (git fetch 2> /dev/null || {
echo >&2 "Failed to update nvm, run 'git fetch' in $INSTALL_DIR yourself." && exit 1
})
else
# Cloning to $NVM_DIR
echo "=> Downloading nvm from git to '$NVM_DIR'"
echo "=> Downloading nvm from git to '$INSTALL_DIR'"
printf "\r=> "
mkdir -p "$NVM_DIR"
command git clone "$(nvm_source git)" "$NVM_DIR"
mkdir -p "$INSTALL_DIR"
git clone "$(nvm_source)" "$INSTALL_DIR"
fi
cd "$NVM_DIR" && command git checkout --quiet "$(nvm_latest_version)"
if [ ! -z "$(cd "$NVM_DIR" && git show-ref refs/heads/master)" ]; then
cd "$INSTALL_DIR" && command git checkout --quiet "$(nvm_latest_version)"
if [ ! -z "$(cd "$INSTALL_DIR" && git show-ref refs/heads/master)" ]; then
if git branch --quiet 2>/dev/null; then
cd "$NVM_DIR" && command git branch --quiet -D master >/dev/null 2>&1
cd "$INSTALL_DIR" && command git branch --quiet -D master >/dev/null 2>&1
else
echo >&2 "Your version of git is out of date. Please update it!"
cd "$NVM_DIR" && command git branch -D master >/dev/null 2>&1
cd "$INSTALL_DIR" && command git branch -D master >/dev/null 2>&1
fi
fi
return
}
install_nvm_as_script() {
local INSTALL_DIR
INSTALL_DIR="$(nvm_install_dir)"
local NVM_SOURCE_LOCAL
NVM_SOURCE_LOCAL=$(nvm_source script)
local NVM_EXEC_SOURCE
NVM_EXEC_SOURCE=$(nvm_source script-nvm-exec)
# Downloading to $NVM_DIR
mkdir -p "$NVM_DIR"
if [ -f "$NVM_DIR/nvm.sh" ]; then
echo "=> nvm is already installed in $NVM_DIR, trying to update the script"
# Downloading to $INSTALL_DIR
mkdir -p "$INSTALL_DIR"
if [ -f "$INSTALL_DIR/nvm.sh" ]; then
echo "=> nvm is already installed in $INSTALL_DIR, trying to update the script"
else
echo "=> Downloading nvm as script to '$NVM_DIR'"
echo "=> Downloading nvm as script to '$INSTALL_DIR'"
fi
nvm_download -s "$NVM_SOURCE_LOCAL" -o "$NVM_DIR/nvm.sh" || {
nvm_download -s "$NVM_SOURCE_LOCAL" -o "$INSTALL_DIR/nvm.sh" || {
echo >&2 "Failed to download '$NVM_SOURCE_LOCAL'"
return 1
}
@ -228,8 +233,10 @@ nvm_do_install() {
local NVM_PROFILE
NVM_PROFILE=$(nvm_detect_profile)
local INSTALL_DIR
INSTALL_DIR="$(nvm_install_dir)"
SOURCE_STR="\nexport NVM_DIR=\"$NVM_DIR\"\n[ -s \"\$NVM_DIR/nvm.sh\" ] && . \"\$NVM_DIR/nvm.sh\" # This loads nvm"
SOURCE_STR="\nexport NVM_DIR=\"$INSTALL_DIR\"\n[ -s \"\$NVM_DIR/nvm.sh\" ] && . \"\$NVM_DIR/nvm.sh\" # This loads nvm"
if [ -z "$NVM_PROFILE" ] ; then
echo "=> Profile not found. Tried $NVM_PROFILE (as defined in \$PROFILE), ~/.bashrc, ~/.bash_profile, ~/.zshrc, and ~/.profile."
@ -261,7 +268,8 @@ nvm_do_install() {
nvm_reset() {
unset -f nvm_reset nvm_has nvm_latest_version \
nvm_source nvm_download install_nvm_as_script install_nvm_from_git \
nvm_detect_profile nvm_check_global_modules nvm_do_install
nvm_detect_profile nvm_check_global_modules nvm_do_install \
nvm_install_dir
}
[ "_$NVM_ENV" = "_testing" ] || nvm_do_install

View File

@ -0,0 +1,25 @@
#!/bin/sh
cleanup () {
unset -f die cleanup
unset install_dir
}
die () { echo $@ ; cleanup ; exit 1; }
NVM_ENV=testing . ../../install.sh
HOME="__home__"
# NVM_DIR is set
NVM_DIR="some_dir"
install_dir=$(nvm_install_dir)
[ "_$install_dir" = "_$NVM_DIR" ] || die "nvm_install_dir should use \$NVM_DIR if it exists. Current output: $install_dir"
unset NVM_DIR
# NVM_DIR is not set
install_dir=$(nvm_install_dir)
[ "_$install_dir" = "_$HOME/.nvm" ] || die "nvm_install_dir should default to \$HOME/.nvm. Current output: $install_dir"
cleanup