Created
June 22, 2018 11:36
-
-
Save JakeSidSmith/3578eb240a560536e9dd37b6cff2e7c9 to your computer and use it in GitHub Desktop.
Bash profile extension to automatically change NodeJS versions with NVM
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 | |
RED="\033[1;31m" | |
GREEN="\033[1;32m" | |
YELLOW="\033[1;33m" | |
COLOR_NONE="\033[1;39m" | |
NVMRC=".nvmrc" | |
function check_node_version () { | |
if [ -e "$NVMRC" ]; then | |
# Get nvmrc version | |
NVM_NODE_VERSION="$(cat .nvmrc)" | |
# Remove v from version | |
NVM_NODE_VERSION="${NVM_NODE_VERSION/v/}" | |
# Strip whitespace | |
NVM_NODE_VERSION="$(echo $NVM_NODE_VERSION | tr -d \"[:space:]\")" | |
else | |
NVM_NODE_VERSION="n/a" | |
fi | |
NODE_LOCATION="$(which node)" | |
if [ -z "$NODE_LOCATION" ]; then | |
NODE_VERSION="n/a" | |
else | |
# Get current node version | |
NODE_VERSION="$(node -v)" | |
# Remove v from version | |
NODE_VERSION="${NODE_VERSION/v/}" | |
# Strip whitespace | |
NODE_VERSION="$(echo $NODE_VERSION | tr -d \"[:space:]\")" | |
fi | |
if [ "$NVM_NODE_VERSION" != "n/a" ] && [ "$NODE_VERSION" != "n/a" ]; then | |
if [ "$NVM_NODE_VERSION" != "$NODE_VERSION" ]; then | |
if nvm use &> /dev/null; then | |
echo -e "${GREEN}➜ Switching to node ${NVM_NODE_VERSION}" | |
else | |
echo -e "${RED}✘ Run 'nvm install' to install node ${NVM_NODE_VERSION}" | |
fi | |
else | |
echo -e "${GREEN}✔︎ Using node $NODE_VERSION" | |
fi | |
else | |
echo -e "${YELLOW}✔︎ Using node $NODE_VERSION" | |
fi | |
} | |
# check_node_version | |
PROMPT_COMMAND="check_node_version;$PROMPT_COMMAND" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment