-
Notifications
You must be signed in to change notification settings - Fork 0
/
lazy-nvm.plugin.zsh
67 lines (52 loc) · 1.42 KB
/
lazy-nvm.plugin.zsh
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# lazy-nvm.plugin.zsh
# author: Seong Yong-ju <[email protected]>
__nvm_loaded=false
__load_nvm() {
unhash -f nvm_find_nvmrc
unhash -f nvm
__nvm_loaded=true
. "${NVM_DIR:-${HOME}/.nvm}/nvm.sh"
}
nvm_find_nvmrc() {
local dir="$PWD"
while [[ "$dir" != '' && ! -f "${dir}/.nvmrc" ]]; do
dir="${dir%/*}"
done
echo "${dir:+${dir}/.nvmrc}"
}
nvm() {
__load_nvm
nvm "$@"
}
__lazy_nvm_chpwd() {
local nvmrc_path="$(nvm_find_nvmrc)"
if [[ "$__nvm_loaded" != true ]]; then
if [[ -n "$nvmrc_path" ]]; then
__load_nvm
else
return
fi
fi
if [[ -n "$nvmrc_path" ]]; then
local node_version="$(nvm version)"
local nvmrc_node_version="$(nvm version "$(cat "$nvmrc_path")")"
if [[ "$nvmrc_node_version" == 'N/A' ]]; then
if [[ "$LAZY_NVM_AUTO_INSTALL" == true ]]; then
nvm install
fi
elif [[ "$nvmrc_node_version" != "$node_version" ]]; then
if [[ "$LAZY_NVM_SILENT_USE" == true ]]; then
nvm use --silent
else
nvm use
fi
fi
elif [[ "$node_version" != "$(nvm version default)" ]]; then
if [[ "$LAZY_NVM_SILENT_USE" != true ]]; then
echo "Reverting to nvm default version"
fi
nvm use default
fi
}
chpwd_functions+=( __lazy_nvm_chpwd )
__lazy_nvm_chpwd