Compare commits

...

4 Commits

Author SHA1 Message Date
Jordan Harband
e2e72bab5b
second 2025-02-04 14:03:45 -08:00
Jordan Harband
0560eed9ff nvmrc 2025-02-04 14:03:42 -08:00
Jordan Harband
ad7c98414a libz 2025-02-04 14:03:35 -08:00
Jordan Harband
45ecc7a03e first awk refactor 2025-02-04 14:03:27 -08:00

78
nvm.sh
View File

@ -63,13 +63,11 @@ nvm_command_info() {
local INFO local INFO
COMMAND="${1}" COMMAND="${1}"
if type "${COMMAND}" | nvm_grep -q hashed; then 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 elif type "${COMMAND}" | nvm_grep -q aliased; then
# shellcheck disable=SC2230 INFO="$(which "${COMMAND}") ($(type "${COMMAND}" | command awk "{ \$1=\$2=\$3=\$4=\"\"; sub(/^ */, \"\"); gsub(/[\\\`']/, \"\"); print}"))"
INFO="$(which "${COMMAND}") ($(type "${COMMAND}" | command awk '{ $1=$2=$3=$4="" ;print }' | command sed -e 's/^\ *//g' -Ee "s/\`|'//g"))"
elif type "${COMMAND}" | nvm_grep -q "^${COMMAND} is an alias for"; then 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=\"\"; sub(/^ */, \"\"); print }"))"
INFO="$(which "${COMMAND}") ($(type "${COMMAND}" | command awk '{ $1=$2=$3=$4=$5="" ;print }' | command sed 's/^\ *//g'))"
elif type "${COMMAND}" | nvm_grep -q "^${COMMAND} is /"; then elif type "${COMMAND}" | nvm_grep -q "^${COMMAND} is /"; then
INFO="$(type "${COMMAND}" | command awk '{print $3}')" INFO="$(type "${COMMAND}" | command awk '{print $3}')"
else else
@ -87,7 +85,7 @@ nvm_has_colors() {
} }
nvm_curl_libz_support() { 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() { nvm_curl_use_compression() {
@ -532,9 +530,13 @@ $(nvm_wrap_with_color_code 'y' "${warn_text}")"
nvm_process_nvmrc() { nvm_process_nvmrc() {
local NVMRC_PATH local NVMRC_PATH
NVMRC_PATH="$1" 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 '{
sub(/#.*/, "")
gsub(/^[[:space:]]+|[[:space:]]+$/, "")
if (length($0) > 0) print
}' "$NVMRC_PATH")
if [ -z "$lines" ]; then if [ -z "$lines" ]; then
nvm_nvmrc_invalid_msg "${lines}" nvm_nvmrc_invalid_msg "${lines}"
@ -1531,19 +1533,32 @@ nvm_ls() {
fi fi
if [ -n "${NVM_DIRS_TO_SEARCH1}${NVM_DIRS_TO_SEARCH2}${NVM_DIRS_TO_SEARCH3}" ]; then 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}*" \ VERSIONS="$(command find "${NVM_DIRS_TO_SEARCH1}"/* "${NVM_DIRS_TO_SEARCH2}"/* "${NVM_DIRS_TO_SEARCH3}"/* -name . -o -type d -prune -o -path "${PATTERN}*" \
| command sed -e " | command awk \
s#${NVM_VERSION_DIR_IOJS}/#versions/${NVM_IOJS_PREFIX}/#; -v dir_iojs="${NVM_VERSION_DIR_IOJS}" \
s#^${NVM_DIR}/##; -v iojs_prefix="versions/${NVM_IOJS_PREFIX}/" \
\\#^[^v]# d; -v dir="${NVM_DIR}" \
\\#^versions\$# d; -v search="${SEARCH_PATTERN}" \
s#^versions/##; -v node_prefix="${NVM_NODE_PREFIX}" '
s#^v#${NVM_NODE_PREFIX}/v#; {
\\#${SEARCH_PATTERN}# !d; sub(dir_iojs"/", iojs_prefix)
" \ sub("^" dir "/", "")
-e 's#^\([^/]\{1,\}\)/\(.*\)$#\2.\1#;' \ if ($0 ~ /^[^v]/) next
if ($0 == "versions") next
sub(/^versions\//, "")
sub(/^v/, node_prefix"/v")
if ($0 !~ search) next
sub(/^([^/]+)\/(.*)$/, "\\2.\\1")
print
}
' \
| command sort -t. -u -k 1.2,1n -k 2,2n -k 3,3n \ | command sort -t. -u -k 1.2,1n -k 2,2n -k 3,3n \
| command sed -e 's#\(.*\)\.\([^\.]\{1,\}\)$#\2-\1#;' \ | command awk -v node_prefix="${NVM_NODE_PREFIX}" '
-e "s#^${NVM_NODE_PREFIX}-##;" \ {
sub(/^(.*)\.([^.]*)$/, "\\2-\\1")
sub("^" node_prefix"-", "")
print
}
'
)" )"
fi fi
fi fi
@ -1993,7 +2008,28 @@ nvm_print_implicit_alias() {
NVM_IOJS_VERSION="$(${NVM_COMMAND})" &&: NVM_IOJS_VERSION="$(${NVM_COMMAND})" &&:
EXIT_CODE="$?" EXIT_CODE="$?"
if [ "_${EXIT_CODE}" = "_0" ]; then 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, "")
# skip lines that do not start with "v"
if ($0 !~ /^v/) next
# remove leading "v"
sub(/^v/, "")
# keep only major.minor
split($0, parts, "\\.")
short = parts[1] "." parts[2]
# replicate "uniq" by only updating if changed
if (short != prev) {
last = short
prev = short
}
}
END { print last }
')"
fi fi
if [ "_$NVM_IOJS_VERSION" = "_N/A" ]; then if [ "_$NVM_IOJS_VERSION" = "_N/A" ]; then