Skip to content

Commit 0579718

Browse files
committed
[Fix] install script: define nvm_echo
- refactor `echo` to use `nvm_echo` Per nvm-sh@589c237#r48360520
1 parent 8884fd3 commit 0579718

File tree

1 file changed

+61
-57
lines changed

1 file changed

+61
-57
lines changed

install.sh

+61-57
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ nvm_has() {
66
type "$1" > /dev/null 2>&1
77
}
88

9+
nvm_echo() {
10+
command printf %s\\n "$*" 2>/dev/null
11+
}
12+
913
nvm_grep() {
1014
GREP_OPTIONS='' command grep "$@"
1115
}
@@ -23,7 +27,7 @@ nvm_install_dir() {
2327
}
2428

2529
nvm_latest_version() {
26-
echo "v0.37.2"
30+
nvm_echo "v0.37.2"
2731
}
2832

2933
nvm_profile_is_bash_or_zsh() {
@@ -64,18 +68,18 @@ nvm_source() {
6468
elif [ "_$NVM_METHOD" = "_git" ] || [ -z "$NVM_METHOD" ]; then
6569
NVM_SOURCE_URL="https://github.com/${NVM_GITHUB_REPO}.git"
6670
else
67-
echo >&2 "Unexpected value \"$NVM_METHOD\" for \$NVM_METHOD"
71+
nvm_echo >&2 "Unexpected value \"$NVM_METHOD\" for \$NVM_METHOD"
6872
return 1
6973
fi
7074
fi
71-
echo "$NVM_SOURCE_URL"
75+
nvm_echo "$NVM_SOURCE_URL"
7276
}
7377

7478
#
7579
# Node.js version to install
7680
#
7781
nvm_node_version() {
78-
echo "$NODE_VERSION"
82+
nvm_echo "$NODE_VERSION"
7983
}
8084

8185
nvm_download() {
@@ -108,37 +112,37 @@ install_nvm_from_git() {
108112
:
109113
# Check if version is an existing changeset
110114
elif ! nvm_download -o /dev/null "$(nvm_source "script-nvm-exec")"; then
111-
echo >&2 "Failed to find '$NVM_VERSION' version."
115+
nvm_echo >&2 "Failed to find '$NVM_VERSION' version."
112116
exit 1
113117
fi
114118
fi
115119

116120
local fetch_error
117121
if [ -d "$INSTALL_DIR/.git" ]; then
118122
# Updating repo
119-
echo "=> nvm is already installed in $INSTALL_DIR, trying to update using git"
123+
nvm_echo "=> nvm is already installed in $INSTALL_DIR, trying to update using git"
120124
command printf '\r=> '
121125
fetch_error="Failed to update nvm with $NVM_VERSION, run 'git fetch' in $INSTALL_DIR yourself."
122126
else
123127
fetch_error="Failed to fetch origin with $NVM_VERSION. Please report this!"
124-
echo "=> Downloading nvm from git to '$INSTALL_DIR'"
128+
nvm_echo "=> Downloading nvm from git to '$INSTALL_DIR'"
125129
command printf '\r=> '
126130
mkdir -p "${INSTALL_DIR}"
127131
if [ "$(ls -A "${INSTALL_DIR}")" ]; then
128132
# Initializing repo
129133
command git init "${INSTALL_DIR}" || {
130-
echo >&2 'Failed to initialize nvm repo. Please report this!'
134+
nvm_echo >&2 'Failed to initialize nvm repo. Please report this!'
131135
exit 2
132136
}
133137
command git --git-dir="${INSTALL_DIR}/.git" remote add origin "$(nvm_source)" 2> /dev/null \
134138
|| command git --git-dir="${INSTALL_DIR}/.git" remote set-url origin "$(nvm_source)" || {
135-
echo >&2 'Failed to add remote "origin" (or set the URL). Please report this!'
139+
nvm_echo >&2 'Failed to add remote "origin" (or set the URL). Please report this!'
136140
exit 2
137141
}
138142
else
139143
# Cloning repo
140144
command git clone "$(nvm_source)" --depth=1 "${INSTALL_DIR}" || {
141-
echo >&2 'Failed to clone nvm repo. Please report this!'
145+
nvm_echo >&2 'Failed to clone nvm repo. Please report this!'
142146
exit 2
143147
}
144148
fi
@@ -148,28 +152,28 @@ install_nvm_from_git() {
148152
:
149153
# Fetch given version
150154
elif ! command git --git-dir="$INSTALL_DIR"/.git --work-tree="$INSTALL_DIR" fetch origin "$NVM_VERSION" --depth=1; then
151-
echo >&2 "$fetch_error"
155+
nvm_echo >&2 "$fetch_error"
152156
exit 1
153157
fi
154158
command git -c advice.detachedHead=false --git-dir="$INSTALL_DIR"/.git --work-tree="$INSTALL_DIR" checkout -f --quiet FETCH_HEAD || {
155-
echo >&2 "Failed to checkout the given version $NVM_VERSION. Please report this!"
159+
nvm_echo >&2 "Failed to checkout the given version $NVM_VERSION. Please report this!"
156160
exit 2
157161
}
158162
if [ -n "$(command git --git-dir="$INSTALL_DIR"/.git --work-tree="$INSTALL_DIR" show-ref refs/heads/master)" ]; then
159163
if command git --git-dir="$INSTALL_DIR"/.git --work-tree="$INSTALL_DIR" branch --quiet 2>/dev/null; then
160164
command git --git-dir="$INSTALL_DIR"/.git --work-tree="$INSTALL_DIR" branch --quiet -D master >/dev/null 2>&1
161165
else
162-
echo >&2 "Your version of git is out of date. Please update it!"
166+
nvm_echo >&2 "Your version of git is out of date. Please update it!"
163167
command git --git-dir="$INSTALL_DIR"/.git --work-tree="$INSTALL_DIR" branch -D master >/dev/null 2>&1
164168
fi
165169
fi
166170

167-
echo "=> Compressing and cleaning up git repository"
171+
nvm_echo "=> Compressing and cleaning up git repository"
168172
if ! command git --git-dir="$INSTALL_DIR"/.git --work-tree="$INSTALL_DIR" reflog expire --expire=now --all; then
169-
echo >&2 "Your version of git is out of date. Please update it!"
173+
nvm_echo >&2 "Your version of git is out of date. Please update it!"
170174
fi
171175
if ! command git --git-dir="$INSTALL_DIR"/.git --work-tree="$INSTALL_DIR" gc --auto --aggressive --prune=now ; then
172-
echo >&2 "Your version of git is out of date. Please update it!"
176+
nvm_echo >&2 "Your version of git is out of date. Please update it!"
173177
fi
174178
return
175179
}
@@ -185,15 +189,15 @@ nvm_install_node() {
185189
return 0
186190
fi
187191

188-
echo "=> Installing Node.js version $NODE_VERSION_LOCAL"
192+
nvm_echo "=> Installing Node.js version $NODE_VERSION_LOCAL"
189193
nvm install "$NODE_VERSION_LOCAL"
190194
local CURRENT_NVM_NODE
191195

192196
CURRENT_NVM_NODE="$(nvm_version current)"
193197
if [ "$(nvm_version "$NODE_VERSION_LOCAL")" == "$CURRENT_NVM_NODE" ]; then
194-
echo "=> Node.js version $NODE_VERSION_LOCAL has been successfully installed"
198+
nvm_echo "=> Node.js version $NODE_VERSION_LOCAL has been successfully installed"
195199
else
196-
echo >&2 "Failed to install Node.js $NODE_VERSION_LOCAL"
200+
nvm_echo >&2 "Failed to install Node.js $NODE_VERSION_LOCAL"
197201
fi
198202
}
199203

@@ -210,28 +214,28 @@ install_nvm_as_script() {
210214
# Downloading to $INSTALL_DIR
211215
mkdir -p "$INSTALL_DIR"
212216
if [ -f "$INSTALL_DIR/nvm.sh" ]; then
213-
echo "=> nvm is already installed in $INSTALL_DIR, trying to update the script"
217+
nvm_echo "=> nvm is already installed in $INSTALL_DIR, trying to update the script"
214218
else
215-
echo "=> Downloading nvm as script to '$INSTALL_DIR'"
219+
nvm_echo "=> Downloading nvm as script to '$INSTALL_DIR'"
216220
fi
217221
nvm_download -s "$NVM_SOURCE_LOCAL" -o "$INSTALL_DIR/nvm.sh" || {
218-
echo >&2 "Failed to download '$NVM_SOURCE_LOCAL'"
222+
nvm_echo >&2 "Failed to download '$NVM_SOURCE_LOCAL'"
219223
return 1
220224
} &
221225
nvm_download -s "$NVM_EXEC_SOURCE" -o "$INSTALL_DIR/nvm-exec" || {
222-
echo >&2 "Failed to download '$NVM_EXEC_SOURCE'"
226+
nvm_echo >&2 "Failed to download '$NVM_EXEC_SOURCE'"
223227
return 2
224228
} &
225229
nvm_download -s "$NVM_BASH_COMPLETION_SOURCE" -o "$INSTALL_DIR/bash_completion" || {
226-
echo >&2 "Failed to download '$NVM_BASH_COMPLETION_SOURCE'"
230+
nvm_echo >&2 "Failed to download '$NVM_BASH_COMPLETION_SOURCE'"
227231
return 2
228232
} &
229233
for job in $(jobs -p | command sort)
230234
do
231235
wait "$job" || return $?
232236
done
233237
chmod a+x "$INSTALL_DIR/nvm-exec" || {
234-
echo >&2 "Failed to mark '$INSTALL_DIR/nvm-exec' as executable"
238+
nvm_echo >&2 "Failed to mark '$INSTALL_DIR/nvm-exec' as executable"
235239
return 3
236240
}
237241
}
@@ -240,7 +244,7 @@ nvm_try_profile() {
240244
if [ -z "${1-}" ] || [ ! -f "${1}" ]; then
241245
return 1
242246
fi
243-
echo "${1}"
247+
nvm_echo "${1}"
244248
}
245249

246250
#
@@ -256,7 +260,7 @@ nvm_detect_profile() {
256260
fi
257261

258262
if [ -n "${PROFILE}" ] && [ -f "${PROFILE}" ]; then
259-
echo "${PROFILE}"
263+
nvm_echo "${PROFILE}"
260264
return
261265
fi
262266

@@ -283,7 +287,7 @@ nvm_detect_profile() {
283287
fi
284288

285289
if [ -n "$DETECTED_PROFILE" ]; then
286-
echo "$DETECTED_PROFILE"
290+
nvm_echo "$DETECTED_PROFILE"
287291
fi
288292
}
289293

@@ -316,37 +320,37 @@ nvm_check_global_modules() {
316320

317321
if [ "${MODULE_COUNT}" != '0' ]; then
318322
# shellcheck disable=SC2016
319-
echo '=> You currently have modules installed globally with `npm`. These will no'
323+
nvm_echo '=> You currently have modules installed globally with `npm`. These will no'
320324
# shellcheck disable=SC2016
321-
echo '=> longer be linked to the active version of Node when you install a new node'
325+
nvm_echo '=> longer be linked to the active version of Node when you install a new node'
322326
# shellcheck disable=SC2016
323-
echo '=> with `nvm`; and they may (depending on how you construct your `$PATH`)'
327+
nvm_echo '=> with `nvm`; and they may (depending on how you construct your `$PATH`)'
324328
# shellcheck disable=SC2016
325-
echo '=> override the binaries of modules installed with `nvm`:'
326-
echo
329+
nvm_echo '=> override the binaries of modules installed with `nvm`:'
330+
nvm_echo
327331

328332
command printf %s\\n "$NPM_GLOBAL_MODULES"
329-
echo '=> If you wish to uninstall them at a later point (or re-install them under your'
333+
nvm_echo '=> If you wish to uninstall them at a later point (or re-install them under your'
330334
# shellcheck disable=SC2016
331-
echo '=> `nvm` Nodes), you can remove them from the system Node as follows:'
332-
echo
333-
echo ' $ nvm use system'
334-
echo ' $ npm uninstall -g a_module'
335-
echo
335+
nvm_echo '=> `nvm` Nodes), you can remove them from the system Node as follows:'
336+
nvm_echo
337+
nvm_echo ' $ nvm use system'
338+
nvm_echo ' $ npm uninstall -g a_module'
339+
nvm_echo
336340
fi
337341
}
338342

339343
nvm_do_install() {
340344
if [ -n "${NVM_DIR-}" ] && ! [ -d "${NVM_DIR}" ]; then
341345
if [ -e "${NVM_DIR}" ]; then
342-
echo >&2 "File \"${NVM_DIR}\" has the same name as installation directory."
346+
nvm_echo >&2 "File \"${NVM_DIR}\" has the same name as installation directory."
343347
exit 1
344348
fi
345349

346350
if [ "${NVM_DIR}" = "$(nvm_default_install_dir)" ]; then
347351
mkdir "${NVM_DIR}"
348352
else
349-
echo >&2 "You have \$NVM_DIR set to \"${NVM_DIR}\", but that directory does not exist. Check your profile files and environment."
353+
nvm_echo >&2 "You have \$NVM_DIR set to \"${NVM_DIR}\", but that directory does not exist. Check your profile files and environment."
350354
exit 1
351355
fi
352356
fi
@@ -357,27 +361,27 @@ nvm_do_install() {
357361
elif nvm_has nvm_download; then
358362
install_nvm_as_script
359363
else
360-
echo >&2 'You need git, curl, or wget to install nvm'
364+
nvm_echo >&2 'You need git, curl, or wget to install nvm'
361365
exit 1
362366
fi
363367
elif [ "${METHOD}" = 'git' ]; then
364368
if ! nvm_has git; then
365-
echo >&2 "You need git to install nvm"
369+
nvm_echo >&2 "You need git to install nvm"
366370
exit 1
367371
fi
368372
install_nvm_from_git
369373
elif [ "${METHOD}" = 'script' ]; then
370374
if ! nvm_has nvm_download; then
371-
echo >&2 "You need curl or wget to install nvm"
375+
nvm_echo >&2 "You need curl or wget to install nvm"
372376
exit 1
373377
fi
374378
install_nvm_as_script
375379
else
376-
echo >&2 "The environment variable \$METHOD is set to \"${METHOD}\", which is not recognized as a valid installation method."
380+
nvm_echo >&2 "The environment variable \$METHOD is set to \"${METHOD}\", which is not recognized as a valid installation method."
377381
exit 1
378382
fi
379383

380-
echo
384+
nvm_echo
381385

382386
local NVM_PROFILE
383387
NVM_PROFILE="$(nvm_detect_profile)"
@@ -395,32 +399,32 @@ nvm_do_install() {
395399
if [ -n "${PROFILE}" ]; then
396400
TRIED_PROFILE="${NVM_PROFILE} (as defined in \$PROFILE), "
397401
fi
398-
echo "=> Profile not found. Tried ${TRIED_PROFILE-}~/.bashrc, ~/.bash_profile, ~/.zshrc, and ~/.profile."
399-
echo "=> Create one of them and run this script again"
400-
echo " OR"
401-
echo "=> Append the following lines to the correct file yourself:"
402+
nvm_echo "=> Profile not found. Tried ${TRIED_PROFILE-}~/.bashrc, ~/.bash_profile, ~/.zshrc, and ~/.profile."
403+
nvm_echo "=> Create one of them and run this script again"
404+
nvm_echo " OR"
405+
nvm_echo "=> Append the following lines to the correct file yourself:"
402406
command printf "${SOURCE_STR}"
403-
echo
407+
nvm_echo
404408
else
405409
if nvm_profile_is_bash_or_zsh "${NVM_PROFILE-}"; then
406410
BASH_OR_ZSH=true
407411
fi
408412
if ! command grep -qc '/nvm.sh' "$NVM_PROFILE"; then
409-
echo "=> Appending nvm source string to $NVM_PROFILE"
413+
nvm_echo "=> Appending nvm source string to $NVM_PROFILE"
410414
command printf "${SOURCE_STR}" >> "$NVM_PROFILE"
411415
else
412-
echo "=> nvm source string already in ${NVM_PROFILE}"
416+
nvm_echo "=> nvm source string already in ${NVM_PROFILE}"
413417
fi
414418
# shellcheck disable=SC2016
415419
if ${BASH_OR_ZSH} && ! command grep -qc '$NVM_DIR/bash_completion' "$NVM_PROFILE"; then
416-
echo "=> Appending bash_completion source string to $NVM_PROFILE"
420+
nvm_echo "=> Appending bash_completion source string to $NVM_PROFILE"
417421
command printf "$COMPLETION_STR" >> "$NVM_PROFILE"
418422
else
419-
echo "=> bash_completion source string already in ${NVM_PROFILE}"
423+
nvm_echo "=> bash_completion source string already in ${NVM_PROFILE}"
420424
fi
421425
fi
422426
if ${BASH_OR_ZSH} && [ -z "${NVM_PROFILE-}" ] ; then
423-
echo "=> Please also append the following lines to the if you are using bash/zsh shell:"
427+
nvm_echo "=> Please also append the following lines to the if you are using bash/zsh shell:"
424428
command printf "${COMPLETION_STR}"
425429
fi
426430

@@ -434,7 +438,7 @@ nvm_do_install() {
434438

435439
nvm_reset
436440

437-
echo "=> Close and reopen your terminal to start using nvm or run the following to use it now:"
441+
nvm_echo "=> Close and reopen your terminal to start using nvm or run the following to use it now:"
438442
command printf "${SOURCE_STR}"
439443
if ${BASH_OR_ZSH} ; then
440444
command printf "${COMPLETION_STR}"

0 commit comments

Comments
 (0)