Skip to content
Myr

[Fix] `nvm use`: fix `--silent` when version is omitted

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Fixed a bug where --silent mode was failing for nvm use I ran a test that just ran ```nvm use node --silent``` that I wrote by myself. Unfortunately I noticed a bug where it still prints out some messages in different cases. This pull request is to fix that bug. * Added in an argument called ***quiet*** to the nvm_rc_version() function at *line 339* * Printed anything inside the nvm_rc_version() only in the scenario where quiet mode is off * Ran the nvm_rc_version() function in quiet mode only if silent mode is on in the "use" command of nvm at *line 2990* * Ran *nvm_echo* and *nvm_err* inside the "use" command of nvm only in the scenario where silent mode is off (There were 4 scenarios where this was forgotten) * Edited the ```nvm deactivate``` command to include a silent mode * Changed the help page to include the --silent option for ```nvm deactivate``` * Added in aliases for the --silent flag in ```nvm deactivate``` and ```nvm use``` * Used silent mode inside the ```nvm use``` when running ```nvm deactivate``` inside it and silent mode is on A test was attached in the 'test/slow/nvm use' directory. It is named *Running "nvm use node --silent" doesn't print anything*. It runs ```nvm use node --silent and checks``` and succeeds if the OUTPUT matches the EXPECTED_OUTPUT which is *null* or *''*. When the test was run locally, it still ouputed results if someone deleted some contents of the *.nvm/* folder. This output was given from the ```nvm deactivate command```. This was the reason that the ```nvm deactivate``` was edited to include silent mode.

authored by Yash-Singh1 · committed by Jordan Harband · parent 16b75b7 · Browse files · 2 files+43 added−19 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+43 added−19 removed

  • tests:+13 added−0 removed
  • other:+30 added−19 removed

2 files+43 added−19 removed

