Skip to content

Instantly share code, notes, and snippets.

@huobazi
Forked from ttonyh/install-node.sh
Created November 5, 2017 02:11
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 huobazi/e222bb592bd1718670f40598a9941a22 to your computer and use it in GitHub Desktop.
Save huobazi/e222bb592bd1718670f40598a9941a22 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-)
URL=http://nodejs.org/dist/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
./install-node -a armv6l -v $@
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment