Created
October 4, 2021 22:36
-
-
Save skryvets/387663746efb3e9e541bfaa88aa68913 to your computer and use it in GitHub Desktop.
check-latest-lts-node-version.sh
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
. ~/.nvm/nvm.sh | |
cr=`echo $'\n.'` | |
cr=${cr%.} | |
currentNodeVersion=$(node -v) | |
echo Your NodeJS version is $currentNodeVersion | |
latestLTSNodeVersion=$(curl -s "https://nodejs.org/dist/index.json" | jq -r 'map(select(.lts != false)) | .[0] | .version') | |
echo "Latest LTS Node Version available is $latestLTSNodeVersion" | |
if [ $currentNodeVersion == $latestLTSNodeVersion ] | |
then | |
echo "You're up to date" | |
else | |
read -p "Newer version available, do you want to update? (Yes) or (No): " | |
echo | |
if [[ $REPLY =~ ^[Yy]|[Yy]es$ ]] | |
then | |
echo "Updating..." | |
echo "Process may take a while..." | |
nvm install $latestLTSNodeVersion --reinstall-packages-from=$currentNodeVersion | |
echo "Setting default node version to updated one..." | |
nvm alias default $latestLTSNodeVersion | |
echo "Uninstalling old node version..." | |
nvm uninstall $currentNodeVersion | |
echo "Success!" | |
echo "You current version is $(node -v)" | |
else | |
echo "Bye..." | |
[[ "$0" = "$BASH_SOURCE" ]] && exit 1 || return 1 # handle exits from shell or function but don't exit interactive shell | |
fi | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment