Last active
August 16, 2016 10:14
-
-
Save pmarques/ba9ae79c76a92b908875e071386b5615 to your computer and use it in GitHub Desktop.
Simple Node version control
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# How to use it | |
# copy the code into .bashrc our download the .bash_node_v file into your home | |
# and add to your .bashrc or .bash_profile: | |
# source .bash_node_v | |
# | |
# Configure it creating a .nrc (in your home or in the same folder you put this file) and add | |
# the folder where you want to install the node versions, for instance: | |
# NODE_PREFIX="/Software/node/" | |
# | |
CONFIGS=~/.nrc | |
[ -f ${CONFIGS} ] || { | |
echo -e "\033[33mNo .nrc file found in your home to configure node_v\033[0m" | |
# Do not exit otherwise it will terminate the shell | |
# exit -2 | |
return | |
} | |
source ${CONFIGS} | |
ORIG_PATH=$PATH | |
n() { | |
if [ "$#" -lt 1 ]; then | |
echo -e "\033[94mNode version $(node -v)\033[0m" | |
return 1 | |
fi | |
local version=$1 | |
local arch=$(uname -s | tr '[:upper:]' '[:lower:]') | |
arch="${arch}-x64" | |
local node_prefix="${NODE_PREFIX}/node-v${version}-${arch}" | |
local fix_node_path="${node_prefix}/lib/node" | |
if [ -d "${NODE_PREFIX}/node-v${version}-${arch}" ] ; then | |
echo -e "\033[32mUsing ${version}\033[0m" | |
export PATH="${node_prefix}/bin:$ORIG_PATH" | |
export NPM_CONFIG_PREFIX=${node_prefix} | |
# we can export NODE_PATH but this can be overwriten by other person | |
# or scripts.... to prevent any issue lets just create a link ^.^ | |
# export NODE_PATH="${node_prefix}/lib/node_modules" | |
if [ ! -L "${fix_node_path}" ]; then | |
if [ -e "${fix_node_path}" ]; then | |
echo -e "\033[31m${fix_node_path} expected to be a sym link!\033[0m" | |
return 1 | |
else | |
ln -s "${fix_node_path}_modules" "${fix_node_path}" || { echo -e "\033[31mFailed to creat sym link ${fix_node_path}!\033[0m" ; return 1 ; } | |
fi | |
fi | |
else | |
echo -e "\033[31mVersion not found!\033[0m" | |
echo -e "\033[33mWe will try to install it!\033[0m" | |
pushd ${NODE_PREFIX} | |
local url="https://nodejs.org/dist/v${version}/node-v${version}-${arch}.tar.gz" | |
local file="node-v${version}-${arch}.tar.gz" | |
# Download tar.gx and test if 200 ^.^ | |
local httpStatusCode=$(curl "${url}" -o "${file}" -w "%{http_code}") | |
[ $httpStatusCode -ne 200 ] && { echo -e "\033[31mVersion ${version} not found on server! [${url}]\033[0m" ; return 1 ; } | |
# Extract it | |
tar -xf "${file}" || { echo -e "\033[31mError extraction file ${NODE_PREFIX}/$file\033[0m" ; return 1 ; } | |
popd | |
if [ "$2" == "retry" ] ; then | |
echo -e "\033[33mRun:\nn ${version}\033[0m" | |
return 1 | |
else | |
n $version "retry" | |
fi | |
fi | |
} | |
export n |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment