Created
October 9, 2025 15:43
-
-
Save Steakeye/a8f951dd59629078414b41efc7c9f54f to your computer and use it in GitHub Desktop.
Revisions
-
Steakeye created this gist
Oct 9, 2025 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,74 @@ # This script will trigger nvm use is there is an .nvm-use file with auto set to true cdnvm() { command cd "$@" || return $? upfind () { local res=$(ls -f $1 2>/dev/null) if [ ! -z $res ]; then echo $res return 0; fi if [ "$(realpath $(dirname $1))" != "/" ]; then upfind ../$1 fi } local foundFile=$(upfind .nvm-use) local foundAutoValue; if [ -f "$foundFile" ]; then #echo found .nvm-use file in: $(realpath $(dirname $foundFile)) foundAutoValue=$(grep '^auto=' $foundFile | tail -1 | awk -F '=' '{print $2}') else return 0; fi if [ "$foundAutoValue" != "true" ]; then #echo .nvm-use auto value was not found or not true return 0 fi nvm_path="$(nvm_find_up .nvmrc | command tr -d '\n')" # If there are no .nvmrc file, use the default nvm version if [[ ! $nvm_path = *[^[:space:]]* ]]; then declare default_version default_version="$(nvm version default)" # If there is no default version, set it to `node` # This will use the latest version on your machine if [ $default_version = 'N/A' ]; then nvm alias default node default_version=$(nvm version default) fi # If the current version is not the default version, set it to use the default version if [ "$(nvm current)" != "${default_version}" ]; then nvm use default fi elif [[ -s "${nvm_path}/.nvmrc" && -r "${nvm_path}/.nvmrc" ]]; then declare nvm_version nvm_version=$(<"${nvm_path}"/.nvmrc) declare locally_resolved_nvm_version # `nvm ls` will check all locally-available versions # If there are multiple matching versions, take the latest one # Remove the `->` and `*` characters and spaces # `locally_resolved_nvm_version` will be `N/A` if no local versions are found locally_resolved_nvm_version=$(nvm ls --no-colors "${nvm_version}" | command tail -1 | command tr -d '\->*' | command tr -d '[:space:]') # If it is not already installed, install it # `nvm install` will implicitly use the newly-installed version if [ "${locally_resolved_nvm_version}" = 'N/A' ]; then nvm install "${nvm_version}"; elif [ "$(nvm current)" != "${locally_resolved_nvm_version}" ]; then nvm use "${nvm_version}"; fi fi } alias cd='cdnvm' cdnvm "$PWD" || exit