Skip to content

Commit

Permalink
[Refactor] prefer awk over pipes in more places
Browse files Browse the repository at this point in the history
  • Loading branch information
ljharb committed Feb 5, 2025
1 parent dc6b251 commit ec08acf
Showing 1 changed file with 39 additions and 13 deletions.
52 changes: 39 additions & 13 deletions nvm.sh
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,11 @@ nvm_command_info() {
local INFO
COMMAND="${1}"
if type "${COMMAND}" | nvm_grep -q hashed; then
INFO="$(type "${COMMAND}" | command sed -E 's/\(|\)//g' | command awk '{print $4}')"
INFO="$(type "${COMMAND}" | command awk '{ gsub(/[()]/,""); print $4 }')"
elif type "${COMMAND}" | nvm_grep -q aliased; then
# shellcheck disable=SC2230
INFO="$(which "${COMMAND}") ($(type "${COMMAND}" | command awk '{ $1=$2=$3=$4="" ;print }' | command sed -e 's/^\ *//g' -Ee "s/\`|'//g"))"
INFO="$(which "${COMMAND}") ($(type "${COMMAND}" | command awk "{ \$1=\$2=\$3=\$4=\"\"; sub(/^ */, \"\"); gsub(/[\`']/,\" \"); print}"))"
elif type "${COMMAND}" | nvm_grep -q "^${COMMAND} is an alias for"; then
# shellcheck disable=SC2230
INFO="$(which "${COMMAND}") ($(type "${COMMAND}" | command awk '{ $1=$2=$3=$4=$5="" ;print }' | command sed 's/^\ *//g'))"
INFO="$(which "${COMMAND}") ($(type "${COMMAND}" | command awk "{ \$1=\$2=\$3=\$4=\$5=\"\"; sub(/^ */, \"\"); print }"))"
elif type "${COMMAND}" | nvm_grep -q "^${COMMAND} is /"; then
INFO="$(type "${COMMAND}" | command awk '{print $3}')"
else
Expand All @@ -87,7 +85,7 @@ nvm_has_colors() {
}

nvm_curl_libz_support() {
curl -V 2>/dev/null | nvm_grep "^Features:" | nvm_grep -q "libz"
curl -V 2>/dev/null | command awk '/^Features:.*libz/ { found=1; exit } END { exit (found ? 0 : 1) }'
}

nvm_curl_use_compression() {
Expand Down Expand Up @@ -534,9 +532,14 @@ $(nvm_wrap_with_color_code 'y' "${warn_text}")"
nvm_process_nvmrc() {
local NVMRC_PATH
NVMRC_PATH="$1"
local lines

lines=$(command sed 's/#.*//' "$NVMRC_PATH" | command sed 's/^[[:space:]]*//;s/[[:space:]]*$//' | nvm_grep -v '^$')
local lines
lines=$(command awk 'BEGIN { FS=""; OFS="" } {
sub(/\r$/, "")
sub(/#.*/, "")
gsub(/^[[:space:]]+|[[:space:]]+$/, "")
if (length($0) > 0) print
}' "$NVMRC_PATH")

if [ -z "$lines" ]; then
nvm_nvmrc_invalid_msg "${lines}"
Expand Down Expand Up @@ -1995,7 +1998,24 @@ nvm_print_implicit_alias() {
NVM_IOJS_VERSION="$(${NVM_COMMAND})" &&:
EXIT_CODE="$?"
if [ "_${EXIT_CODE}" = "_0" ]; then
NVM_IOJS_VERSION="$(nvm_echo "${NVM_IOJS_VERSION}" | command sed "s/^${NVM_IMPLICIT}-//" | nvm_grep -e '^v' | command cut -c2- | command cut -d . -f 1,2 | uniq | command tail -1)"
NVM_IOJS_VERSION="$(nvm_echo "${NVM_IOJS_VERSION}" | command awk -v prefix="^${NVM_IMPLICIT}-" '
BEGIN {
last = ""
prev = ""
}
{
sub(prefix, "")
if ($0 !~ /^v/) next
# remove leading "v"
sub(/^v/, "")
split($0, parts, "\\.") # keep only major.minor
short = parts[1] "." parts[2]
# replicate "uniq" by only updating if changed
last = short
prev = short
}
END { print last }
')"
fi

if [ "_$NVM_IOJS_VERSION" = "_N/A" ]; then
Expand All @@ -2017,7 +2037,7 @@ nvm_print_implicit_alias() {

nvm_is_zsh && setopt local_options shwordsplit

LAST_TWO=$($NVM_COMMAND | nvm_grep -e '^v' | command cut -c2- | command cut -d . -f 1,2 | uniq)
LAST_TWO="$($NVM_COMMAND | command awk '/^v/ { sub(/^v/, ""); split($0, parts, "\\."); short=parts[1]"."parts[2]; if (!seen[short]++) print short }')"
;;
esac
local MINOR
Expand Down Expand Up @@ -2734,13 +2754,19 @@ nvm_npm_global_modules() {
local NPMLIST
local VERSION
VERSION="$1"
NPMLIST=$(nvm use "${VERSION}" >/dev/null && npm list -g --depth=0 2>/dev/null | command sed -e '1d' -e '/UNMET PEER DEPENDENCY/d')
NPMLIST="$(nvm use "${VERSION}" >/dev/null && npm list -g --depth=0 2>/dev/null | command awk 'NR == 1 { next } /UNMET PEER DEPENDENCY/ { next } { print }')"

local INSTALLS
INSTALLS=$(nvm_echo "${NPMLIST}" | command sed -e '/ -> / d' -e '/\(empty\)/ d' -e 's/^.* \(.*@[^ ]*\).*/\1/' -e '/^npm@[^ ]*.*$/ d' | command xargs)
INSTALLS="$(nvm_echo "${NPMLIST}" | command awk '
/ -> / { next }
/\(empty\)/ { next }
/^npm@/ { next }
{ sub(/^.* (.*@[^ ]*).*/,"\\1"); if (length($0) > 0) print }
' | command xargs
)"

local LINKS
LINKS="$(nvm_echo "${NPMLIST}" | command sed -n 's/.* -> \(.*\)/\1/ p')"
LINKS="$(nvm_echo "${NPMLIST}" | command awk '/ -> / { sub(/.* -> /,""); print } ')"

nvm_echo "${INSTALLS} //// ${LINKS}"
}
Expand Down

0 comments on commit ec08acf

Please sign in to comment.