From 1e277a930ae8d41a7792ccce0dbfd51e1b51a806 Mon Sep 17 00:00:00 2001 From: Jordan Harband Date: Sun, 28 Jul 2024 19:07:30 -0700 Subject: [PATCH] [Fix] do not colorize output in a pipe, unless `--colors` is passed Fixes 2497 --- nvm.sh | 72 +++++++++++++++++-- .../Using --colors and --no-colors errors | 12 ++++ .../`--no-colors` is implied in a pipe | 25 +++++++ 3 files changed, 103 insertions(+), 6 deletions(-) create mode 100755 test/fast/Set Colors/Using --colors and --no-colors errors create mode 100755 test/fast/Set Colors/`--no-colors` is implied in a pipe diff --git a/nvm.sh b/nvm.sh index 64c15cdc49..530f473413 100755 --- a/nvm.sh +++ b/nvm.sh @@ -2994,14 +2994,29 @@ nvm() { case $i in --) break ;; '-h'|'help'|'--help') - NVM_NO_COLORS="" - for j in "$@"; do - if [ "${j}" = '--no-colors' ]; then - NVM_NO_COLORS="${j}" - break - fi + local NVM_NO_COLORS + NVM_NO_COLORS='' + local NVM_YES_COLORS + NVM_YES_COLORS=0 + + while [ $# -ne 0 ]; do + case "${1}" in + --colors) NVM_YES_COLORS=1 ;; + --no-colors) NVM_NO_COLORS='--no-colors' ;; + --) ;; + esac + shift done + if [ $NVM_YES_COLORS -eq 1 ]; then + if [ "${NVM_NO_COLORS-}" = '--no-colors' ]; then + nvm_err '--colors and --no-colors are mutually exclusive' + return 55 + fi + elif ! nvm_stdout_is_terminal; then + NVM_NO_COLORS='--no-colors' + fi + local NVM_IOJS_PREFIX NVM_IOJS_PREFIX="$(nvm_iojs_prefix)" local NVM_NODE_PREFIX @@ -3598,6 +3613,7 @@ nvm() { return $EXIT_CODE ;; + "uninstall") if [ $# -ne 1 ]; then >&2 nvm --help @@ -3678,6 +3694,7 @@ nvm() { nvm unalias "$(command basename "${ALIAS}")" done ;; + "deactivate") local NVM_SILENT while [ $# -ne 0 ]; do @@ -3727,6 +3744,7 @@ nvm() { unset NVM_BIN unset NVM_INC ;; + "use") local PROVIDED_VERSION local NVM_SILENT @@ -3864,6 +3882,7 @@ nvm() { nvm_echo "${NVM_USE_OUTPUT}" fi ;; + "run") local provided_version local has_checked_nvmrc @@ -3948,6 +3967,7 @@ nvm() { EXIT_CODE="$?" return $EXIT_CODE ;; + "exec") local NVM_SILENT local NVM_LTS @@ -4008,14 +4028,18 @@ nvm() { fi NODE_VERSION="${VERSION}" "${NVM_DIR}/nvm-exec" "$@" ;; + "ls" | "list") local PATTERN local NVM_NO_COLORS + local NVM_YES_COLORS + NVM_YES_COLORS=0 local NVM_NO_ALIAS while [ $# -gt 0 ]; do case "${1}" in --) ;; + --colors) NVM_YES_COLORS=1 ;; --no-colors) NVM_NO_COLORS="${1}" ;; --no-alias) NVM_NO_ALIAS="${1}" ;; --*) @@ -4028,6 +4052,16 @@ nvm() { esac shift done + + if [ $NVM_YES_COLORS -eq 1 ]; then + if [ "${NVM_NO_COLORS-}" = '--no-colors' ]; then + nvm_err '--colors and --no-colors are mutually exclusive' + return 55 + fi + elif ! nvm_stdout_is_terminal; then + NVM_NO_COLORS='--no-colors' + fi + if [ -n "${PATTERN-}" ] && [ -n "${NVM_NO_ALIAS-}" ]; then nvm_err '`--no-alias` is not supported when a pattern is provided.' return 55 @@ -4046,10 +4080,13 @@ nvm() { fi return $NVM_LS_EXIT_CODE ;; + "ls-remote" | "list-remote") local NVM_LTS local PATTERN local NVM_NO_COLORS + local NVM_YES_COLORS + NVM_YES_COLORS=0 while [ $# -gt 0 ]; do case "${1-}" in @@ -4060,6 +4097,7 @@ nvm() { --lts=*) NVM_LTS="${1##--lts=}" ;; + --colors) NVM_YES_COLORS=1 ;; --no-colors) NVM_NO_COLORS="${1}" ;; --*) nvm_err "Unsupported option \"${1}\"." @@ -4086,6 +4124,15 @@ nvm() { shift done + if [ $NVM_YES_COLORS -eq 1 ]; then + if [ "${NVM_NO_COLORS-}" = '--no-colors' ]; then + nvm_err '--colors and --no-colors are mutually exclusive' + return 55 + fi + elif ! nvm_stdout_is_terminal; then + NVM_NO_COLORS='--no-colors' + fi + local NVM_OUTPUT local EXIT_CODE NVM_OUTPUT="$(NVM_LTS="${NVM_LTS-}" nvm_remote_versions "${PATTERN}" &&:)" @@ -4166,12 +4213,15 @@ nvm() { local ALIAS local TARGET local NVM_NO_COLORS + local NVM_YES_COLORS + NVM_YES_COLORS=0 ALIAS='--' TARGET='--' while [ $# -gt 0 ]; do case "${1-}" in --) ;; + --colors) NVM_YES_COLORS=1 ;; --no-colors) NVM_NO_COLORS="${1}" ;; --*) nvm_err "Unsupported option \"${1}\"." @@ -4188,6 +4238,15 @@ nvm() { shift done + if [ $NVM_YES_COLORS -eq 1 ]; then + if [ "${NVM_NO_COLORS-}" = '--no-colors' ]; then + nvm_err '--colors and --no-colors are mutually exclusive' + return 55 + fi + elif ! nvm_stdout_is_terminal; then + NVM_NO_COLORS='--no-colors' + fi + if [ -z "${TARGET}" ]; then # for some reason the empty string was explicitly passed as the target # so, unalias it. @@ -4216,6 +4275,7 @@ nvm() { nvm_list_aliases "${ALIAS-}" fi ;; + "unalias") local NVM_ALIAS_DIR NVM_ALIAS_DIR="$(nvm_alias_path)" diff --git a/test/fast/Set Colors/Using --colors and --no-colors errors b/test/fast/Set Colors/Using --colors and --no-colors errors new file mode 100755 index 0000000000..0b0e76b3f1 --- /dev/null +++ b/test/fast/Set Colors/Using --colors and --no-colors errors @@ -0,0 +1,12 @@ +#!/bin/sh + +set -ex + +die () { echo "$@" ; cleanup ; exit 1; } + +\. ../../nvm.sh + +nvm ls --colors --no-colors && die "\`nvm ls --colors --no-colors\` did not fail" +nvm ls-remote --colors --no-colors && die "\`nvm ls-remote --colors --no-colors\` did not fail" +nvm alias --colors --no-colors && die "\`nvm alias --colors --no-colors\` did not fail" +nvm --help --colors --no-colors && die "\`nvm --help --colors --no-colors\` did not fail" diff --git a/test/fast/Set Colors/`--no-colors` is implied in a pipe b/test/fast/Set Colors/`--no-colors` is implied in a pipe new file mode 100755 index 0000000000..061b0cae30 --- /dev/null +++ b/test/fast/Set Colors/`--no-colors` is implied in a pipe @@ -0,0 +1,25 @@ +#!/bin/sh + +set -ex + +die () { echo "$@" ; cleanup ; exit 1; } + +\. ../../nvm.sh + +# ensures that commands are run as if in a terminal +term_cmd() { + script -q -a /dev/null sh -c "\. ../../nvm.sh ; $*" +} + +for cmd in ls ls-remote alias --help; do + NO_COLORS="$(term_cmd nvm "${cmd}" --no-colors)" + COLORS="$(term_cmd nvm "${cmd}" --colors)" + + [ "${COLORS}" != "${NO_COLORS}" ] || die "Expected \`nvm ${cmd} --colors\` to be different from \`nvm ${cmd} --no-colors\`" + + PIPE="$(nvm "${cmd}")" + PIPE_COLORS="$(nvm "${cmd}" --colors)" + + [ "${PIPE}" = "${NO_COLORS}" ] || die "Expected \`nvm ${cmd}\` in a pipe to be the same as \`nvm ${cmd} --no-colors\`" + [ "${PIPE_COLORS}" = "${COLORS}" ] || die "Expected \`nvm ${cmd} --colors\` in a pipe to be the same as \`nvm ls\` in a terminal (with colors)" +done