forked from rbenv/rbenv-installer
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathnodenv-doctor
executable file
·215 lines (197 loc) · 5.79 KB
/
nodenv-doctor
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
#!/usr/bin/env bash
# Usage: nodenv doctor
# Summary: Detects common problems in nodenv installation
set -e
[ -n "$NODENV_DEBUG" ] && { export PS4='+ [${BASH_SOURCE##*/}:${LINENO}] '; set -x; }
count-lines() {
printf "%d" "$(wc -l)"
}
command-list() {
# shellcheck disable=SC2230
which -a "$1"
}
indent() {
sed 's/^/ /'
}
# for ubuntu /bin is a symlink to /usr/bin
# which -a doesn't know the binaries are the same
fix-usr-bin() {
sed 's:^/usr/bin/:/bin/:'
}
printc() {
local color_name="color_$1"
local msg="$2"
printf "%s%s%s" "${!color_name}" "$msg" "$color_reset"
}
if [ -t 1 ]; then
# shellcheck disable=SC2034
color_red=$'\e[31m'
# shellcheck disable=SC2034
color_green=$'\e[32m'
# shellcheck disable=SC2034
color_yellow=$'\e[1;33m'
# shellcheck disable=SC2034
color_bright=$'\e[1;37m'
color_reset=$'\e[0m'
else
# shellcheck disable=SC2034
color_red=""
# shellcheck disable=SC2034
color_green=""
# shellcheck disable=SC2034
color_yellow=""
# shellcheck disable=SC2034
color_bright=""
# shellcheck disable=SC2034
color_reset=""
fi
warnings=0
echo -n "Checking for \`nodenv' in PATH: "
num_locations="$(command-list nodenv | fix-usr-bin | uniq | count-lines)"
if [ "$num_locations" -eq 0 ]; then
printc red "not found"
echo
{ if [ -x "$HOME/.nodenv/bin/nodenv" ] || [ -x "${NODENV_ROOT:-}/bin/nodenv" ]; then
[ -x "$HOME/.nodenv/bin/nodenv" ] &&
echo "You seem to have nodenv installed in \`$HOME/.nodenv/bin',"
[ -x "${NODENV_ROOT:-}/bin/nodenv" ] &&
echo "You seem to have nodenv installed in \`$NODENV_ROOT/bin',"
echo "but that directory is not present in PATH."
echo "Please add it to PATH by configuring your shell initialization files."
else
echo "Please refer to https://github.com/nodenv/nodenv#installation"
fi
} | indent
if [ -n "$(find "${NODENV_ROOT:-~/.nodenv}/bin/nodenv" -mmin 1 2>/dev/null)" ]; then
printc bright "Recent installation detected.\n"
{ echo "Complete the installation by configuring your PATH as above,"
echo "and re-run nodenv-doctor in a fresh shell."
} | indent
exit 0
fi
exit 1
elif [ "$num_locations" -eq 1 ]; then
printc green "$(command -v nodenv)"
echo
else
printc yellow "multiple"
echo
{ echo "You seem to have multiple nodenv installs in the following locations."
echo "Please pick just one installation and remove the others."
echo
command-list nodenv
} | indent
echo
: $((warnings++))
fi
NODENV_ROOT="${NODENV_ROOT:-$(nodenv root)}"
echo -n "Checking for nodenv shims in PATH: "
shims_dir="${NODENV_ROOT}/shims"
found=""
precedence_problem=""
IFS=: read -r -a path <<<"$PATH" || true
for dir in "${path[@]}"; do
if [ "$dir" = "$shims_dir" ]; then
found=true
elif [ -x "$dir"/node ] && [ -z "$found" ]; then
precedence_problem="$dir"
fi
done
if [ -n "$found" ]; then
if [ -n "$precedence_problem" ]; then
printc yellow "found at wrong position"
echo
{ echo "The directory \`$shims_dir' is present in PATH, but is listed too late."
echo "The Node version found in \`$precedence_problem' will have precedence. Please reorder your PATH."
} | indent
echo
: $((warnings++))
else
printc green "OK"
echo
fi
else
printc red "not found"
echo
{ echo "The directory \`$shims_dir' must be present in PATH for nodenv to work."
echo "Please run \`nodenv init' and follow the instructions."
} | indent
echo
: $((warnings++))
fi
echo -n "Checking \`nodenv install' support: "
nodenv_installs="$({ ls "$NODENV_ROOT"/plugins/*/bin/nodenv-install 2>/dev/null || true
command-list nodenv-install 2>/dev/null || true
} | uniq)"
num_installs="$(count-lines <<<"$nodenv_installs")"
if [ -z "$nodenv_installs" ]; then
printc red "not found"
echo
{ echo "Unless you plan to add Node versions manually, you should install node-build."
echo "Please refer to https://github.com/nodenv/node-build#installation"
}
echo
: $((warnings++))
elif [ "$num_installs" -eq 1 ]; then
printc green "$nodenv_installs"
if [[ $nodenv_installs == "$NODENV_ROOT"/plugins/* ]]; then
nodenv_install_cmd="${nodenv_installs##*/}"
nodenv_install_version="$(nodenv "${nodenv_install_cmd#nodenv-}" --version || true)"
else
nodenv_install_version="$("$nodenv_installs" --version || true)"
fi
printf " (%s)\n" "$nodenv_install_version"
else
printc yellow "multiple"
echo
{ echo "You seem to have multiple \`nodenv-install' in the following locations."
echo "Please pick just one installation and remove the others."
echo
echo "$nodenv_installs"
} | indent
echo
: $((warnings++))
fi
echo -n "Counting installed Node versions: "
num_nodes="$(nodenv versions --bare | count-lines)"
if [ "$num_nodes" -eq 0 ]; then
printc yellow "none"
echo
echo "There aren't any Node versions installed under \`$NODENV_ROOT/versions'." | indent
[ "$num_installs" -eq 0 ] || {
latest_version="$(nodenv install -l 2>/dev/null | grep -vFe - | tail -1)"
[ -n "$latest_version" ] || latest_version="<version>"
echo -n "You can install Node versions like so: "
printc bright "nodenv install $latest_version"
echo
} | indent
else
printc green "$num_nodes versions"
echo
fi
echo -n "Auditing installed plugins: "
hooks_list="$(nodenv hooks exec || true)"
IFS=$'\n' read -r -d '' -a hooks <<<"$hooks_list" || true
plugin_broken=0
for hook in "${hooks[@]}"; do
plugin_name=
message=
if [ -n "$plugin_name" ]; then
if [ "$((plugin_broken++))" -eq 0 ]; then
printc yellow "warning"
echo
fi
{ printc bright "$plugin_name"
echo " $message"
echo " (found in \`${hook}')"
} | indent
: $((warnings++))
fi
done
if [ "$plugin_broken" -eq 0 ]; then
printc green "OK"
echo
fi
if [ "$warnings" -gt 0 ]; then
exit 1
fi