Skip to content

Commit

Permalink
[Fix] do not colorize output in a pipe, unless --colors is passed
Browse files Browse the repository at this point in the history
Fixes 2497
  • Loading branch information
ljharb committed Jul 29, 2024
1 parent ff1257e commit 8fdcc43
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 8 deletions.
72 changes: 66 additions & 6 deletions nvm.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -3598,6 +3613,7 @@ nvm() {

return $EXIT_CODE
;;

"uninstall")
if [ $# -ne 1 ]; then
>&2 nvm --help
Expand Down Expand Up @@ -3678,6 +3694,7 @@ nvm() {
nvm unalias "$(command basename "${ALIAS}")"
done
;;

"deactivate")
local NVM_SILENT
while [ $# -ne 0 ]; do
Expand Down Expand Up @@ -3727,6 +3744,7 @@ nvm() {
unset NVM_BIN
unset NVM_INC
;;

"use")
local PROVIDED_VERSION
local NVM_SILENT
Expand Down Expand Up @@ -3864,6 +3882,7 @@ nvm() {
nvm_echo "${NVM_USE_OUTPUT}"
fi
;;

"run")
local provided_version
local has_checked_nvmrc
Expand Down Expand Up @@ -3948,6 +3967,7 @@ nvm() {
EXIT_CODE="$?"
return $EXIT_CODE
;;

"exec")
local NVM_SILENT
local NVM_LTS
Expand Down Expand Up @@ -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}" ;;
--*)
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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}\"."
Expand All @@ -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}" &&:)"
Expand Down Expand Up @@ -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}\"."
Expand All @@ -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.
Expand Down Expand Up @@ -4216,6 +4275,7 @@ nvm() {
nvm_list_aliases "${ALIAS-}"
fi
;;

"unalias")
local NVM_ALIAS_DIR
NVM_ALIAS_DIR="$(nvm_alias_path)"
Expand Down
5 changes: 5 additions & 0 deletions test/common.sh
Original file line number Diff line number Diff line change
Expand Up @@ -245,3 +245,8 @@ _json_parse() {
nvm_json_extract() {
nvm_json_tokenize | _json_parse | grep -e "${1}" | awk '{print $2 $3}'
}

# ensures that commands are run as if in a terminal
run_in_terminal() {
script -q -a /dev/null sh -c "\. ../../nvm.sh ; $*"
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ fi

nvm alias test-stable-1 0.0.2 || die '`nvm alias test-stable-1 0.0.2` failed'

OUTPUT="$(nvm alias test-stable-1 | strip_colors)"
OUTPUT="$(run_in_terminal 'nvm alias test-stable-1' | strip_colors)"
EXPECTED_OUTPUT='test-stable-1 -> 0.0.2 (-> v0.0.2)'
echo "$OUTPUT" | \grep -F "$EXPECTED_OUTPUT" || die "nvm alias test-stable-1 0.0.2 did not set test-stable-1 to 0.0.2: got '$OUTPUT'"

nvm alias test-stable-1 0.0.1 || die '`nvm alias test-stable-1 0.0.1` failed'

OUTPUT="$(nvm alias test-stable-1 | strip_colors)"
OUTPUT="$(run_in_terminal 'nvm alias test-stable-1' | strip_colors)"
EXPECTED_OUTPUT='test-stable-1 -> 0.0.1 (-> v0.0.1)'
echo "$OUTPUT" | \grep -F "$EXPECTED_OUTPUT" || die "nvm alias test-stable-1 0.0.1 did not set test-stable-1 to 0.0.1: got '$OUTPUT'"
12 changes: 12 additions & 0 deletions test/fast/Set Colors/Using --colors and --no-colors errors
Original file line number Diff line number Diff line change
@@ -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"
21 changes: 21 additions & 0 deletions test/fast/Set Colors/`--no-colors` is implied in a pipe
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#!/bin/sh

set -ex

die () { echo "$@" ; cleanup ; exit 1; }

\. ../../../nvm.sh
\. ../../common.sh

for cmd in ls ls-remote alias --help; do
NO_COLORS="$(run_in_terminal nvm "${cmd}" --no-colors)"
COLORS="$(run_in_terminal 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

0 comments on commit 8fdcc43

Please sign in to comment.