mirror of
				https://github.com/nvm-sh/nvm.git
				synced 2025-10-31 10:15:53 +08:00 
			
		
		
		
	[Fix] nvm use: fix --silent when version is omitted
				
					
				
			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.
This commit is contained in:
		
				
					committed by
					
						 Jordan Harband
						Jordan Harband
					
				
			
			
				
	
			
			
			
						parent
						
							16b75b7333
						
					
				
				
					commit
					b1200c659b
				
			
							
								
								
									
										49
									
								
								nvm.sh
									
									
									
									
									
								
							
							
						
						
									
										49
									
								
								nvm.sh
									
									
									
									
									
								
							| @@ -341,15 +341,21 @@ nvm_rc_version() { | |||||||
|   local NVMRC_PATH |   local NVMRC_PATH | ||||||
|   NVMRC_PATH="$(nvm_find_nvmrc)" |   NVMRC_PATH="$(nvm_find_nvmrc)" | ||||||
|   if [ ! -e "${NVMRC_PATH}" ]; then |   if [ ! -e "${NVMRC_PATH}" ]; then | ||||||
|     nvm_err "No .nvmrc file found" |     if [ "${NVM_SILENT:-0}" -ne 1 ]; then | ||||||
|  |       nvm_err "No .nvmrc file found" | ||||||
|  |     fi | ||||||
|     return 1 |     return 1 | ||||||
|   fi |   fi | ||||||
|   NVM_RC_VERSION="$(command head -n 1 "${NVMRC_PATH}" | command tr -d '\r')" || command printf '' |   NVM_RC_VERSION="$(command head -n 1 "${NVMRC_PATH}" | command tr -d '\r')" || command printf '' | ||||||
|   if [ -z "${NVM_RC_VERSION}" ]; then |   if [ -z "${NVM_RC_VERSION}" ]; then | ||||||
|     nvm_err "Warning: empty .nvmrc file found at \"${NVMRC_PATH}\"" |     if [ "${NVM_SILENT:-0}" -ne 1 ]; then | ||||||
|  |       nvm_err "Warning: empty .nvmrc file found at \"${NVMRC_PATH}\"" | ||||||
|  |     fi | ||||||
|     return 2 |     return 2 | ||||||
|   fi |   fi | ||||||
|   nvm_echo "Found '${NVMRC_PATH}' with version <${NVM_RC_VERSION}>" |   if [ "${NVM_SILENT:-0}" -ne 1 ]; then | ||||||
|  |     nvm_echo "Found '${NVMRC_PATH}' with version <${NVM_RC_VERSION}>" | ||||||
|  |   fi | ||||||
| } | } | ||||||
|  |  | ||||||
| nvm_clang_version() { | nvm_clang_version() { | ||||||
| @@ -2995,15 +3001,18 @@ nvm() { | |||||||
|     ;; |     ;; | ||||||
|     "use") |     "use") | ||||||
|       local PROVIDED_VERSION |       local PROVIDED_VERSION | ||||||
|       local NVM_USE_SILENT |       local NVM_SILENT | ||||||
|       NVM_USE_SILENT=0 |       local NVM_SILENT_ARG | ||||||
|       local NVM_DELETE_PREFIX |       local NVM_DELETE_PREFIX | ||||||
|       NVM_DELETE_PREFIX=0 |       NVM_DELETE_PREFIX=0 | ||||||
|       local NVM_LTS |       local NVM_LTS | ||||||
|  |  | ||||||
|       while [ $# -ne 0 ]; do |       while [ $# -ne 0 ]; do | ||||||
|         case "$1" in |         case "$1" in | ||||||
|           --silent) NVM_USE_SILENT=1 ;; |           --silent) | ||||||
|  |             NVM_SILENT=1 | ||||||
|  |             NVM_SILENT_ARG='--silent' | ||||||
|  |           ;; | ||||||
|           --delete-prefix) NVM_DELETE_PREFIX=1 ;; |           --delete-prefix) NVM_DELETE_PREFIX=1 ;; | ||||||
|           --) ;; |           --) ;; | ||||||
|           --lts) NVM_LTS='*' ;; |           --lts) NVM_LTS='*' ;; | ||||||
| @@ -3021,7 +3030,7 @@ nvm() { | |||||||
|       if [ -n "${NVM_LTS-}" ]; then |       if [ -n "${NVM_LTS-}" ]; then | ||||||
|         VERSION="$(nvm_match_version "lts/${NVM_LTS:-*}")" |         VERSION="$(nvm_match_version "lts/${NVM_LTS:-*}")" | ||||||
|       elif [ -z "${PROVIDED_VERSION-}" ]; then |       elif [ -z "${PROVIDED_VERSION-}" ]; then | ||||||
|         nvm_rc_version |         NVM_SILENT="${NVM_SILENT:-0}" nvm_rc_version | ||||||
|         if [ -n "${NVM_RC_VERSION-}" ]; then |         if [ -n "${NVM_RC_VERSION-}" ]; then | ||||||
|           PROVIDED_VERSION="${NVM_RC_VERSION}" |           PROVIDED_VERSION="${NVM_RC_VERSION}" | ||||||
|           VERSION="$(nvm_version "${PROVIDED_VERSION}")" |           VERSION="$(nvm_version "${PROVIDED_VERSION}")" | ||||||
| @@ -3041,30 +3050,32 @@ nvm() { | |||||||
|       fi |       fi | ||||||
|  |  | ||||||
|       if [ "_${VERSION}" = '_system' ]; then |       if [ "_${VERSION}" = '_system' ]; then | ||||||
|         if nvm_has_system_node && nvm deactivate >/dev/null 2>&1; then |         if nvm_has_system_node && nvm deactivate "${NVM_SILENT_ARG-}" >/dev/null 2>&1; then | ||||||
|           if [ $NVM_USE_SILENT -ne 1 ]; then |           if [ "${NVM_SILENT:-0}" -ne 1 ]; then | ||||||
|             nvm_echo "Now using system version of node: $(node -v 2>/dev/null)$(nvm_print_npm_version)" |             nvm_echo "Now using system version of node: $(node -v 2>/dev/null)$(nvm_print_npm_version)" | ||||||
|           fi |           fi | ||||||
|           return |           return | ||||||
|         elif nvm_has_system_iojs && nvm deactivate >/dev/null 2>&1; then |         elif nvm_has_system_iojs && nvm deactivate "${NVM_SILENT_ARG-}" >/dev/null 2>&1; then | ||||||
|           if [ $NVM_USE_SILENT -ne 1 ]; then |           if [ "${NVM_SILENT:-0}" -ne 1 ]; then | ||||||
|             nvm_echo "Now using system version of io.js: $(iojs --version 2>/dev/null)$(nvm_print_npm_version)" |             nvm_echo "Now using system version of io.js: $(iojs --version 2>/dev/null)$(nvm_print_npm_version)" | ||||||
|           fi |           fi | ||||||
|           return |           return | ||||||
|         elif [ $NVM_USE_SILENT -ne 1 ]; then |         elif [ "${NVM_SILENT:-0}" -ne 1 ]; then | ||||||
|           nvm_err 'System version of node not found.' |           nvm_err 'System version of node not found.' | ||||||
|         fi |         fi | ||||||
|         return 127 |         return 127 | ||||||
|       elif [ "_${VERSION}" = "_∞" ]; then |       elif [ "_${VERSION}" = "_∞" ]; then | ||||||
|         if [ $NVM_USE_SILENT -ne 1 ]; then |         if [ "${NVM_SILENT:-0}" -ne 1 ]; then | ||||||
|           nvm_err "The alias \"${PROVIDED_VERSION}\" leads to an infinite loop. Aborting." |           nvm_err "The alias \"${PROVIDED_VERSION}\" leads to an infinite loop. Aborting." | ||||||
|         fi |         fi | ||||||
|         return 8 |         return 8 | ||||||
|       fi |       fi | ||||||
|       if [ "${VERSION}" = 'N/A' ]; then |       if [ "${VERSION}" = 'N/A' ]; then | ||||||
|         nvm_err "N/A: version \"${PROVIDED_VERSION} -> ${VERSION}\" is not yet installed." |         if [ "${NVM_SILENT:-0}" -ne 1 ]; then | ||||||
|         nvm_err "" |           nvm_err "N/A: version \"${PROVIDED_VERSION} -> ${VERSION}\" is not yet installed." | ||||||
|         nvm_err "You need to run \"nvm install ${PROVIDED_VERSION}\" to install it before using it." |           nvm_err "" | ||||||
|  |           nvm_err "You need to run \"nvm install ${PROVIDED_VERSION}\" to install it before using it." | ||||||
|  |         fi | ||||||
|         return 3 |         return 3 | ||||||
|       # This nvm_ensure_version_installed call can be a performance bottleneck |       # This nvm_ensure_version_installed call can be a performance bottleneck | ||||||
|       # on shell startup. Perhaps we can optimize it away or make it faster. |       # on shell startup. Perhaps we can optimize it away or make it faster. | ||||||
| @@ -3095,7 +3106,7 @@ nvm() { | |||||||
|       fi |       fi | ||||||
|       local NVM_USE_OUTPUT |       local NVM_USE_OUTPUT | ||||||
|       NVM_USE_OUTPUT='' |       NVM_USE_OUTPUT='' | ||||||
|       if [ $NVM_USE_SILENT -ne 1 ]; then |       if [ "${NVM_SILENT:-0}" -ne 1 ]; then | ||||||
|         if nvm_is_iojs_version "${VERSION}"; then |         if nvm_is_iojs_version "${VERSION}"; then | ||||||
|           NVM_USE_OUTPUT="Now using io.js $(nvm_strip_iojs_prefix "${VERSION}")$(nvm_print_npm_version)" |           NVM_USE_OUTPUT="Now using io.js $(nvm_strip_iojs_prefix "${VERSION}")$(nvm_print_npm_version)" | ||||||
|         else |         else | ||||||
| @@ -3108,14 +3119,14 @@ nvm() { | |||||||
|         if [ -n "${PROVIDED_VERSION}" ]; then |         if [ -n "${PROVIDED_VERSION}" ]; then | ||||||
|           NVM_USE_CMD="${NVM_USE_CMD} ${VERSION}" |           NVM_USE_CMD="${NVM_USE_CMD} ${VERSION}" | ||||||
|         fi |         fi | ||||||
|         if [ $NVM_USE_SILENT -eq 1 ]; then |         if [ "${NVM_SILENT:-0}" -eq 1 ]; then | ||||||
|           NVM_USE_CMD="${NVM_USE_CMD} --silent" |           NVM_USE_CMD="${NVM_USE_CMD} --silent" | ||||||
|         fi |         fi | ||||||
|         if ! nvm_die_on_prefix "${NVM_DELETE_PREFIX}" "${NVM_USE_CMD}"; then |         if ! nvm_die_on_prefix "${NVM_DELETE_PREFIX}" "${NVM_USE_CMD}"; then | ||||||
|           return 11 |           return 11 | ||||||
|         fi |         fi | ||||||
|       fi |       fi | ||||||
|       if [ -n "${NVM_USE_OUTPUT-}" ]; then |       if [ -n "${NVM_USE_OUTPUT-}" ] && [ "${NVM_SILENT:-0}" -ne 1 ]; then | ||||||
|         nvm_echo "${NVM_USE_OUTPUT}" |         nvm_echo "${NVM_USE_OUTPUT}" | ||||||
|       fi |       fi | ||||||
|     ;; |     ;; | ||||||
|   | |||||||
							
								
								
									
										13
									
								
								test/slow/nvm use/Running "nvm use node --silent" doesn't print anything
									
									
									
									
									
										Executable file
									
								
							
							
						
						
									
										13
									
								
								test/slow/nvm use/Running "nvm use node --silent" doesn't print anything
									
									
									
									
									
										Executable file
									
								
							| @@ -0,0 +1,13 @@ | |||||||
|  | #!/bin/sh | ||||||
|  |  | ||||||
|  | die () { echo "$@" ; exit 1; } | ||||||
|  |  | ||||||
|  | \. ../../../nvm.sh | ||||||
|  |  | ||||||
|  | nvm deactivate 2>&1 >/dev/null || die 'deactivate failed' | ||||||
|  |  | ||||||
|  | OUTPUT=$(nvm use node --silent || die 'nvm use node failed') | ||||||
|  | EXPECTED_OUTPUT="" | ||||||
|  |  | ||||||
|  | [ "_$OUTPUT" = "_$EXPECTED_OUTPUT" ] \ | ||||||
|  |   || die "'nvm use node --silent' output was not silenced to '$EXPECTED_OUTPUT'; got '$OUTPUT'" | ||||||
		Reference in New Issue
	
	Block a user