Last active
January 30, 2026 18:43
-
-
Save kritish-dhaubanjar/571e66c60ee89faf779fca3493bf49c0 to your computer and use it in GitHub Desktop.
source nvm_selector.sh
This file contains hidden or 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
| #!/bin/bash | |
| function get_nvmrc_search_path(){ | |
| local DIR=$1 | |
| local PATH="$DIR/.nvmrc" | |
| echo $PATH | |
| } | |
| function nvmrc_selector(){ | |
| builtin cd "$@" | |
| local DIR=$(pwd) | |
| local DEPTH=$(( $(grep -o '/' <<< $DIR | wc -l) + 1 )) | |
| while true; do | |
| local NVMRC_PATH=$(get_nvmrc_search_path "$DIR") | |
| if [[ -f $NVMRC_PATH ]]; then | |
| local CURRENT_NODE_VERSION=$(nvm current) | |
| local TARGET_NODE_VERSION="$(cat $NVMRC_PATH)" | |
| if [[ $CURRENT_NODE_VERSION != $TARGET_NODE_VERSION ]]; then | |
| nvm use | |
| fi | |
| break | |
| fi | |
| if [[ -z $DIR ]]; then | |
| break | |
| fi | |
| DIR="${DIR%/*}" | |
| done | |
| } | |
| alias cd='nvmrc_selector' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment