Skip to content

Instantly share code, notes, and snippets.

@ttonyh
Last active October 17, 2021 15:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ttonyh/cc21e90990810ee53734ec28e3af9534 to your computer and use it in GitHub Desktop.
Save ttonyh/cc21e90990810ee53734ec28e3af9534 to your computer and use it in GitHub Desktop.
Install Node
#!/bin/bash
# *** RUN AS ROOT ***
# Define help function
function help(){
echo "Node JS Install Script - Install Node JS";
echo "Usage example:";
echo "install-node (-v|--version) string [(-f|--force)] [(-p|--platform) string] [(-a|--arch) string]";
echo "Options:";
echo "-v or --version string: Node JS Version to install. Required.";
echo "-f or --force: Force install (re-install same version).";
echo "-p or --platform string: Platform (defaults to linux).";
echo "-a or --arch string: Processor Architecture Platform (defaults to x64).";
exit 1;
}
# Declare vars. Flags initalizing to 0.
force=false;
# Execute getopt
ARGS=$(getopt -o "v:fp:a:" -l "version:,force,platform:,arch:" -n "Node JS Install Script" -- "$@");
#Bad arguments
if [ $? -ne 0 ];
then
help;
fi
eval set -- "$ARGS";
while true; do
case "$1" in
-v|--version)
shift;
if [ -n "$1" ];
then
version="$1";
shift;
fi
;;
-f|--force)
shift;
force=true;
;;
-p|--platform)
shift;
if [ -n "$1" ];
then
platform="$1";
shift;
fi
;;
-a|--arch)
shift;
if [ -n "$1" ];
then
arch="$1";
shift;
fi
;;
--)
shift;
break;
;;
esac
done
# Check required arguments
if [ -z "$version" ]
then
echo "You must specify a Node JS version number";
help;
fi
if [ -z "$platform" ]; then
PLATFORM=linux
else
PLATFORM=$platform
fi
if [ -z "$arch" ]; then
ARCH=x64
else
ARCH=$arch
fi
# Iterate over rest arguments called $arg
for arg in "$@"
do
# Your code here (remove example below)
echo $arg
done
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
SEMVER_REGEX="^(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)(\-[0-9A-Za-z-]+(\.[0-9A-Za-z-]+)*)?(\+[0-9A-Za-z-]+(\.[0-9A-Za-z-]+)*)?$"
function validate_version {
if [[ "$version" =~ $SEMVER_REGEX ]]; then
# if a second argument is passed, store the result in var named by $2
if [ "$#" -eq "2" ]; then
local major=${BASH_REMATCH[1]}
local minor=${BASH_REMATCH[2]}
local patch=${BASH_REMATCH[3]}
local prere=${BASH_REMATCH[4]}
local build=${BASH_REMATCH[5]}
eval "$2=(\"$major\" \"$minor\" \"$patch\" \"$prere\" \"$build\")"
else
echo "$version"
fi
else
echo "Version $version does not match the semver scheme 'X.Y.Z(-PRERELEASE)(+BUILD)'"
exit 2
fi
}
# Validate Version
validate_version
# Download & Unpack Node JS
CURRENT=$(node -v | cut -c 2-)
if [ "$arch" == "armv6l" ]; then
BASEURL=https://unofficial-builds.nodejs.org/download/release
else
BASEURL=https://nodejs.org/dist
fi
URL=$BASEURL/v$version/node-v$version-$PLATFORM-$ARCH.tar.gz
echo "* Requested Version: $version"
echo "* Current Version: $CURRENT"
if [ "$force" == false ] && [ $version == $CURRENT ]; then
echo '!! Node version is the same, no need to download and install.'
exit 0
fi
echo 'Download and Unpack Node.js'
curl -# -L "$URL" | tar xzf - --strip-components=1 -C /usr/local
# echo "Install Node Globals"
# npm install -g gulp-cli gulp
echo 'Node.js install completed'
exit 0
#!/bin/bash
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
echo "Preparing to UNINSTALL current version of NodeJs: $(node -v)"
echo "(ctrl+c to cancel)"
# Chance to cancel before uninstalling
sleep 5
# Uninstall Old Version
rm -rf /usr/local/bin/node
rm -rf /usr/local/bin/npm
rm -rf ~/.npm
rm -rf ~/.node-gyp
rm -rf /usr/local/share/man/man1/node.1
rm -rf /usr/local/share/systemtap/tapset/node.stp
rm -rf /usr/local/lib/node
rm -rf /usr/local/lib/node_modules
rm -rf /usr/local/lib/dtrace/node.d
rm -rf /var/db/receipts/org.nodejs.*
# Remove dead symbolic links
find -L /usr/local/bin -name . -o -type l -delete
echo "Uninstall DONE"
#!/bin/bash
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
./remove-node-rpi.sh
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
sleep 1
echo "Installing NODE $@"
./install-node.sh -a $(uname -m) -v $@
echo "Install DONE"
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
sleep 0.5
echo "Verifying..."
sleep 0.2
echo "Requested Node Version: $@"
sleep 0.2
echo "Installed Node Version: $(node -v)"
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
sleep 1
echo "Installing Global Modules"
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
sleep 1
echo "Upgrade NPM"
npm install -g npm
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
sleep 1
# Install Globals
npm install -g forever nodemon npm-check-updates pm2
echo "Installing Global Modules DONE"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment