From 0085880273890a344bbc8c696713d10c2fc40497 Mon Sep 17 00:00:00 2001 From: Brandon Rodriguez <brodriguez8774@gmail.com> Date: Thu, 10 Nov 2022 21:19:04 -0500 Subject: [PATCH] Add npm setup script --- run.sh | 10 +++++++ src/setup_npm.sh | 75 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 85 insertions(+) create mode 100755 src/setup_npm.sh diff --git a/run.sh b/run.sh index 25df592..b3f7c91 100755 --- a/run.sh +++ b/run.sh @@ -65,6 +65,15 @@ function main () { echo -e "${text_blue}This may ask for your system password.${text_reset}" get_user_confirmation "${text_blue}This will also trigger a general system update. Is this okay?${text_reset}" + if [[ ${return_value} == true ]] + then + sudo ./setup_npm.sh + else + echo "" + echo -e "${text_blue}Cancelling Npm/NodeJs setup." + echo "" + fi + elif [[ "${user_input}" == "0" ]] then # Exit program. @@ -76,6 +85,7 @@ function main () { # Handle unrecognized input. echo "" echo -e "${text_red}Unrecognized selection.${text_reset}" + echo "" fi diff --git a/src/setup_npm.sh b/src/setup_npm.sh new file mode 100755 index 0000000..091e769 --- /dev/null +++ b/src/setup_npm.sh @@ -0,0 +1,75 @@ +#!/usr/bin/env bash +### + # Fully install + update npm. + # Because the commands are annoying to remember if you don't use them regularly. + # Also as of 2020/2021-ish, it seems like certain machine/installation setups are + # buggy to update without this exact command ordering. + ## + + +# Abort on error +set -e + + +# Import utility script. +source $(dirname $0)/utils.sh + + + +### + # Program main. + ## +function main () { + + echo "" + echo -e "${text_blue}Installing/updating Npm & NodeJs.${text_reset}" + echo -e "${text_blue}This will make sure your system has the latest LTS version.${text_reset}" + echo -e "${text_blue}This may take a few minutes, depending on your system and connection...${text_reset}" + echo "" + + # Make sure we are root. + check_is_user "root" + + # Install base node/npm packages. + curl -fsSL https://deb.nodesource.com/setup_lts.x -y > /dev/null 2>&1 | sudo -E bash - + apt-get update -y > /dev/null 2>&1 + apt-get upgrade -y > /dev/null 2>&1 + apt-get install nodejs -y > /dev/null 2>&1 + apt-get install npm -y > /dev/null 2>&1 + + # Note that at this point, depending on the system, this might provide an old/outdated version. + # So it's installed, but we have to do additional commands to get the proper latest version. + # Basically, we want to use npm's built-in version manager. So as long as we get ANY version of + # npm, then we can get to the proper version with said built-in version manager. + + # Install NodeJs "version manager". + npm install -g n > /dev/null 2>&1 + + # Verify we have the latest stable major version of npm. + n stable > /dev/null 2>&1 + + # Try again to update to latest version of npm. + # This is usually unecessary, but required in some systems with certain configurations, + # Otherwise, it doesn't seem to *actually* get to the proper version until running commands again. + # Aka, this is a workaround for an obscure update bug in some systems. + npm install -g npm@latest > /dev/null 2>&1 + + # Install update checker for npm. + npm install -g npm-check-updates > /dev/null 2>&1 + + echo -e "${text_blue}NodeJs, Npm, and React dependencies installed.${text_reset}" + echo -e " ${text_purple}NodeJs Version${text_reset}: $(node -v)" + echo -e " ${text_purple}Npm Version${text_reset}: $(npm -v)" + echo -e " ${text_purple}React Version${text_reset}: $(npm view react version)" + echo "" + echo -e "${text_blue}The npm-check-updates package has also been installed.${text_reset}" + echo -e "${text_blue}This allows for quick package updating per-project.${text_reset}" + echo -e "${text_blue}Run ${text_cyan}ncu${text_blue} to see what can upgrade.${text_reset}" + echo -e "${text_blue}Run ${text_cyan}ncu -u${text_blue} to commit upgrades to project package.json file.${text_reset}" + echo -e "${text_blue}Run ${text_cyan}ncu <package_name>${text_blue} to upgrade only a single package.${text_reset}" + echo "" + +} + + +main -- GitLab