# place this after nvm initialization!
autoload -U add-zsh-hook
load-nvmrc() {
local node_version="$(nvm version)"
local nvmrc_path="$(nvm_find_nvmrc)"
if [ -n "$nvmrc_path" ]; then
local nvmrc_node_version=$(nvm version "$(cat "${nvmrc_path}")")
if [ "$nvmrc_node_version" = "N/A" ]; then
nvm install
elif [ "$nvmrc_node_version" != "$node_version" ]; then
nvm use
fi
elif [ "$node_version" != "$(nvm version default)" ]; then
echo "Reverting to nvm default version"
nvm use default
fi
}
add-zsh-hook chpwd load-nvmrc
load-nvmrc
_nvmrc_hook() {
if [[ $PWD == $PREV_PWD ]]; then
return
fi
PREV_PWD=$PWD
[[ -f ".nvmrc" ]] && nvm use
}
if ! [[ "${PROMPT_COMMAND:-}" =~ _nvmrc_hook ]]; then
PROMPT_COMMAND="_nvmrc_hook${PROMPT_COMMAND:+;$PROMPT_COMMAND}"
fi
#
# Run 'nvm use' automatically every time there's
# a .nvmrc file in the directory. Also, revert to default
# version when entering a directory without .nvmrc
#
enter_directory() {
if [[ $PWD == $PREV_PWD ]]; then
return
fi
PREV_PWD=$PWD
if [[ -f ".nvmrc" ]]; then
nvm use
NVM_DIRTY=true
elif [[ $NVM_DIRTY = true ]]; then
nvm use default
NVM_DIRTY=false
fi
}
export PROMPT_COMMAND="$PROMPT_COMMAND; enter_directory"
enter_directory() {
if [[ $PWD == $PREV_PWD ]]; then
return
fi
if [[ "$PWD" =~ "$PREV_PWD" && ! -f ".nvmrc" ]]; then
return
fi
PREV_PWD=$PWD
if [[ -f ".nvmrc" ]]; then
nvm use
NVM_DIRTY=true
elif [[ $NVM_DIRTY = true ]]; then
nvm use default
NVM_DIRTY=false
fi
}
诀窍就在这里:
if [[ "$PWD" =~ "$PREV_PWD" && ! -f ".nvmrc" ]]; then
return
fi
find-up () {
path=$(pwd)
while [[ "$path" != "" && ! -e "$path/$1" ]]; do
path=${path%/*}
done
echo "$path"
}
cdnvm(){
cd "$@";
nvm_path=$(find-up .nvmrc | tr -d '[:space:]')
# 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)
# Add the `v` suffix if it does not exists in the .nvmrc file
if [[ $nvm_version != v* ]]; then
nvm_version="v""$nvm_version"
fi
# If it is not already installed, install it
if [[ $(nvm ls "$nvm_version" | tr -d '[:space:]') == "N/A" ]]; then
nvm install "$nvm_version";
fi
if [[ $(nvm current) != "$nvm_version" ]]; then
nvm use "$nvm_version";
fi
fi
}
alias cd='cdnvm'
_enter_dir() {
local git_root
git_root=$(git rev-parse --show-toplevel 2>/dev/null)
if [[ "$git_root" == "$PREV_PWD" ]]; then
return
elif [[ -n "$git_root" && -f "$git_root/.nvmrc" ]]; then
nvm use
NVM_DIRTY=1
elif [[ "$NVM_DIRTY" == 1 ]]; then
nvm use default
NVM_DIRTY=0
fi
PREV_PWD="$git_root"
}
export PROMPT_COMMAND=_enter_dir
#export PROMPT_COMMAND="$PROMPT_COMMAND;_enter_dir" # use this if PROMPT_COMMAND already defined
call_nvm_use_if_needed() {
NEW_NVMRC="$(nvm_find_nvmrc)"
if [[ "$NEW_NVMRC" != "$CURRENT_NVMRC" ]]; then
if [[ -z "$NEW_NVMRC" ]]; then
nvm use default
else
nvm use
fi
CURRENT_NVMRC="$NEW_NVMRC"
fi
}
PROMPT_COMMAND="call_nvm_use_if_needed; ${PROMPT_COMMAND}"
% time (source "$NVM_DIR/nvm.sh")
( source "$NVM_DIR/nvm.sh"; ) 0.58s user 0.37s system 109% cpu 0.874 total
% time (_zsh_nvm_lazy_load)
( _zsh_nvm_lazy_load; ) 0.01s user 0.01s system 168% cpu 0.012 total
autoload -U add-zsh-hook
load-nvmrc() {
if [ -f ".nvmrc" ]; then
local required_version=$(cat .nvmrc | cut -c2-)
local current_version=$(node -v)
echo "Required Node version: $required_version"
local is_available_already=$(nvm ls | grep -c "$required_version")
if [[ $required_version != $current_version && $is_available_already -lt 1 ]]; then
echo "Required version $required_version not installed, installing..."
nvm install $required_version
fi
nvm use $required_version
fi
}
add-zsh-hook chpwd load-nvmrc
load-nvmrc
# place this after nvm initialization!
autoload -U add-zsh-hook
load-nvmrc() {
local node_version="$(nvm version)"
local nvmrc_path="$(nvm_find_nvmrc)"
if [ -n "$nvmrc_path" ]; then
local nvmrc_node_version=$(nvm version "$(cat "${nvmrc_path}")")
if [ "$nvmrc_node_version" = "N/A" ]; then
# check if we're in a native (ARM) terminal
if [[ $(uname -p) == "arm" ]]; then
local nvmrc_remote_version=$(nvm version-remote "$(cat "${nvmrc_path}")")
if printf '%s\n%s\n' v16.0.0 "${nvmrc_remote_version}" | sort -VC; then
# arm and node >= v16; install native node
nvm install
else
# arm and node < v16; install x64 node using rosetta
arch -x86_64 zsh -c '. "/opt/homebrew/opt/nvm/nvm.sh"; nvm install'
nvm use
fi
else
# not arm
nvm install
fi
elif [ "$nvmrc_node_version" != "$node_version" ]; then
nvm use
fi
elif [ "$node_version" != "$(nvm version default)" ]; then
echo "Reverting to nvm default version"
nvm use default
fi
}
add-zsh-hook chpwd load-nvmrc
load-nvmrc
# TODO: save this as `$HOME/.config/fish/conf.d/use_nvmrc.fish`
# HOW IT WORKS
# `nvm use` whenever .nvmrc is present in $PWD when using fish shell
# when traveling deeper, use the parent .nvmrc unless otherwise set
# also go back to default nvm when leaving the nvmrc-specified zone
function set_nvm --on-event fish_prompt
# runs whenever the fish_prompt event occurs
# if the current directory hasn't changed, do nothing
string match -q $PWD $PREV_PWD; and return 1
# if the current directory is within the previous one where we found an nvmrc
# and there is no subsequent .nvmrc here, do nothing, we are in the same repo
string match -eq $PREV_PWD $PWD; and not test -e '.nvmrc'; and return 1
# if we clear those checks, keep track of where we are
set -g PREV_PWD $PWD
if test -e '.nvmrc'
# if we find .nvmrc, run nvm use
nvm use
# and remember that we used that node
set NVM_DIRTY true
else if not string match $NVM_DIRTY true
# if we have set nvm and have stepped out of that repo
# go back to default node, if not already on it
not string match -eq (nvm current) (nvm alias default); and nvm use default
# and clear the flag
set NVM_DIRTY
end
end
autoload -U add-zsh-hook
use_nvmrc_version_automatically() {
if [[ -f .nvmrc ]]; then
echo ".nvmrc FOUND now INSTALLING and USING $(cat .nvmrc)"
nvm install $(cat .nvmrc) && nvm use $(cat .nvmrc)
fi
}
add-zsh-hook chpwd use_nvmrc_version_automatically
use_nvmrc_version_automatically
# Load right version of NVM
autoload -U add-zsh-hook
load-nvmrc() {
local node_version="$(nvm version)"
local nvmrc_path="$(nvm_find_nvmrc)"
if [ -n "$nvmrc_path" ]; then
local nvmrc_node_version=$(nvm version "$(cat "${nvmrc_path}")")
if [ "$nvmrc_node_version" = "N/A" ]; then
nvm install
elif [ "$nvmrc_node_version" != "$node_version" ]; then
nvm use
fi
elif [ "$node_version" != "$(nvm version-remote --lts)" ]; then
if [[ "$(nvm ls)" != *"$(nvm version-remote --lts)"* ]]; then
echo "New lts version available. Installing..."
nvm install --lts
fi
echo "Reverting to node lts version"
nvm use --lts
fi
}
add-zsh-hook chpwd load-nvmrc
load-nvmrc