mirror of
https://github.com/nvm-sh/nvm.git
synced 2025-05-10 14:21:50 +00:00
[Fix] nvm_get_default_packages
: use awk
for more reliable file processing
See db19450caa
Fixes #3382
This commit is contained in:
parent
f439acda4a
commit
87a709741f
46
nvm.sh
46
nvm.sh
@ -4436,37 +4436,23 @@ nvm() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
nvm_get_default_packages() {
|
nvm_get_default_packages() {
|
||||||
local NVM_DEFAULT_PACKAGE_FILE="${NVM_DIR}/default-packages"
|
local NVM_DEFAULT_PACKAGE_FILE
|
||||||
|
NVM_DEFAULT_PACKAGE_FILE="${NVM_DIR}/default-packages"
|
||||||
if [ -f "${NVM_DEFAULT_PACKAGE_FILE}" ]; then
|
if [ -f "${NVM_DEFAULT_PACKAGE_FILE}" ]; then
|
||||||
local DEFAULT_PACKAGES
|
command awk -v filename="${NVM_DEFAULT_PACKAGE_FILE}" '
|
||||||
DEFAULT_PACKAGES=''
|
/^[[:space:]]*#/ { next } # Skip lines that begin with #
|
||||||
|
/^[[:space:]]*$/ { next } # Skip empty lines
|
||||||
# Read lines from $NVM_DIR/default-packages
|
/[[:space:]]/ && !/^[[:space:]]*#/ {
|
||||||
local line
|
print "Only one package per line is allowed in `" filename "`. Please remove any lines with multiple space-separated values." > "/dev/stderr"
|
||||||
# ensure a trailing newline
|
err = 1
|
||||||
WORK=$(mktemp -d) || exit $?
|
exit 1
|
||||||
# shellcheck disable=SC2064
|
}
|
||||||
trap "command rm -rf '$WORK'" EXIT
|
{
|
||||||
# shellcheck disable=SC1003
|
if (NR > 1 && !prev_space) printf " "
|
||||||
sed -e '$a\' "${NVM_DEFAULT_PACKAGE_FILE}" > "${WORK}/default-packages"
|
printf "%s", $0
|
||||||
while IFS=' ' read -r line; do
|
prev_space = 0
|
||||||
# Skip empty lines.
|
}
|
||||||
[ -n "${line-}" ] || continue
|
' "${NVM_DEFAULT_PACKAGE_FILE}"
|
||||||
|
|
||||||
# Skip comment lines that begin with `#`.
|
|
||||||
[ "$(nvm_echo "${line}" | command cut -c1)" != "#" ] || continue
|
|
||||||
|
|
||||||
# Fail on lines that have multiple space-separated words
|
|
||||||
case $line in
|
|
||||||
*\ *)
|
|
||||||
nvm_err "Only one package per line is allowed in the ${NVM_DIR}/default-packages file. Please remove any lines with multiple space-separated values."
|
|
||||||
return 1
|
|
||||||
;;
|
|
||||||
esac
|
|
||||||
|
|
||||||
DEFAULT_PACKAGES="${DEFAULT_PACKAGES}${line} "
|
|
||||||
done < "${WORK}/default-packages"
|
|
||||||
echo "${DEFAULT_PACKAGES}" | command xargs
|
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -74,7 +74,7 @@ rimraf
|
|||||||
EOF
|
EOF
|
||||||
|
|
||||||
DEFAULT_PKGS="$(nvm_get_default_packages 2>&1 >/dev/null)"
|
DEFAULT_PKGS="$(nvm_get_default_packages 2>&1 >/dev/null)"
|
||||||
EXPECTED_PKGS="Only one package per line is allowed in the $FILE file. Please remove any lines with multiple space-separated values."
|
EXPECTED_PKGS="Only one package per line is allowed in \`${FILE}\`. Please remove any lines with multiple space-separated values."
|
||||||
[ "${DEFAULT_PKGS}" = "${EXPECTED_PKGS}" ] || die "4: expected default packages >${EXPECTED_PKGS}<; got >${DEFAULT_PKGS}<"
|
[ "${DEFAULT_PKGS}" = "${EXPECTED_PKGS}" ] || die "4: expected default packages >${EXPECTED_PKGS}<; got >${DEFAULT_PKGS}<"
|
||||||
|
|
||||||
cleanup
|
cleanup
|
||||||
|
Loading…
Reference in New Issue
Block a user