-
Notifications
You must be signed in to change notification settings - Fork 0
/
autoload-nvmrc.sh
28 lines (25 loc) · 1.02 KB
/
autoload-nvmrc.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# @see https://stackoverflow.com/questions/57110542/how-to-write-a-nvmrc-file-which-automatically-change-node-version
#
# To autoload the specified node version
# You need to add something else to your shell configuration depending on what you use bash or zsh
# To get the exact configuration for each of them, please follow the instructions in the corresponding shell config section.
# In my case I'm using zsh so I do need to add this at the end of my .zshrc file:
# 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