Skip to content

Instantly share code, notes, and snippets.

@simbo
Last active November 12, 2015 12:55
Show Gist options
  • Save simbo/e183b2e2bdcc884b1696 to your computer and use it in GitHub Desktop.
Save simbo/e183b2e2bdcc884b1696 to your computer and use it in GitHub Desktop.
sync globally installed packages of two node versions installed via nvm
#!/bin/bash
#
# nvm-sync-global-packages (nsgp)
source ~/.nvm/nvm.sh
function change_node_version() {
nvm use $1 &> /dev/null
}
if [[ -z "$1" ]] || [[ -z "$2" ]]; then
echo "nvm-sync-global-packages (nsgp)"
echo "-------------------------------"
echo "sync globally installed packages of two node versions installed via nvm"
echo
echo "Usage:"
echo "nsgp <source-version> <target-version>"
exit 0
fi
NSGP_SOURCE_VERSION=$(change_node_version $1 && node --version)
change_node_version $NSGP_SOURCE_VERSION
NSGP_SOURCE_PACKAGES=$(npm ls -g --depth=0 | grep @ | sed -e 's/[^a-z\-]//g' -e 's/^npm$//g')
NSGP_TARGET_VERSION=$(change_node_version $2 && node --version)
echo "Sync globally installed packages between node $NSGP_SOURCE_VERSION and node $NSGP_TARGET_VERSION:"
echo
echo $NSGP_SOURCE_PACKAGES
echo
read -p "Continue? (Y/N) " -n 1 -r
echo
if [[ $REPLY =~ ^[YyJj]$ ]]; then
change_node_version $NSGP_TARGET_VERSION
read -p "Update npm to latest version for node $NSGP_TARGET_VERSION? (Y/N) " -n 1 -r
echo
if [[ $REPLY =~ ^[YyJj]$ ]]; then
npm i -g npm@latest
fi
npm i -g $NSGP_SOURCE_PACKAGES
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment