Skip to content

Instantly share code, notes, and snippets.

@puckbag
Last active January 7, 2017 22:59
Show Gist options
  • Save puckbag/2e8b3c68bd21ed895ee793d8bc97f04d to your computer and use it in GitHub Desktop.
Save puckbag/2e8b3c68bd21ed895ee793d8bc97f04d to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
# Installation
#
# $ bash <(curl -s https://gist.githubusercontent.com/puckbag/2e8b3c68bd21ed895ee793d8bc97f04d/raw/install-nodenv.sh)
#
# Reference
#
# https://github.com/nodenv/nodenv
# https://github.com/nodenv/node-build
# https://github.com/nodenv/nodenv-update
#
echo_error () {
echo; echo "$(tput setaf 1)$@$(tput sgr0)"; echo
}
echo_info () {
echo; echo "$(tput setaf 2)$@$(tput sgr0)"; echo
}
echo_alert () {
echo; echo "$(tput setaf 3)$@$(tput sgr0)"; echo
}
if [ -d ~/.nodenv ]; then
echo_error 'Nodenv already installed'
exit 1
fi
PROFILE_FILE=''
if [ -f "$HOME/.bash_profile" ]; then
PROFILE_FILE="$HOME/.bash_profile"
elif [ -f "$HOME/.bash_login" ]; then
PROFILE_FILE="$HOME/.bash_login"
elif [ -f "$HOME/.profile" ]; then
PROFILE_FILE="$HOME/.profile"
fi
CWD=`pwd`
# Install nodenv/nodenv
echo_info 'Install nodenv/nodenv'
git clone https://github.com/nodenv/nodenv.git ~/.nodenv
cd ~/.nodenv && src/configure && make -C src
cd $CWD
# Inject nodenv into this environment
echo_info 'Inject nodenv into this environment'
export PATH="$HOME/.nodenv/bin:$PATH"
eval "$(nodenv init -)"
# Save nodenv into environment scripts
if [[ -z "$PROFILE_FILE" ]]; then
echo_alert 'BASH profile file could not be established.'
else
echo_info "Save nodenv into profile file: $PROFILE_FILE"
if ! grep -q '.nodenv/bin' "$PROFILE_FILE"; then
echo 'export PATH="$HOME/.nodenv/bin:$PATH"' >> "$PROFILE_FILE"
fi
if ! grep -q 'nodenv init' "$PROFILE_FILE"; then
echo 'eval "$(nodenv init -)"' >> "$PROFILE_FILE"
fi
fi
# Install nodenv plugin: node-build
echo_info 'Install nodenv plugin: node-build'
git clone https://github.com/nodenv/node-build.git $(nodenv root)/plugins/node-build
# Install nodenv plugin: node-update
echo_info 'Install nodenv plugin: node-update'
git clone https://github.com/nodenv/nodenv-update.git "$(nodenv root)"/plugins/nodenv-update
# Update nodenv and plugins
echo_info 'Update nodenv and plugins'
nodenv update
# Note about shell environment
echo_alert 'You must restart your shell or run the following:'
echo ' $ export PATH="$HOME/.nodenv/bin:$PATH" && eval "$(nodenv init -)"'
echo
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment