Skip to content

Instantly share code, notes, and snippets.

@aparajita

aparajita/upgrade-node.sh

Last active Apr 17, 2021
Embed
What would you like to do?
Shell script to install the latest Node version using fnm, pnpm and jq
#!/bin/bash
localVersion=$(fnm ls | tail -n 1 | perl -n -e '/^.*(v\d+\.\d+\.\d+)/ && print $1')
remoteVersion=$(fnm ls-remote | grep -E '.+v\d+\.\d+\.\d+' | tail -n 1 | perl -n -e '/^.*(v\d+\.\d+\.\d+)/ && print $1')
if [[ "$remoteVersion" != "$localVersion" ]]; then
echo Version "$remoteVersion" available
fnm use "$localVersion"
# shellcheck disable=SC2034
globalPackages=$(pnpm ls -g --depth 0 --json | jq ".[].dependencies | keys | .[]" | tr -d '"' | tr "\n" " ")
fnm install "$remoteVersion"
fnm use "$remoteVersion"
fnm alias "$remoteVersion" default
fnm alias "$remoteVersion" latest
npm i -g --silent --no-progress pnpm@latest
# shellcheck disable=SC2046
# shellcheck disable=SC2116
# shellcheck disable=SC2086
pnpm i -g $(echo $globalPackages)
echo
read -r -p "Uninstall $localVersion? [Y/n] " shouldUninstall
case "$shouldUninstall" in
[yY] | "")
fnm uninstall "$localVersion"
;;
esac
else
echo No new version available
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment