Skip to content

Instantly share code, notes, and snippets.

@ScottDillman
Last active January 16, 2019 14:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ScottDillman/da96abb1301713cb73e609f6fabd650d to your computer and use it in GitHub Desktop.
Save ScottDillman/da96abb1301713cb73e609f6fabd650d to your computer and use it in GitHub Desktop.
set up isolated node env
#!/bin/bash
#===============================================================================
#title : node_env.sh
#description : This script will generate an isolated Node.js environment
#date : 20180912
#version : 0.1
#usage : bash node_env.sh
#notes : Edit user settings before running if needed
#bash_version : 4.3.48(1)-release
## REQUIREMENS:
## 7zip on Windows ( https://www.7-zip.org/ )
## wget on all platforms ( https://www.gnu.org/software/wget/)
## jq on all platforms ( https://stedolan.github.io/jq/ )
#===============================================================================
## latest node can be found by looking at:
## https://nodejs.org/dist/index.json
## =============================================================================
## =============================================================================
## USER SETTINGS
## =============================================================================
NODE_DIST=https://nodejs.org/dist/
NODE_PATH=$PWD/node
## =============================================================================
## DO NOT EDIT BELOW THIS
## =============================================================================
## =============================================================================
red="\033[0;31m"
green="\033[0;32m"
yellow="\033[0;33m"
blue="\033[0;34m"
white="\033[1;37m"
TOA="\033[0m" # No Color
function logStd () {
echo -e "${white}$1${TOA}" 1>&2
}
function logError () {
echo -e "${red}$1${TOA}" 1>&2
}
function logWarn () {
echo -e "${yellow}$1${TOA}" 1>&2
}
function logInfo () {
echo -e "${green}$1${TOA}" 1>&2
}
function logDebug () {
echo -e "${blue}$1${TOA}" 1>&2
}
function logBanner(){
logInfo "============================================================================="
logStd "= ${1}"
logInfo "============================================================================="
}
NODE_ARCH=$(uname -m)
NODE_ARCH=${NODE_ARCH/86_}
NODE_OS=$OSTYPE
if [[ $OSTYPE = *"msys"* ]]; then
NODE_OS='win'
fi
if [[ $OSTYPE = *"cygwin"* ]]; then
NODE_OS='win'
fi
if [[ $OSTYPE = *"win32"* ]]; then
NODE_OS='win'
fi
if [[ $OSTYPE = *"linux"* ]]; then
NODE_OS='linux'
fi
if [[ $OSTYPE = *"darwin"* ]]; then
NODE_OS='darwin'
fi
depends="true"
logBanner "Beginning Node js install on ${NODE_OS}"
if [[ $NODE_OS = "win" ]]; then
hash 7z 2>/dev/null
if [[ $? -ne 0 ]]; then
logError 'Error: 7z is not installed.'
logInfo 'You can download the command line version of 7Zip from https://www.7-zip.org/download.html'
depends="false"
fi
else
hash jq 2>/dev/null
if [[ $? -ne 0 ]]; then
logError 'Error: jq is not installed.'
logInfo 'You can download the command line version of jq from https://stedolan.github.io/jq'
depends="false"
else
jq_ver=$(jq --version | cut -d'-' -f 2)
requiredver="1.5"
if [ "$(printf '%s\n' "$requiredver" "$jq_ver" | sort -V | head -n1)" = "$requiredver" ]; then
echo "jq meets version requirements for this script"
else
echo "jq must be version $requiredver or greater to work with this script"
depends="false"
fi
fi
hash wget 2>/dev/null
if [[ $? -ne 0 ]]; then
logError 'Error: wget is not installed.'
if [[ $NODE_OS = "win" ]]; then
logInfo 'You can download the command line version of wget from http://gnuwin32.sourceforge.net/packages/wget.htm'
else
logInfo 'You should install wget using your OS package manager of choice..'
fi
depends="false"
fi
fi
if [[ "${depends}" != "true" ]]; then
logError "THERE ARE MISSIGN DEPENDENCIES!!"
exit -1
fi
## get node versions available
version_json=$(wget --quiet -O- "https://nodejs.org/dist/index.json")
## override node version from command line
if [[ "$1" != "" ]]; then
NODE_VER=$1
logInfo "Looking for node version: [${NODE_VER}]"
echo ${version_json} | jq -e '.[] | select(.version | contains("'${NODE_VER}'"))' > /dev/null
if [ $? -eq 0 ]; then
echo OK
else
logError "Error: Node version [${NODE_VER}] could not be found..."
echo "Availble versions include:"
echo
versions=$(echo ${version_json} | jq -e '.[].version')
echo ${versions}
exit -1
fi
else
### get latest node version
version=$(echo ${version_json} | jq -e 'first(.[].version)')
version="${version%\"}"
NODE_VER=${version#\"}
logInfo "Latest node version is : ${NODE_VER}"
fi
set -e
## some settings to help with errors
if [[ $NODE_OS != "win" ]]; then
set -o errexit -o nounset -o noclobber -o pipefail
shopt -s nullglob
set -e
fi
# support Linux and Windows (MSYS2 bash) environments
## requires tar or 7z with wget
if [[ $NODE_OS != "win" ]]; then NODE_PATH="$NODE_PATH/bin"; fi
NPM_CONFIG_ROOT=$PWD/npmrc
## clean up existing installs
rm -f boot_node.sh
rm -rf $PWD/npmrc
rm -rf node
## create download url
if [[ $NODE_OS = "win" ]]; then
filestr="node-$NODE_VER-$NODE_OS-${NODE_ARCH}.7z"; output="node.7z";
else
filestr="node-${NODE_VER}-$NODE_OS-${NODE_ARCH}.tar.xz"; output="node.tar.xz";
fi
## download node binary package
wget ${NODE_DIST}${NODE_VER}/${filestr} -O ${output}
## extract to node install dir
if [[ $NODE_OS = "win" ]]; then
7z x node.7z; mv node-${NODE_VER}-${NODE_OS}-${NODE_ARCH} node; rm node.7z;
else
tar xvf node.tar.xz; mv node-${NODE_VER}-${NODE_OS}-${NODE_ARCH} node; rm node.tar.xz;
fi
echo '#!/bin/bash' > boot_node.sh
echo export PATH=\"$NODE_PATH:$PWD/node:$PWD/node_modules/.bin:$PWD/npmrc/.npm_global:$PWD/npmrc/.npm_global/bin:$PATH\" >> boot_node.sh
echo export NPM_CONFIG_ROOT=$NPM_CONFIG_ROOT >> boot_node.sh
echo export NPM_CONFIG_USERCONFIG=$NPM_CONFIG_ROOT/.npmrc >> boot_node.sh
echo export NPM_CONFIG_GLOBALCONFIG=$NPM_CONFIG_ROOT/.npmrc >> boot_node.sh
echo export NPM_CONFIG_USERPREFIX=$NPM_CONFIG_ROOT/.npm_user >> boot_node.sh
echo export NPM_CONFIG_PREFIX=$NPM_CONFIG_ROOT/.npm_global >> boot_node.sh
echo export NPM_CONFIG_CACHE=$NPM_CONFIG_ROOT/.npm >> boot_node.sh
source ./boot_node.sh
npm install yarn -g
echo -e '\n\n============================================================================='
echo '============================================================================='
node --version
echo '============================================================================='
which node
which yarn
which npm
echo '============================================================================='
logWarn "\n\n*** PLEASE RUN [ source ./boot_node.sh ] to set up your path and vars for this
env...\n\n"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment