Compare commits

..

2 Commits

Author SHA1 Message Date
Jordan Harband
8283b91ede
[Refactor] prefer awk over pipes in more places 2025-02-04 15:45:57 -08:00
Jordan Harband
d5e3896466
[Refactor] combine sed -e invocations/arguments 2025-02-04 15:55:10 -08:00

71
nvm.sh
View File

@ -134,15 +134,17 @@ nvm_download() {
eval "curl -q --fail ${CURL_COMPRESSED_FLAG:-} ${CURL_HEADER_FLAG:-} ${NVM_DOWNLOAD_ARGS}"
elif nvm_has "wget"; then
# Emulate curl with wget
ARGS=$(nvm_echo "$@" | command sed -e 's/--progress-bar /--progress=bar /' \
-e 's/--compressed //' \
-e 's/--fail //' \
-e 's/-L //' \
-e 's/-I /--server-response /' \
-e 's/-s /-q /' \
-e 's/-sS /-nv /' \
-e 's/-o /-O /' \
-e 's/-C - /-c /')
ARGS=$(nvm_echo "$@" | command sed "
s/--progress-bar /--progress=bar /
s/--compressed //
s/--fail //
s/-L //
s/-I /--server-response /
s/-s /-q /
s/-sS /-nv /
s/-o /-O /
s/-C - /-c /
")
if [ -n "${NVM_AUTH_HEADER:-}" ]; then
ARGS="${ARGS} --header \"${NVM_AUTH_HEADER}\""
@ -1533,32 +1535,19 @@ nvm_ls() {
fi
if [ -n "${NVM_DIRS_TO_SEARCH1}${NVM_DIRS_TO_SEARCH2}${NVM_DIRS_TO_SEARCH3}" ]; then
VERSIONS="$(command find "${NVM_DIRS_TO_SEARCH1}"/* "${NVM_DIRS_TO_SEARCH2}"/* "${NVM_DIRS_TO_SEARCH3}"/* -name . -o -type d -prune -o -path "${PATTERN}*" \
| command awk \
-v dir_iojs="${NVM_VERSION_DIR_IOJS}" \
-v iojs_prefix="versions/${NVM_IOJS_PREFIX}/" \
-v dir="${NVM_DIR}" \
-v search="${SEARCH_PATTERN}" \
-v node_prefix="${NVM_NODE_PREFIX}" '
{
sub(dir_iojs"/", iojs_prefix)
sub("^" dir "/", "")
if ($0 ~ /^[^v]/) next
if ($0 == "versions") next
sub(/^versions\//, "")
sub(/^v/, node_prefix"/v")
if ($0 !~ search) next
sub(/^([^/]+)\/(.*)$/, "\\2.\\1")
print
}
' \
| command sed -e "
s#${NVM_VERSION_DIR_IOJS}/#versions/${NVM_IOJS_PREFIX}/#;
s#^${NVM_DIR}/##;
\\#^[^v]# d;
\\#^versions\$# d;
s#^versions/##;
s#^v#${NVM_NODE_PREFIX}/v#;
\\#${SEARCH_PATTERN}# !d;
" \
-e 's#^\([^/]\{1,\}\)/\(.*\)$#\2.\1#;' \
| command sort -t. -u -k 1.2,1n -k 2,2n -k 3,3n \
| command awk -v node_prefix="${NVM_NODE_PREFIX}" '
{
sub(/^(.*)\.([^.]*)$/, "\\2-\\1")
sub("^" node_prefix"-", "")
print
}
'
| command sed -e 's#\(.*\)\.\([^\.]\{1,\}\)$#\2-\1#;' \
-e "s#^${NVM_NODE_PREFIX}-##;" \
)"
fi
fi
@ -2051,7 +2040,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
@ -2768,13 +2757,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 1,1d | nvm_grep -v 'UNMET PEER DEPENDENCY')
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}"
}