Skip to content

Instantly share code, notes, and snippets.

@DeruiDENG
Last active December 26, 2023 03:28
Show Gist options
  • Save DeruiDENG/f629d6f4a535e0c1bc0aaa6ad686c239 to your computer and use it in GitHub Desktop.
Save DeruiDENG/f629d6f4a535e0c1bc0aaa6ad686c239 to your computer and use it in GitHub Desktop.
A shell script which can help the nvm to detect .nvmrc and .node_version file
#!/bin/bash
load-nvmrc() {
local nvmrc_path
nvmrc_path="$(nvm_find_nvmrc)"
if [ ! -e "${nvmrc_path}" ]; then
local LOCAL_NODE_VERSION_DOTFILE_PATH
LOCAL_NODE_VERSION_DOTFILE_PATH="${PWD}/.node-version"
if [ -e "${LOCAL_NODE_VERSION_DOTFILE_PATH}" ]; then
nvmrc_path="${LOCAL_NODE_VERSION_DOTFILE_PATH}"
fi
fi
if [ -n "$nvmrc_path" ]; then
local nvmrc_node_version
nvmrc_node_version=$(nvm version "$(cat "${nvmrc_path}")")
if [ "$nvmrc_node_version" = "N/A" ]; then
nvm install
elif [ "$nvmrc_node_version" != "$(nvm version)" ]; then
nvm use "$nvmrc_node_version"
fi
elif [ -n "$(PWD=$OLDPWD nvm_find_nvmrc)" ] && [ "$(nvm version)" != "$(nvm version default)" ]; then
echo "Reverting to nvm default version"
nvm use default
fi
}
load-nvmrc
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment