[Refactor] extract “get default packages” logic to `nvm_get_default_packages`

Jordan Harband 2019-02-01 13:27:18 -08:00
parent 02997b0753
commit 5c117e6ab3
No known key found for this signature in database
GPG Key ID: 64A196AEE0916D55
4 changed files with 116 additions and 26 deletions

2
.gitignore vendored
View File

@ -15,7 +15,7 @@ npm-debug.log
.DS_Store .DS_Store
current current
default-packages /default-packages
# Only apps should have lockfiles # Only apps should have lockfiles
npm-shrinkwrap.json npm-shrinkwrap.json

60
nvm.sh
View File

@ -2631,28 +2631,12 @@ nvm() {
shift shift
done done
if [ -z "${SKIP_DEFAULT_PACKAGES-}" ] && [ -f "${NVM_DIR}/default-packages" ]; then if [ -z "${SKIP_DEFAULT_PACKAGES-}" ]; then
DEFAULT_PACKAGES="" DEFAULT_PACKAGES="$(nvm_get_default_packages)"
EXIT_CODE=$?
# Read lines from $NVM_DIR/default-packages if [ $EXIT_CODE -ne 0 ]; then
local line return $EXIT_CODE
while IFS=" " read -r line; do fi
# 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"
fi fi
if [ -n "${PROVIDED_REINSTALL_PACKAGES_FROM-}" ] && [ "$(nvm_ensure_version_prefix "${PROVIDED_REINSTALL_PACKAGES_FROM}")" = "${VERSION}" ]; then 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_version_greater nvm_version_greater_than_or_equal_to \
nvm_print_npm_version nvm_install_latest_npm nvm_npm_global_modules \ nvm_print_npm_version nvm_install_latest_npm nvm_npm_global_modules \
nvm_has_system_node nvm_has_system_iojs \ 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_supports_source_options nvm_auto nvm_supports_xz \
nvm_echo nvm_err nvm_grep nvm_cd \ nvm_echo nvm_err nvm_grep nvm_cd \
nvm_die_on_prefix nvm_get_make_jobs nvm_get_minor_version \ nvm_die_on_prefix nvm_get_make_jobs nvm_get_minor_version \
@ -3509,8 +3494,37 @@ nvm() {
esac 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_install_default_packages() {
nvm_echo "Installing default global packages from ${NVM_DIR}/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 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!" nvm_err "Failed installing default packages. Please check if your default-packages file or a package in it has problems!"

View File

@ -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

View File

@ -21,7 +21,7 @@ cleanup () {
setup setup
\. ../../../nvm.sh \. ../../nvm.sh
cat > $FILE << EOF cat > $FILE << EOF
rimraf rimraf
@ -45,7 +45,7 @@ cleanup
setup setup
\. ../../../nvm.sh \. ../../nvm.sh
cat > $FILE << EOF cat > $FILE << EOF
rimraf rimraf