forked from moll/sh-chnode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
chnode-auto
executable file
·52 lines (45 loc) · 1.29 KB
/
chnode-auto
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
#!/usr/bin/env false
. "$(dirname "$0")/chnode"
unset CHNODE_AUTO_VERSION
chnode-auto() {
local old_="$1" # https://github.com/postmodern/chruby/pull/381
local dir="$PWD/" version
until [ -z "$dir" ]; do
dir="${dir%/*}"
if { read -r version < "$dir/.node-version" ||
read -r version < "$dir/.nvmrc"; } 2>/dev/null; then
version="${version%%[[:space:]]}"
if [ -n "$version" ]; then
test "$version" = "$CHNODE_AUTO_VERSION" && return
export CHNODE_AUTO_VERSION="$version"
chnode "$version"
return $?
fi
fi
: "$old_"
done
if [ -n "$CHNODE_AUTO_VERSION" ]; then
chnode system
unset CHNODE_AUTO_VERSION
fi
}
if [ -n "$ZSH_VERSION" ]; then
if [[ "$preexec_functions" != *chnode-auto* ]]; then
preexec_functions+=("chnode-auto")
fi
elif [ -n "$BASH_VERSION" ]; then
CHNODE_AUTO_TRAP='local [[ "$BASH_COMMAND" != "$PROMPT_COMMAND" ]] && chnode-auto "$_"'
if [[ "$(trap -p DEBUG)" =~ DEBUG ]]; then
if ! [[ "$(trap -p DEBUG)" =~ "$CHNODE_AUTO_TRAP" ]]; then
unset CHNODE_AUTO_TRAP
echo 'chnode-auto cannot be installed - DEBUG trap already set' >&2
return 1
fi
fi
trap "$CHNODE_AUTO_TRAP" DEBUG
unset CHNODE_AUTO_TRAP
else
echo "Unsupported shell $SHELL"
return 1
fi
chnode-auto