[Refactor] extract “get default packages” logic to `nvm_get_default_packages`
parent
02997b0753
commit
5c117e6ab3
|
@ -15,7 +15,7 @@ npm-debug.log
|
|||
|
||||
.DS_Store
|
||||
current
|
||||
default-packages
|
||||
/default-packages
|
||||
|
||||
# Only apps should have lockfiles
|
||||
npm-shrinkwrap.json
|
||||
|
|
60
nvm.sh
60
nvm.sh
|
@ -2631,28 +2631,12 @@ nvm() {
|
|||
shift
|
||||
done
|
||||
|
||||
if [ -z "${SKIP_DEFAULT_PACKAGES-}" ] && [ -f "${NVM_DIR}/default-packages" ]; then
|
||||
DEFAULT_PACKAGES=""
|
||||
|
||||
# Read lines from $NVM_DIR/default-packages
|
||||
local line
|
||||
while IFS=" " read -r line; do
|
||||
# Skip empty lines.
|
||||
[ -n "${line}" ] || continue
|
||||
|
||||
# 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 < "${NVM_DIR}/default-packages"
|
||||
if [ -z "${SKIP_DEFAULT_PACKAGES-}" ]; then
|
||||
DEFAULT_PACKAGES="$(nvm_get_default_packages)"
|
||||
EXIT_CODE=$?
|
||||
if [ $EXIT_CODE -ne 0 ]; then
|
||||
return $EXIT_CODE
|
||||
fi
|
||||
fi
|
||||
|
||||
if [ -n "${PROVIDED_REINSTALL_PACKAGES_FROM-}" ] && [ "$(nvm_ensure_version_prefix "${PROVIDED_REINSTALL_PACKAGES_FROM}")" = "${VERSION}" ]; then
|
||||
|
@ -3486,7 +3470,8 @@ nvm() {
|
|||
nvm_version_greater nvm_version_greater_than_or_equal_to \
|
||||
nvm_print_npm_version nvm_install_latest_npm nvm_npm_global_modules \
|
||||
nvm_has_system_node nvm_has_system_iojs \
|
||||
nvm_download nvm_get_latest nvm_has nvm_install_default_packages nvm_curl_use_compression nvm_curl_version \
|
||||
nvm_download nvm_get_latest nvm_has nvm_install_default_packages nvm_get_default_packages \
|
||||
nvm_curl_use_compression nvm_curl_version \
|
||||
nvm_supports_source_options nvm_auto nvm_supports_xz \
|
||||
nvm_echo nvm_err nvm_grep nvm_cd \
|
||||
nvm_die_on_prefix nvm_get_make_jobs nvm_get_minor_version \
|
||||
|
@ -3509,8 +3494,37 @@ nvm() {
|
|||
esac
|
||||
}
|
||||
|
||||
nvm_get_default_packages() {
|
||||
if [ -f "${NVM_DIR}/default-packages" ]; then
|
||||
local DEFAULT_PACKAGES
|
||||
DEFAULT_PACKAGES=''
|
||||
|
||||
# Read lines from $NVM_DIR/default-packages
|
||||
local line
|
||||
while IFS=' ' read -r line; do
|
||||
# Skip empty lines.
|
||||
[ -n "${line-}" ] || continue
|
||||
|
||||
# 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 < "${NVM_DIR}/default-packages"
|
||||
echo "${DEFAULT_PACKAGES}" | xargs
|
||||
fi
|
||||
}
|
||||
|
||||
nvm_install_default_packages() {
|
||||
nvm_echo "Installing default global packages from ${NVM_DIR}/default-packages..."
|
||||
nvm_echo "npm install -g --quiet $1"
|
||||
|
||||
if ! nvm_echo "$1" | command xargs npm install -g --quiet; then
|
||||
nvm_err "Failed installing default packages. Please check if your default-packages file or a package in it has problems!"
|
||||
|
|
|
@ -0,0 +1,76 @@
|
|||
#!/bin/sh
|
||||
|
||||
FILE="$NVM_DIR/default-packages"
|
||||
\. ../../../nvm.sh
|
||||
|
||||
die () { echo "$@" ; cleanup ; exit 1; }
|
||||
setup () {
|
||||
if [ -f $FILE ]; then
|
||||
ORIG_DEFAULT_PACKAGES=$(cat $FILE)
|
||||
mkdir -p ./tmp/ ||:
|
||||
mv $FILE ./tmp/default-packages ||:
|
||||
fi
|
||||
touch $FILE
|
||||
}
|
||||
cleanup () {
|
||||
if [ "$ORIG_DEFAULT_PACKAGES" != "" ]; then
|
||||
rm -rf ./tmp/
|
||||
echo "$ORIG_DEFAULT_PACKAGES" > $FILE
|
||||
fi
|
||||
}
|
||||
|
||||
setup
|
||||
|
||||
cat > $FILE << EOF
|
||||
rimraf
|
||||
object-inspect@1.0.2
|
||||
|
||||
# commented-package
|
||||
|
||||
stevemao/left-pad
|
||||
EOF
|
||||
|
||||
DEFAULT_PKGS="$(nvm_get_default_packages)"
|
||||
EXPECTED_PKGS='rimraf object-inspect@1.0.2 stevemao/left-pad'
|
||||
[ "${DEFAULT_PKGS}" = "${EXPECTED_PKGS}" ] || die "1: expected default packages >${EXPECTED_PKGS}<; got >${DEFAULT_PKGS}<"
|
||||
|
||||
cleanup
|
||||
|
||||
setup
|
||||
|
||||
cat > $FILE << EOF
|
||||
rimraf
|
||||
not~a~package~name
|
||||
mkdirp
|
||||
EOF
|
||||
|
||||
DEFAULT_PKGS="$(nvm_get_default_packages)"
|
||||
EXPECTED_PKGS='rimraf not~a~package~name mkdirp'
|
||||
[ "${DEFAULT_PKGS}" = "${EXPECTED_PKGS}" ] || die "2: expected default packages >${EXPECTED_PKGS}<; got >${DEFAULT_PKGS}<"
|
||||
|
||||
cleanup
|
||||
|
||||
setup
|
||||
|
||||
cat > $FILE << EOF
|
||||
object-inspect @ 1.0.2
|
||||
rimraf
|
||||
EOF
|
||||
|
||||
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."
|
||||
[ "${DEFAULT_PKGS}" = "${EXPECTED_PKGS}" ] || die "3: expected default packages >${EXPECTED_PKGS}<; got >${DEFAULT_PKGS}<"
|
||||
|
||||
cleanup
|
||||
|
||||
setup
|
||||
|
||||
rm -rf $FILE
|
||||
|
||||
DEFAULT_PKGS="$(nvm_get_default_packages)"
|
||||
EXPECTED_PKGS=''
|
||||
[ "${DEFAULT_PKGS}" = "${EXPECTED_PKGS}" ] || die "4: expected default packages >${EXPECTED_PKGS}<; got >${DEFAULT_PKGS}<"
|
||||
|
||||
touch $FILE
|
||||
|
||||
cleanup
|
|
@ -21,7 +21,7 @@ cleanup () {
|
|||
|
||||
setup
|
||||
|
||||
\. ../../../nvm.sh
|
||||
\. ../../nvm.sh
|
||||
|
||||
cat > $FILE << EOF
|
||||
rimraf
|
||||
|
@ -45,7 +45,7 @@ cleanup
|
|||
|
||||
setup
|
||||
|
||||
\. ../../../nvm.sh
|
||||
\. ../../nvm.sh
|
||||
|
||||
cat > $FILE << EOF
|
||||
rimraf
|
Loading…
Reference in New Issue