@@ -6,6 +6,10 @@ nvm_has() {
6
6
type " $1 " > /dev/null 2>&1
7
7
}
8
8
9
+ nvm_echo () {
10
+ command printf %s\\ n " $* " 2> /dev/null
11
+ }
12
+
9
13
nvm_grep () {
10
14
GREP_OPTIONS=' ' command grep " $@ "
11
15
}
@@ -23,7 +27,7 @@ nvm_install_dir() {
23
27
}
24
28
25
29
nvm_latest_version () {
26
- echo " v0.37.2"
30
+ nvm_echo " v0.37.2"
27
31
}
28
32
29
33
nvm_profile_is_bash_or_zsh () {
@@ -64,18 +68,18 @@ nvm_source() {
64
68
elif [ " _$NVM_METHOD " = " _git" ] || [ -z " $NVM_METHOD " ]; then
65
69
NVM_SOURCE_URL=" https://github.com/${NVM_GITHUB_REPO} .git"
66
70
else
67
- echo >&2 " Unexpected value \" $NVM_METHOD \" for \$ NVM_METHOD"
71
+ nvm_echo >&2 " Unexpected value \" $NVM_METHOD \" for \$ NVM_METHOD"
68
72
return 1
69
73
fi
70
74
fi
71
- echo " $NVM_SOURCE_URL "
75
+ nvm_echo " $NVM_SOURCE_URL "
72
76
}
73
77
74
78
#
75
79
# Node.js version to install
76
80
#
77
81
nvm_node_version () {
78
- echo " $NODE_VERSION "
82
+ nvm_echo " $NODE_VERSION "
79
83
}
80
84
81
85
nvm_download () {
@@ -108,37 +112,37 @@ install_nvm_from_git() {
108
112
:
109
113
# Check if version is an existing changeset
110
114
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."
112
116
exit 1
113
117
fi
114
118
fi
115
119
116
120
local fetch_error
117
121
if [ -d " $INSTALL_DIR /.git" ]; then
118
122
# 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"
120
124
command printf ' \r=> '
121
125
fetch_error=" Failed to update nvm with $NVM_VERSION , run 'git fetch' in $INSTALL_DIR yourself."
122
126
else
123
127
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 '"
125
129
command printf ' \r=> '
126
130
mkdir -p " ${INSTALL_DIR} "
127
131
if [ " $( ls -A " ${INSTALL_DIR} " ) " ]; then
128
132
# Initializing repo
129
133
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!'
131
135
exit 2
132
136
}
133
137
command git --git-dir=" ${INSTALL_DIR} /.git" remote add origin " $( nvm_source) " 2> /dev/null \
134
138
|| 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!'
136
140
exit 2
137
141
}
138
142
else
139
143
# Cloning repo
140
144
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!'
142
146
exit 2
143
147
}
144
148
fi
@@ -148,28 +152,28 @@ install_nvm_from_git() {
148
152
:
149
153
# Fetch given version
150
154
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 "
152
156
exit 1
153
157
fi
154
158
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!"
156
160
exit 2
157
161
}
158
162
if [ -n " $( command git --git-dir=" $INSTALL_DIR " /.git --work-tree=" $INSTALL_DIR " show-ref refs/heads/master) " ]; then
159
163
if command git --git-dir=" $INSTALL_DIR " /.git --work-tree=" $INSTALL_DIR " branch --quiet 2> /dev/null; then
160
164
command git --git-dir=" $INSTALL_DIR " /.git --work-tree=" $INSTALL_DIR " branch --quiet -D master > /dev/null 2>&1
161
165
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!"
163
167
command git --git-dir=" $INSTALL_DIR " /.git --work-tree=" $INSTALL_DIR " branch -D master > /dev/null 2>&1
164
168
fi
165
169
fi
166
170
167
- echo " => Compressing and cleaning up git repository"
171
+ nvm_echo " => Compressing and cleaning up git repository"
168
172
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!"
170
174
fi
171
175
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!"
173
177
fi
174
178
return
175
179
}
@@ -185,15 +189,15 @@ nvm_install_node() {
185
189
return 0
186
190
fi
187
191
188
- echo " => Installing Node.js version $NODE_VERSION_LOCAL "
192
+ nvm_echo " => Installing Node.js version $NODE_VERSION_LOCAL "
189
193
nvm install " $NODE_VERSION_LOCAL "
190
194
local CURRENT_NVM_NODE
191
195
192
196
CURRENT_NVM_NODE=" $( nvm_version current) "
193
197
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"
195
199
else
196
- echo >&2 " Failed to install Node.js $NODE_VERSION_LOCAL "
200
+ nvm_echo >&2 " Failed to install Node.js $NODE_VERSION_LOCAL "
197
201
fi
198
202
}
199
203
@@ -210,28 +214,28 @@ install_nvm_as_script() {
210
214
# Downloading to $INSTALL_DIR
211
215
mkdir -p " $INSTALL_DIR "
212
216
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"
214
218
else
215
- echo " => Downloading nvm as script to '$INSTALL_DIR '"
219
+ nvm_echo " => Downloading nvm as script to '$INSTALL_DIR '"
216
220
fi
217
221
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 '"
219
223
return 1
220
224
} &
221
225
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 '"
223
227
return 2
224
228
} &
225
229
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 '"
227
231
return 2
228
232
} &
229
233
for job in $( jobs -p | command sort)
230
234
do
231
235
wait " $job " || return $?
232
236
done
233
237
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"
235
239
return 3
236
240
}
237
241
}
@@ -240,7 +244,7 @@ nvm_try_profile() {
240
244
if [ -z " ${1-} " ] || [ ! -f " ${1} " ]; then
241
245
return 1
242
246
fi
243
- echo " ${1} "
247
+ nvm_echo " ${1} "
244
248
}
245
249
246
250
#
@@ -256,7 +260,7 @@ nvm_detect_profile() {
256
260
fi
257
261
258
262
if [ -n " ${PROFILE} " ] && [ -f " ${PROFILE} " ]; then
259
- echo " ${PROFILE} "
263
+ nvm_echo " ${PROFILE} "
260
264
return
261
265
fi
262
266
@@ -283,7 +287,7 @@ nvm_detect_profile() {
283
287
fi
284
288
285
289
if [ -n " $DETECTED_PROFILE " ]; then
286
- echo " $DETECTED_PROFILE "
290
+ nvm_echo " $DETECTED_PROFILE "
287
291
fi
288
292
}
289
293
@@ -316,37 +320,37 @@ nvm_check_global_modules() {
316
320
317
321
if [ " ${MODULE_COUNT} " != ' 0' ]; then
318
322
# 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'
320
324
# 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'
322
326
# 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`)'
324
328
# 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
327
331
328
332
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'
330
334
# 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
336
340
fi
337
341
}
338
342
339
343
nvm_do_install () {
340
344
if [ -n " ${NVM_DIR-} " ] && ! [ -d " ${NVM_DIR} " ]; then
341
345
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."
343
347
exit 1
344
348
fi
345
349
346
350
if [ " ${NVM_DIR} " = " $( nvm_default_install_dir) " ]; then
347
351
mkdir " ${NVM_DIR} "
348
352
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."
350
354
exit 1
351
355
fi
352
356
fi
@@ -357,27 +361,27 @@ nvm_do_install() {
357
361
elif nvm_has nvm_download; then
358
362
install_nvm_as_script
359
363
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'
361
365
exit 1
362
366
fi
363
367
elif [ " ${METHOD} " = ' git' ]; then
364
368
if ! nvm_has git; then
365
- echo >&2 " You need git to install nvm"
369
+ nvm_echo >&2 " You need git to install nvm"
366
370
exit 1
367
371
fi
368
372
install_nvm_from_git
369
373
elif [ " ${METHOD} " = ' script' ]; then
370
374
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"
372
376
exit 1
373
377
fi
374
378
install_nvm_as_script
375
379
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."
377
381
exit 1
378
382
fi
379
383
380
- echo
384
+ nvm_echo
381
385
382
386
local NVM_PROFILE
383
387
NVM_PROFILE=" $( nvm_detect_profile) "
@@ -395,32 +399,32 @@ nvm_do_install() {
395
399
if [ -n " ${PROFILE} " ]; then
396
400
TRIED_PROFILE=" ${NVM_PROFILE} (as defined in \$ PROFILE), "
397
401
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:"
402
406
command printf " ${SOURCE_STR} "
403
- echo
407
+ nvm_echo
404
408
else
405
409
if nvm_profile_is_bash_or_zsh " ${NVM_PROFILE-} " ; then
406
410
BASH_OR_ZSH=true
407
411
fi
408
412
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 "
410
414
command printf " ${SOURCE_STR} " >> " $NVM_PROFILE "
411
415
else
412
- echo " => nvm source string already in ${NVM_PROFILE} "
416
+ nvm_echo " => nvm source string already in ${NVM_PROFILE} "
413
417
fi
414
418
# shellcheck disable=SC2016
415
419
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 "
417
421
command printf " $COMPLETION_STR " >> " $NVM_PROFILE "
418
422
else
419
- echo " => bash_completion source string already in ${NVM_PROFILE} "
423
+ nvm_echo " => bash_completion source string already in ${NVM_PROFILE} "
420
424
fi
421
425
fi
422
426
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:"
424
428
command printf " ${COMPLETION_STR} "
425
429
fi
426
430
@@ -434,7 +438,7 @@ nvm_do_install() {
434
438
435
439
nvm_reset
436
440
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:"
438
442
command printf " ${SOURCE_STR} "
439
443
if ${BASH_OR_ZSH} ; then
440
444
command printf " ${COMPLETION_STR} "
0 commit comments