Skip to content

Instantly share code, notes, and snippets.

@jiverson
Forked from sidneys/nvm_node_updater.sh
Created April 28, 2016 16:05
Show Gist options
  • Save jiverson/af77740b41b3623a172075f4221eddc1 to your computer and use it in GitHub Desktop.
Save jiverson/af77740b41b3623a172075f4221eddc1 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/sh
# NVM_NODE_UPDATER
# v1.0.1
#
# 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_CURRENT=$(nvm_ls_current)
NODE_LATEST=$(nvm_remote_versions | grep '^v' | tail -1)
if [ "${NODE_CURRENT}" != "${NODE_LATEST}" ]
then
# Update to latest NodeJS version (migrating all currently installed global NPM packages)
echo "Updating global NodeJS: ${NODE_CURRENT} to ${NODE_LATEST}"
nvm install ${NODE_LATEST} --reinstall-packages-from=${NODE_CURRENT}
nvm alias default ${NODE_LATEST} && nvm use default
echo "Updated default NodeJS to ${NODE_LATEST}."
else
echo "Global NodeJS already up-to-date (${NODE_CURRENT})."
fi
# Check for latest NPM package versions
NPM_OUTDATED=$(npm --global outdated --parseable --depth=0 --loglevel silent | cut -d: -f3 | cut -f1 -d'@' | tr '\r\n' ' ' | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')
if [ -n "${NPM_OUTDATED}" ]
then
# Update to latest NPM package versions
echo "Updating global NPM package(s): ${NPM_OUTDATED// /, }"
for PACKAGE in ${NPM_OUTDATED}
do npm --global install --loglevel silent ${PACKAGE} && echo "Updated '${PACKAGE}' to latest."
done
else
echo "Global NPM packages are already up-to-date."
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment