Skip to content

Commit

Permalink
[New] Supercharge nvm debug output
Browse files Browse the repository at this point in the history
Try to get shell version, OS and its version, curl/wget/git version.
  • Loading branch information
PeterDaveHello authored and ljharb committed Mar 23, 2017
1 parent f344d06 commit c7e6b22
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 1 deletion.
42 changes: 41 additions & 1 deletion nvm.sh
Expand Up @@ -49,6 +49,24 @@ nvm_is_alias() {
\alias "${1-}" > /dev/null 2>&1
}

nvm_command_info() {
local COMMAND
local INFO
COMMAND="${1}"
if type "${COMMAND}" | command grep -q hashed; then
INFO="$(type "${COMMAND}" | command sed -E 's/\(|)//g' | command awk '{print $4}')"
elif type "${COMMAND}" | command grep -q aliased; then
INFO="$(which "${COMMAND}") ($(type "${COMMAND}" | command awk '{ $1=$2=$3=$4="" ;print }' | command sed -e 's/^\ *//g' -Ee "s/\`|'//g" ))"
elif type "${COMMAND}" | command grep -q "^${COMMAND} is an alias for"; then
INFO="$(which "${COMMAND}") ($(type "${COMMAND}" | command awk '{ $1=$2=$3=$4=$5="" ;print }' | command sed 's/^\ *//g'))"
elif type "${COMMAND}" | command grep -q "^${COMMAND} is \/"; then
INFO="$(type "${COMMAND}" | command awk '{print $3}')"
else
INFO="$(type "${COMMAND}")"
fi
nvm_echo "${INFO}"
}

nvm_has_colors() {
local NVM_COLORS
if nvm_has tput; then
Expand Down Expand Up @@ -2249,6 +2267,28 @@ nvm() {
nvm_err "\$NPM_CONFIG_PREFIX: '$(nvm_sanitize_path "$NPM_CONFIG_PREFIX")'"
nvm_err "\$NVM_NODEJS_ORG_MIRROR: '${NVM_NODEJS_ORG_MIRROR}'"
nvm_err "\$NVM_IOJS_ORG_MIRROR: '${NVM_IOJS_ORG_MIRROR}'"
nvm_err "shell version: '$(${SHELL} --version | command head -n 1)'"
nvm_err "uname -a: '$(uname -a | awk '{$2=""; print}' | xargs)'"
if [ "$(nvm_get_os)" = "darwin" ] && nvm_has sw_vers; then
nvm_err "OS version: $(sw_vers | command awk '{print $2}' | command xargs)"
elif [ -r "/etc/issue" ]; then
nvm_err "OS version: $(command head -n 1 /etc/issue | command sed 's/\\.//g')"
fi
if nvm_has "curl"; then
nvm_err "curl: $(nvm_command_info curl), $(command curl -V | command head -n 1)"
else
nvm_err "curl: not found"
fi
if nvm_has "wget"; then
nvm_err "wget: $(nvm_command_info wget), $(command wget -V | command head -n 1)"
else
nvm_err "wget: not found"
fi
if nvm_has "git"; then
nvm_err "git: $(nvm_command_info git), $(command git --version)"
else
nvm_err "git: not found"
fi
local NVM_DEBUG_OUTPUT
for NVM_DEBUG_COMMAND in 'nvm current' 'which node' 'which iojs' 'which npm' 'npm config get prefix' 'npm root -g'
do
Expand Down Expand Up @@ -3204,7 +3244,7 @@ nvm() {
nvm_print_default_alias nvm_print_formatted_alias nvm_resolve_local_alias \
nvm_sanitize_path nvm_has_colors nvm_process_parameters \
node_version_has_solaris_binary iojs_version_has_solaris_binary \
nvm_curl_libz_support \
nvm_curl_libz_support nvm_command_info \
> /dev/null 2>&1
unset RC_VERSION NVM_NODEJS_ORG_MIRROR NVM_IOJS_ORG_MIRROR NVM_DIR \
NVM_CD_FLAGS NVM_BIN NVM_MAKE_JOBS \
Expand Down
40 changes: 40 additions & 0 deletions test/fast/Unit tests/nvm_command_info
@@ -0,0 +1,40 @@
#!/bin/sh

cleanup() {
unalias wget > /dev/null 2>&1
unset -f wget > /dev/null 2>&1
unset WGET_EXPECTED_INFO WGET_COMMAND_INFO
}

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

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


# 1. test wget command
WGET_COMMAND_INFO="$(nvm_command_info wget)"
WGET_EXPECTED_INFO="$(which wget)"
[ "${WGET_COMMAND_INFO}" = "${WGET_EXPECTED_INFO}" ] || die "wget command info wrong(stage 1), expected: '${WGET_EXPECTED_INFO}', got '${WGET_COMMAND_INFO}'"

cleanup

# 2. test aliased wget
shopt -s expand_aliases
# enable expand_aliases to make alias working in interactive shell
alias wget="wget -V"
WGET_COMMAND_INFO="$(nvm_command_info wget)"
WGET_EXPECTED_INFO="$(which wget) (wget -V)"
[ "${WGET_COMMAND_INFO}" = "${WGET_EXPECTED_INFO}" ] || die "wget command info wrong(stage 2), expected: '${WGET_EXPECTED_INFO}', got '${WGET_COMMAND_INFO}'"

cleanup

# 3. test wget function
wget() {
echo "wget function"
}

WGET_COMMAND_INFO="$(nvm_command_info wget)"
WGET_EXPECTED_INFO="$(type wget)"
[ "${WGET_COMMAND_INFO}" = "${WGET_EXPECTED_INFO}" ] || die "wget command info wrong(stage 3), expected: '${WGET_EXPECTED_INFO}', got '${WGET_COMMAND_INFO}'"

cleanup

0 comments on commit c7e6b22

Please sign in to comment.