Skip to content

Instantly share code, notes, and snippets.

@adamjcooper
Last active February 4, 2022 21:28
Show Gist options
  • Save adamjcooper/711f578acdad9f20572e22eb5764ae90 to your computer and use it in GitHub Desktop.
Save adamjcooper/711f578acdad9f20572e22eb5764ae90 to your computer and use it in GitHub Desktop.
# Automatically call `nvm use`/`nvm install` after `cd` or `git checkout` to use
# the version of Node.js defined in .nvmrc (or the default Node.js version if
# there is no .nvmrc and you are in a directory with a package.json file).
# This first `nvm_after_cd` function is based on the function recommended in the
# official documentation at
# https://github.com/nvm-sh/nvm/tree/2c0c34f10e6f415d1c6f60c54fcbf53a7670dec6#bash,
# but avoids calling `nvm` when there is no package.json present so that `cd` is
# not so slow all the time.
# Only calls nvm when:
# 1) A directory or one of its parents contains a .nvmrc file. In this case,
# will call `nvm use` or `nvm install`, whichever is appropriate.
# 2) A directory or one of its parents does NOT contain a .nvmrc file AND the
# directory contains a package.json file, indicating the directory is for a
# Node.js project. In this case, will call `nvm use default`.
nvm_after_cd() {
command cd "$@";
nvm_path=$(nvm_find_up .nvmrc | tr -d '\n')
# If there are no .nvmrc file AND there is a package.json file (indicating
# we are in a Node.js project directory), use the default nvm version
if [[ ! $nvm_path = *[^[:space:]]* && -s package.json ]]; then
echo "HEY! You should probably create an .nvmrc file for this directory!"
echo "Like this:"
echo " node -v > .nvmrc"
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" | tail -1 | tr -d '\->*' | 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='nvm_after_cd'
cd "$PWD"
# Automatically set the Node.js version defined in .nvmrc after a git checkout
# by invoking `cd .`. Depends on auto-invoke-nvm-on-cd.sh.
cd_dot_after_git_checkout() {
if [[ $1 == "checkout" ]]; then
command git "$@" && cd .
else
command git "$@"
fi
}
alias git='cd_dot_after_git_checkout'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment