Skip to content

Commit

Permalink
[Refactor] extract “get default packages” logic to `nvm_get_default_p…
Browse files Browse the repository at this point in the history
…ackages`
  • Loading branch information
ljharb committed Feb 1, 2019
1 parent 02997b0 commit 5c117e6
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 26 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ npm-debug.log

.DS_Store
current
default-packages
/default-packages

# Only apps should have lockfiles
npm-shrinkwrap.json
Expand Down
60 changes: 37 additions & 23 deletions nvm.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 \
Expand All @@ -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!"
Expand Down
76 changes: 76 additions & 0 deletions test/fast/Unit tests/nvm_get_default_packages
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ cleanup () {

setup

\. ../../../nvm.sh
\. ../../nvm.sh

cat > $FILE << EOF
rimraf
Expand All @@ -45,7 +45,7 @@ cleanup

setup

\. ../../../nvm.sh
\. ../../nvm.sh

cat > $FILE << EOF
rimraf
Expand Down

0 comments on commit 5c117e6

Please sign in to comment.