added:test/slow/nvm use/Running "nvm use node --silent" doesn't print anything+13 added
1#!/bin/sh23die () { echo "$@" ; exit 1; }45\. ../../../nvm.sh67nvm deactivate 2>&1 >/dev/null || die 'deactivate failed'89OUTPUT=$(nvm use node --silent || die 'nvm use node failed')10EXPECTED_OUTPUT=""1112[ "_$OUTPUT" = "_$EXPECTED_OUTPUT" ] \13 || die "'nvm use node --silent' output was not silenced to '$EXPECTED_OUTPUT'; got '$OUTPUT'"
modified:nvm.sh+30 added-19 removed
339 unmodified lines
340 export NVM_RC_VERSION=''341 local NVMRC_PATH342 NVMRC_PATH="$(nvm_find_nvmrc)"343 if [ ! -e "${NVMRC_PATH}" ]; then344 if [ "${NVM_SILENT:-0}" -ne 1 ]; then344 nvm_err "No .nvmrc file found"345 nvm_err "No .nvmrc file found"346 fi347 return 1348 fi349 NVM_RC_VERSION="$(command head -n 1 "${NVMRC_PATH}" | command tr -d '\r')" || command printf ''350 if [ -z "${NVM_RC_VERSION}" ]; then351 if [ "${NVM_SILENT:-0}" -ne 1 ]; then349 nvm_err "Warning: empty .nvmrc file found at \"${NVMRC_PATH}\""352 nvm_err "Warning: empty .nvmrc file found at \"${NVMRC_PATH}\""353 fi354 return 2355 fi356 if [ "${NVM_SILENT:-0}" -ne 1 ]; then352 nvm_echo "Found '${NVMRC_PATH}' with version <${NVM_RC_VERSION}>"357 nvm_echo "Found '${NVMRC_PATH}' with version <${NVM_RC_VERSION}>"358 fi359}360361nvm_clang_version() {362 clang --version | command awk '{ if ($2 == "version") print $3; else if ($3 == "version") print $4 }' | command sed 's/-.*$//g'
2637 unmodified lines
3000 unset NVM_INC3001 ;;3002 "use")3003 local PROVIDED_VERSION2998 local NVM_USE_SILENT2999 NVM_USE_SILENT=03004 local NVM_SILENT3005 local NVM_SILENT_ARG3006 local NVM_DELETE_PREFIX3007 NVM_DELETE_PREFIX=03008 local NVM_LTS30093010 while [ $# -ne 0 ]; do3011 case "$1" in3006 --silent) NVM_USE_SILENT=1 ;;3012 --silent)3013 NVM_SILENT=13014 NVM_SILENT_ARG='--silent'3015 ;;3016 --delete-prefix) NVM_DELETE_PREFIX=1 ;;3017 --) ;;3018 --lts) NVM_LTS='*' ;;3019 --lts=*) NVM_LTS="${1##--lts=}" ;;
9 unmodified lines
30293030 if [ -n "${NVM_LTS-}" ]; then3031 VERSION="$(nvm_match_version "lts/${NVM_LTS:-*}")"3032 elif [ -z "${PROVIDED_VERSION-}" ]; then3024 nvm_rc_version3033 NVM_SILENT="${NVM_SILENT:-0}" nvm_rc_version3034 if [ -n "${NVM_RC_VERSION-}" ]; then3035 PROVIDED_VERSION="${NVM_RC_VERSION}"3036 VERSION="$(nvm_version "${PROVIDED_VERSION}")"3037 fi
11 unmodified lines
3049 return 1273050 fi30513052 if [ "_${VERSION}" = '_system' ]; then3044 if nvm_has_system_node && nvm deactivate >/dev/null 2>&1; then3045 if [ $NVM_USE_SILENT -ne 1 ]; then3053 if nvm_has_system_node && nvm deactivate "${NVM_SILENT_ARG-}" >/dev/null 2>&1; then3054 if [ "${NVM_SILENT:-0}" -ne 1 ]; then3055 nvm_echo "Now using system version of node: $(node -v 2>/dev/null)$(nvm_print_npm_version)"3056 fi3057 return3049 elif nvm_has_system_iojs && nvm deactivate >/dev/null 2>&1; then3050 if [ $NVM_USE_SILENT -ne 1 ]; then3058 elif nvm_has_system_iojs && nvm deactivate "${NVM_SILENT_ARG-}" >/dev/null 2>&1; then3059 if [ "${NVM_SILENT:-0}" -ne 1 ]; then3060 nvm_echo "Now using system version of io.js: $(iojs --version 2>/dev/null)$(nvm_print_npm_version)"3061 fi3062 return3054 elif [ $NVM_USE_SILENT -ne 1 ]; then3063 elif [ "${NVM_SILENT:-0}" -ne 1 ]; then3064 nvm_err 'System version of node not found.'3065 fi3066 return 1273067 elif [ "_${VERSION}" = "_∞" ]; then3059 if [ $NVM_USE_SILENT -ne 1 ]; then3068 if [ "${NVM_SILENT:-0}" -ne 1 ]; then3069 nvm_err "The alias \"${PROVIDED_VERSION}\" leads to an infinite loop. Aborting."3070 fi3071 return 83072 fi3073 if [ "${VERSION}" = 'N/A' ]; then3074 if [ "${NVM_SILENT:-0}" -ne 1 ]; then3065 nvm_err "N/A: version \"${PROVIDED_VERSION} -> ${VERSION}\" is not yet installed."3066 nvm_err ""3067 nvm_err "You need to run \"nvm install ${PROVIDED_VERSION}\" to install it before using it."3075 nvm_err "N/A: version \"${PROVIDED_VERSION} -> ${VERSION}\" is not yet installed."3076 nvm_err ""3077 nvm_err "You need to run \"nvm install ${PROVIDED_VERSION}\" to install it before using it."3078 fi3079 return 33080 # This nvm_ensure_version_installed call can be a performance bottleneck3081 # on shell startup. Perhaps we can optimize it away or make it faster.3082 elif ! nvm_ensure_version_installed "${VERSION}"; then
22 unmodified lines
3105 command rm -f "${NVM_DIR}/current" && ln -s "${NVM_VERSION_DIR}" "${NVM_DIR}/current"3106 fi3107 local NVM_USE_OUTPUT3108 NVM_USE_OUTPUT=''3098 if [ $NVM_USE_SILENT -ne 1 ]; then3109 if [ "${NVM_SILENT:-0}" -ne 1 ]; then3110 if nvm_is_iojs_version "${VERSION}"; then3111 NVM_USE_OUTPUT="Now using io.js $(nvm_strip_iojs_prefix "${VERSION}")$(nvm_print_npm_version)"3112 else3113 NVM_USE_OUTPUT="Now using node ${VERSION}$(nvm_print_npm_version)"
4 unmodified lines
3118 NVM_USE_CMD="nvm use --delete-prefix"3119 if [ -n "${PROVIDED_VERSION}" ]; then3120 NVM_USE_CMD="${NVM_USE_CMD} ${VERSION}"3121 fi3111 if [ $NVM_USE_SILENT -eq 1 ]; then3122 if [ "${NVM_SILENT:-0}" -eq 1 ]; then3123 NVM_USE_CMD="${NVM_USE_CMD} --silent"3124 fi3125 if ! nvm_die_on_prefix "${NVM_DELETE_PREFIX}" "${NVM_USE_CMD}"; then3126 return 113127 fi3128 fi3118 if [ -n "${NVM_USE_OUTPUT-}" ]; then3129 if [ -n "${NVM_USE_OUTPUT-}" ] && [ "${NVM_SILENT:-0}" -ne 1 ]; then3130 nvm_echo "${NVM_USE_OUTPUT}"3131 fi3132 ;;3133 "run")
685 unmodified lines