Skip to content

Instantly share code, notes, and snippets.

@sidneys
Last active July 25, 2020 13:54
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save sidneys/e27eb239fd022d3e5204 to your computer and use it in GitHub Desktop.
Save sidneys/e27eb239fd022d3e5204 to your computer and use it in GitHub Desktop.
Node & NPM Global Updater: Updates NodeJS, NPM and all global Packages to 'latest' in one step
#!/bin/bash
# NVM_NODE_UPDATER
# v2.0.0
#
# Makes keeping NVM-managed, global NodeJS installations up-to-date a breeze.
# First, the global NodeJS installation is updated to 'latest'.
# Second, all global NPM packages are migrated, then also updated to 'latest'.
# Requires the Node Version Manager (https://github.com/creationix/nvm).
#
nvm_node_updater () {
# Check for latest NodeJS version
NODE_VERSION_INSTALLED=$(nvm_ls_current)
NODE_VERSION_LIST=($(nvm_remote_versions | awk '/^v/'))
NODE_VERSION_LATEST=${NODE_VERSION_LIST[${#NODE_VERSION_LIST[@]} - 1]}
# Pretty Print
function PRINT_BLUE () {
while read; do
printf '\e[44m%s\e[0m\n' "[STASH NODE-UPDATE] $REPLY";
done
}
if [ "${NODE_VERSION_INSTALLED}" != "${NODE_VERSION_LATEST}" ]
then
# Update to latest NodeJS version (migrating all currently installed global NPM packages)
echo "Updating global NodeJS: ${NODE_VERSION_INSTALLED} to ${NODE_VERSION_LATEST}" | PRINT_BLUE
nvm install ${NODE_VERSION_LATEST} --reinstall-packages-from=${NODE_VERSION_INSTALLED}
nvm alias default ${NODE_VERSION_LATEST} && nvm use default
echo "Updated default NodeJS to ${NODE_VERSION_LATEST}." | PRINT_BLUE
else
echo "Global NodeJS installation is up to date:" | PRINT_BLUE
echo "-> ${NODE_VERSION_INSTALLED}" | PRINT_BLUE
fi
# Check for latest NPM package versions
NPM_GLOBAL_PACKAGES_LIST=($(npm --global list --parseable --depth=0 --loglevel silent | sed -e 's@.*/@@' | tr '\r\n' ' ' | sed -e 's/lib//g' -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//'))
NPM_GLOBAL_PACKAGES_COUNT=${#NPM_GLOBAL_PACKAGES_LIST[@]}
NPM_GLOBAL_PACKAGES=$( IFS=' '; echo "${NPM_GLOBAL_PACKAGES_LIST[*]}" )
NPM_GLOBAL_PACKAGES_OUTDATED_LIST=($(npm --global outdated --parseable --loglevel silent | cut -d: -f3 | cut -f1 -d'@' | tr '\r\n' ' ' | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//'))
NPM_GLOBAL_PACKAGES_OUTDATED_COUNT=${#NPM_GLOBAL_PACKAGES_OUTDATED_LIST[@]}
NPM_GLOBAL_PACKAGES_OUTDATED=$( IFS=' '; echo "${NPM_GLOBAL_PACKAGES_OUTDATED_LIST[*]}" )
if [ -n "$NPM_GLOBAL_PACKAGES_OUTDATED_LIST" ]
then
# Update to latest NPM package versions
echo "Number of global packages requiring update: ${NPM_GLOBAL_PACKAGES_OUTDATED_COUNT}" | PRINT_BLUE
echo "-> ${NPM_GLOBAL_PACKAGES_OUTDATED}" | PRINT_BLUE
npm --global install --loglevel silent ${NPM_GLOBAL_PACKAGES}
else
echo "All ${NPM_GLOBAL_PACKAGES_COUNT} global packages are up to date:" | PRINT_BLUE
echo ${NPM_GLOBAL_PACKAGES} | PRINT_BLUE
fi
}
@pensierinmusica
Copy link

Hi @sidneys, what is touch node_updater.sh supposed to do?

@sidneys
Copy link
Author

sidneys commented Apr 7, 2016

Hi @pensierinmusica, thanks for your comment.

I included this instruction step for commandline-averse users who might need to create a new script file first.
However, in v1.0.0 i omitted this step.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment