From 5c117e6ab329cb1c5dc1c6fa932e7a590919a26f Mon Sep 17 00:00:00 2001 From: Jordan Harband Date: Fri, 1 Feb 2019 13:27:18 -0800 Subject: [PATCH] =?UTF-8?q?[Refactor]=20extract=20=E2=80=9Cget=20default?= =?UTF-8?q?=20packages=E2=80=9D=20logic=20to=20`nvm=5Fget=5Fdefault=5Fpack?= =?UTF-8?q?ages`?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 2 +- nvm.sh | 60 +++++++++------ test/fast/Unit tests/nvm_get_default_packages | 76 +++++++++++++++++++ .../default-packages} | 4 +- 4 files changed, 116 insertions(+), 26 deletions(-) create mode 100755 test/fast/Unit tests/nvm_get_default_packages rename test/{fast/Unit tests/nvm_default_packages => installation_node/default-packages} (98%) diff --git a/.gitignore b/.gitignore index 6d7d6d6ebd..fcf59f8039 100644 --- a/.gitignore +++ b/.gitignore @@ -15,7 +15,7 @@ npm-debug.log .DS_Store current -default-packages +/default-packages # Only apps should have lockfiles npm-shrinkwrap.json diff --git a/nvm.sh b/nvm.sh index f2744c6779..bf9a1a31e8 100644 --- a/nvm.sh +++ b/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!" diff --git a/test/fast/Unit tests/nvm_get_default_packages b/test/fast/Unit tests/nvm_get_default_packages new file mode 100755 index 0000000000..115bdf6d7c --- /dev/null +++ b/test/fast/Unit tests/nvm_get_default_packages @@ -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 diff --git a/test/fast/Unit tests/nvm_default_packages b/test/installation_node/default-packages similarity index 98% rename from test/fast/Unit tests/nvm_default_packages rename to test/installation_node/default-packages index 0ae318d156..ad08aa53fc 100755 --- a/test/fast/Unit tests/nvm_default_packages +++ b/test/installation_node/default-packages @@ -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