Skip to content

Instantly share code, notes, and snippets.

@aparajita
Last active May 27, 2023 21:32
Show Gist options
  • Save aparajita/5951bae9aca0c91561fef7716fbc2422 to your computer and use it in GitHub Desktop.
Save aparajita/5951bae9aca0c91561fef7716fbc2422 to your computer and use it in GitHub Desktop.
Shell script to install the latest Node version using fnm, pnpm and jq
#!/bin/bash
localVersion=$(fnm ls | sort | tail -1 | perl -ne 'if (/.*(v\d+\.\d+\.\d+)/) { print "$1" }')
remoteVersion=$(fnm ls-remote | grep -E '.*v\d+\.\d+\.\d+' | tail -n 1 | perl -ne 'if (/^.*(v\d+\.\d+\.\d+)/) { print "$1" }')
if [[ "$remoteVersion" != "$localVersion" ]]; then
read -r -n 1 -p "Version $remoteVersion is available. Install it? [Y/n] " shouldInstall
echo
case "$shouldInstall" in
[nN])
exit 0
;;
esac
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
echo Migrating global packages
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 -n 1 -p "Uninstall $localVersion? [Y/n] " shouldUninstall
echo
case "$shouldUninstall" in
[yY] | "")
fnm uninstall "$localVersion"
;;
esac
else
echo No new node version available
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment