Skip to content

Commit

Permalink
Refactor, add verbose mode (#5)
Browse files Browse the repository at this point in the history
* Refactor, add verbose mode

* Remove unused function
  • Loading branch information
Jan Kotrlik authored Jul 28, 2023
1 parent 639ce5f commit 81c42c8
Showing 1 changed file with 133 additions and 115 deletions.
248 changes: 133 additions & 115 deletions install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,28 @@ mkdir -p /usr/local/bin
cat > "$INSTALLER_PATH" << 'EOF'
#!/bin/bash
set -e
set -euo pipefail
readonly TMP_DIR=$( mktemp -d )
print_help() {
function clean_on_exit() {
rm -rf $TMP_DIR
}
function print_help() {
cat <<'eof'
Usage as root or with sudo:
node_installer 8.9.1 # this installs 8.9.1 version
node_installer v8.9.1 # this installs 8.9.1 version
node_installer v8.9.1 --verbose # enables set -x
node_installer v8.9.1 --yarn --npx # no-op options. Present for compatibility. Yarn and npx are always installed.
node_installer v8.9.1 --cache /path # stores fetched install files to cache location. Using those and not fetching if present.
node_installer 8.9.1 # this installs 8.9.1 version
node_installer clean # this cleans your system from older installations done via this script
node_installer help # prints this help
eof
}
clean_previous_installations() {
function clean_previous_installations() {
echo "Cleaning previous node installations"
rm -f /usr/local/bin/node
rm -f /usr/local/bin/npm
Expand All @@ -36,29 +44,30 @@ clean_previous_installations() {
rm -rf /usr/local/lib/node_modules
}
download() {
function download() {
local node=$1
local target=$2
echo "Downloading"
if `which wget > /dev/null` ; then
wget -q https://nodejs.org/dist/"$NODE"/"$TARGET".tar.gz
wget -q https://nodejs.org/dist/"$node"/"$target".tar.gz
elif `which curl > /dev/null` ; then
curl -sS -o "$TARGET".tar.gz https://nodejs.org/dist/"$NODE"/"$TARGET".tar.gz
curl -sS -o "$target".tar.gz https://nodejs.org/dist/"$node"/"$target".tar.gz
else
echo "wget or curl command not found"
exit 4
fi || {
echo "node version not found"
echo "please provide existing version"
rm -rf "$TMP_DIR"
exit 5
}
}
looks_like_version() {
function looks_like_version() {
[[ "$1" =~ ^v?[0-9]+\.[0-9]+\.[0-9]+$ ]]
}
handle_positional_argument() {
function handle_positional_argument() {
if [[ -z "${NODE:-}" ]] ; then
if looks_like_version "$1" ; then
NODE="$1"
Expand All @@ -77,124 +86,133 @@ handle_positional_argument() {
fi
}
if [[ "$1" == 'help' ]] || [[ -z "$1" ]] ; then
print_help
exit 0
fi
function main() {
[[ "$*" != *"--verbose"* ]] || set -x
trap clean_on_exit 0
if [[ $EUID -ne 0 ]]; then
echo "This script must be run as root"
print_help
exit 1
fi
if [[ "$1" == 'help' ]] || [[ -z "$1" ]] ; then
print_help
exit 0
fi
if [[ "$1" == clean ]] ; then
clean_previous_installations
exit 0
fi
if [[ $EUID -ne 0 ]]; then
echo "This script must be run as root"
print_help
exit 1
fi
# Defaults for options
NODE=
INSTALL_FILES_CACHE=
while [[ "$#" -ne 0 ]] ; do
case "$1" in
-c | --cache)
INSTALL_FILES_CACHE="$2"
[[ -n "${INSTALL_FILES_CACHE}" ]] || {
echo "Cache path is empty"
exit 1
}
echo "Setting cache folder: $INSTALL_FILES_CACHE"
shift 2
;;
if [[ "$1" == clean ]] ; then
clean_previous_installations
exit 0
fi
-y | --yarn)
echo "Yarn is always installed. '--yarn' is deprecated."
shift
;;
# Defaults for options
local NODE INSTALL_FILES_CACHE
while [[ "$#" -ne 0 ]] ; do
case "$1" in
-c | --cache)
INSTALL_FILES_CACHE="$2"
[[ -n "${INSTALL_FILES_CACHE}" ]] || {
echo "Cache path is empty"
exit 1
}
echo "Setting cache folder: $INSTALL_FILES_CACHE"
shift 2
;;
-y | --yarn)
echo "Yarn is always installed. '--yarn' is deprecated."
shift
;;
-n | --npx)
echo "Npx is always installed. '--npx' is deprecated."
shift
;;
-v | --verbose)
echo "Verbose mode active."
shift
;;
*)
handle_positional_argument "$1"
shift
;;
esac
done
if [[ -z "${NODE}" ]] ; then
echo "Provide node version"
exit 1
fi
-n | --npx)
echo "Npx is always installed. '--npx' is deprecated."
shift
;;
if ! [[ $NODE =~ ^v ]] ; then
NODE=v$NODE
fi
if [[ ! "$NODE" =~ ^v[0-9]+\.[0-9]+\.[0-9]+$ ]] ; then
echo "Please provide existing node version in 'X.Y.Z' or 'vX.Y.Z' format"
exit 2
fi
readonly NODE
*)
handle_positional_argument "$1"
shift
local TARGET=
case $(uname) in
"Linux")
TARGET="node-${NODE}-linux-x64"
;;
"Darwin")
TARGET="node-${NODE}-darwin-x64"
;;
*)
echo "System not supported"
exit 3
;;
esac
done
if [[ -z "${NODE}" ]] ; then
echo "Provide node version"
exit 1
fi
if ! [[ $NODE =~ ^v ]] ; then
NODE=v$NODE
fi
if [[ ! "$NODE" =~ ^v[0-9]+\.[0-9]+\.[0-9]+$ ]] ; then
echo "Please provide existing node version in 'X.Y.Z' or 'vX.Y.Z' format"
exit 2
fi
clean_previous_installations
TARGET=
case $(uname) in
"Linux")
TARGET="node-${NODE}-linux-x64"
;;
"Darwin")
TARGET="node-${NODE}-darwin-x64"
;;
*)
echo "System not supported"
exit 3
;;
esac
clean_previous_installations
export TMP_DIR=$( mktemp -d )
if [[ -z "$INSTALL_FILES_CACHE" ]] ; then
cd "$TMP_DIR"
download
else
mkdir -p -- "$INSTALL_FILES_CACHE"
cd -- "$INSTALL_FILES_CACHE"
if [[ -f "$TARGET.tar.gz" ]] ; then
echo "Using $TARGET.tar.gz from cache"
if [[ -z "${INSTALL_FILES_CACHE:-}" ]] ; then
cd "$TMP_DIR"
download $NODE $TARGET
else
echo "No $TARGET.tar.gz in cache"
rm -rf "$TARGET.tar.gz"
download
mkdir -p -- "$INSTALL_FILES_CACHE"
cd -- "$INSTALL_FILES_CACHE"
if [[ -f "$TARGET.tar.gz" ]] ; then
echo "Using $TARGET.tar.gz from cache"
else
echo "No $TARGET.tar.gz in cache"
rm -rf "$TARGET.tar.gz"
download $NODE $TARGET
fi
cp "$TARGET.tar.gz" "$TMP_DIR"
fi
cp "$TARGET.tar.gz" "$TMP_DIR"
fi
echo "Installing"
cd "$TMP_DIR"
tar xf "$TARGET".tar.gz
rm -rf "$TARGET".tar.gz
cp -r "$TARGET"/bin/node /usr/local/bin/
mkdir -p /usr/local/lib
cp -r "$TARGET"/lib/node_modules /usr/local/lib/
cd /usr/local/bin
ln -s ../lib/node_modules/npm/bin/npm-cli.js npm
chmod +x node npm
echo "Linking npx"
rm -f npx
ln -s ../lib/node_modules/npm/bin/npx-cli.js npx
chmod +x npx
echo "Installing yarn"
npm install -g yarn >/dev/null
rm -rf "$TMP_DIR"
echo "Node version "$NODE" successfully installed"
echo "Installing"
cd "$TMP_DIR"
tar xf "$TARGET".tar.gz
rm -rf "$TARGET".tar.gz
cp -r "$TARGET"/bin/node /usr/local/bin/
mkdir -p /usr/local/lib
cp -r "$TARGET"/lib/node_modules /usr/local/lib/
cd /usr/local/bin
ln -s ../lib/node_modules/npm/bin/npm-cli.js npm
chmod +x node npm
echo "Linking npx"
rm -f npx
ln -s ../lib/node_modules/npm/bin/npx-cli.js npx
chmod +x npx
echo "Installing yarn"
npm install -g yarn >/dev/null
echo "Node version "$NODE" successfully installed"
}
main "$@"
EOF

chmod +x "$INSTALLER_PATH"
Expand Down

0 comments on commit 81c42c8

Please sign in to comment.