diff --git a/.gitignore b/.gitignore index fc578cc..b126f5e 100644 --- a/.gitignore +++ b/.gitignore @@ -14,3 +14,4 @@ npm-debug.log .DS_Store current +default-packages diff --git a/README.md b/README.md index e286899..a7cb2be 100644 --- a/README.md +++ b/README.md @@ -13,6 +13,7 @@ - [Usage](#usage) - [Long-term support](#long-term-support) - [Migrating global packages while installing](#migrating-global-packages-while-installing) + - [Default global packages from file while installing](#default-global-packages-from-file-while-installing) - [io.js](#iojs) - [System version of node](#system-version-of-node) - [Listing versions](#listing-versions) @@ -228,6 +229,18 @@ nvm install 6 --reinstall-packages-from=5 nvm install v4.2 --reinstall-packages-from=iojs ``` +### Default global packages from file while installing + +If you have a list of default packages you want installed every time you install a new version we support that too. You can add anything npm would accept as a package argument on the command line. + +```sh +# $NVM_DIR/default-packages + +rimraf +object-inspect@1.0.2 +stevemao/left-pad +``` + ### io.js If you want to install [io.js](https://github.com/iojs/io.js/): diff --git a/nvm.sh b/nvm.sh index 32ecea3..88d391f 100644 --- a/nvm.sh +++ b/nvm.sh @@ -2202,6 +2202,7 @@ nvm() { nvm_echo ' --reinstall-packages-from= When installing, reinstall packages installed in ' nvm_echo ' --lts When installing, only select from LTS (long-term support) versions' nvm_echo ' --lts= When installing, only select from versions for a specific LTS line' + nvm_echo ' --skip-default-packages When installing, skip the default-packages file if it exists' nvm_echo ' nvm uninstall Uninstall a version' nvm_echo ' nvm uninstall --lts Uninstall using automatic LTS (long-term support) alias `lts/*`, if available.' nvm_echo ' nvm uninstall --lts= Uninstall using automatic alias for provided LTS line, if available.' @@ -2421,6 +2422,8 @@ nvm() { ADDITIONAL_PARAMETERS='' local PROVIDED_REINSTALL_PACKAGES_FROM local REINSTALL_PACKAGES_FROM + local SKIP_DEFAULT_PACKAGES + local DEFAULT_PACKAGES while [ $# -ne 0 ] do @@ -2433,6 +2436,9 @@ nvm() { PROVIDED_REINSTALL_PACKAGES_FROM="$(nvm_echo "$1" | command cut -c 22-)" REINSTALL_PACKAGES_FROM="$(nvm_version "$PROVIDED_REINSTALL_PACKAGES_FROM")" ||: ;; + --skip-default-packages) + SKIP_DEFAULT_PACKAGES=true + ;; *) ADDITIONAL_PARAMETERS="$ADDITIONAL_PARAMETERS $1" ;; @@ -2440,6 +2446,30 @@ 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 `#`. + [ "$(echo "$line" | 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-seperated values." + return 1 + ;; + esac + + DEFAULT_PACKAGES="${DEFAULT_PACKAGES}${line} " + done < "${NVM_DIR}/default-packages" + fi + if [ -n "${PROVIDED_REINSTALL_PACKAGES_FROM-}" ] && [ "$(nvm_ensure_version_prefix "${PROVIDED_REINSTALL_PACKAGES_FROM}")" = "${VERSION}" ]; then nvm_err "You can't reinstall global packages from the same version of node you're installing." return 4 @@ -2457,8 +2487,13 @@ nvm() { if nvm_is_version_installed "$VERSION"; then nvm_err "$VERSION is already installed." - if nvm use "$VERSION" && [ -n "${REINSTALL_PACKAGES_FROM-}" ] && [ "_$REINSTALL_PACKAGES_FROM" != "_N/A" ]; then - nvm reinstall-packages "$REINSTALL_PACKAGES_FROM" + if nvm use "$VERSION"; then + if [ -z "${SKIP_DEFAULT_PACKAGES-}" ] && [ -n "${DEFAULT_PACKAGES-}" ]; then + nvm_install_default_packages "$DEFAULT_PACKAGES" + fi + if [ -n "${REINSTALL_PACKAGES_FROM-}" ] && [ "_$REINSTALL_PACKAGES_FROM" != "_N/A" ]; then + nvm reinstall-packages "$REINSTALL_PACKAGES_FROM" + fi fi if [ -n "${LTS-}" ]; then nvm_ensure_default_set "lts/${LTS}" @@ -2525,8 +2560,10 @@ nvm() { else nvm_ensure_default_set "$provided_version" fi - if [ -n "${REINSTALL_PACKAGES_FROM-}" ] \ - && [ "_$REINSTALL_PACKAGES_FROM" != "_N/A" ]; then + if [ -z "${SKIP_DEFAULT_PACKAGES-}" ] && [ -n "${DEFAULT_PACKAGES-}" ]; then + nvm_install_default_packages "$DEFAULT_PACKAGES" + fi + if [ -n "${REINSTALL_PACKAGES_FROM-}" ] && [ "_$REINSTALL_PACKAGES_FROM" != "_N/A" ]; then nvm reinstall-packages "$REINSTALL_PACKAGES_FROM" EXIT_CODE=$? fi @@ -3247,7 +3284,7 @@ nvm() { nvm_version_greater nvm_version_greater_than_or_equal_to \ nvm_print_npm_version nvm_npm_global_modules \ nvm_has_system_node nvm_has_system_iojs \ - nvm_download nvm_get_latest nvm_has \ + nvm_download nvm_get_latest nvm_has nvm_install_default_packages \ 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 \ @@ -3270,6 +3307,15 @@ nvm() { esac } +nvm_install_default_packages() { + nvm_echo "Installing default global packages from ${NVM_DIR}/default-packages..." + + 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!" + return 1 + fi +} + nvm_supports_source_options() { # shellcheck disable=SC1091 [ "_$(echo '[ $# -gt 0 ] && echo $1' | . /dev/stdin yes 2> /dev/null)" = "_yes" ] diff --git "a/test/fast/Running \"nvm unload\" should unset all function and variables." "b/test/fast/Running \"nvm unload\" should unset all function and variables." index f42470c..d2cad17 100755 --- "a/test/fast/Running \"nvm unload\" should unset all function and variables." +++ "b/test/fast/Running \"nvm unload\" should unset all function and variables." @@ -10,7 +10,9 @@ die () { echo "$@" ; cleanup ; exit 1; } typeset -f | awk '/ \(\) $/ && !/^main / {print $1}' > "${BEFORE}" +set +e # TODO: fix \. ../../nvm.sh +set -e type nvm > /dev/null 2>&1 || die "nvm not loaded" diff --git a/test/fast/Unit tests/nvm_default_packages b/test/fast/Unit tests/nvm_default_packages new file mode 100755 index 0000000..0ae318d --- /dev/null +++ b/test/fast/Unit tests/nvm_default_packages @@ -0,0 +1,115 @@ +#!/bin/sh + +FILE="$NVM_DIR/default-packages" + +die () { echo "$@" ; cleanup ; exit 1; } +setup () { + if [ -f $FILE ]; then + ORIG_DEFAULT_PACKAGES=$(cat $FILE) + mkdir ./tmp/ ||: + mv $FILE ./tmp/default-packages ||: + fi + touch $FILE +} +cleanup () { + rm -rf "$(nvm_version_path v6.10.1)" $FILE + if [ "$ORIG_DEFAULT_PACKAGES" != "" ]; then + rm -rf ./tmp/ + echo "$ORIG_DEFAULT_PACKAGES" > $FILE + fi +} + +setup + +\. ../../../nvm.sh + +cat > $FILE << EOF +rimraf +object-inspect@1.0.2 + +# commented-package + +stevemao/left-pad +EOF + +nvm install v6.10.1 2>&1 +EXIT_CODE=$? +[ "_$EXIT_CODE" = "_0" ] || die "expected 'nvm install v6.10.1' to exit with 0, got $EXIT_CODE" + +nvm exec v6.10.1 npm ls -g --depth=0 | grep -q 'rimraf' +if [ -z "$?" ]; then + die "expected 'nvm exec v6.10.1 npm ls -g --depth=0 | grep -q 'rimraf'' to exit with 0, got $?" +fi + +cleanup + +setup + +\. ../../../nvm.sh + +cat > $FILE << EOF +rimraf +object-inspect@1.0.2 + +# commented-package + +stevemao/left-pad +EOF + +nvm install v6.10.1 --skip-default-packages 2>&1 +EXIT_CODE=$? +[ "_$EXIT_CODE" = "_0" ] || die "expected 'nvm install v6.10.1' to exit with 0, got $EXIT_CODE" + +if nvm exec v6.10.1 npm ls -g --depth=0 | grep -q 'rimraf'; then + die "expected 'nvm exec v6.10.1 npm ls -g --depth=0 | grep -q 'rimraf'' to be empty" +fi + +cleanup + +setup + +cat > $FILE << EOF +not~a~package~name +EOF + +nvm install v6.10.1 +EXIT_CODE=$? +[ "_$EXIT_CODE" = "_0" ] || die "expected 'nvm install v6.10.1' to exit with 0, got $EXIT_CODE" + +if nvm exec v6.10.1 npm ls -g --depth=0 | grep -q 'not~a~package~name'; then + die "expected 'nvm exec v6.10.1 npm ls -g --depth=0 | grep -q 'not~a~package~name'' to exit with 1, got $?" +fi + +cleanup + +setup + +cat > $FILE << EOF +object-inspect @ 1.0.2 +EOF + +nvm install v6.10.1 2>&1 +EXIT_CODE=$? +[ "_$EXIT_CODE" = "_1" ] || die "expected 'nvm install v6.10.1' to exit with 1, got $EXIT_CODE" + +if nvm exec v6.10.1 npm ls -g --depth=0 | grep -q 'object-inspect'; then + die "expected 'nvm exec v6.10.1 npm ls -g --depth=0 | grep -q 'object-inspect'' to exit with 1, got $?" +fi + +cleanup + +setup + +rm -rf $FILE + +nvm install v6.10.1 2>&1 +EXIT_CODE=$? +[ "_$EXIT_CODE" = "_0" ] || die "expected 'nvm install v6.10.1' to exit with 0, got $EXIT_CODE" + +if nvm exec v6.10.1 npm ls -g --depth=0 | grep -q 'object-inspect'; then + die "expected 'nvm exec v6.10.1 npm ls -g --depth=0 | grep -q 'object-inspect'' to exit with 1, got $?" +fi + +touch $FILE + +cleanup diff --git a/test/fast/Unit tests/nvm_get_checksum_alg b/test/fast/Unit tests/nvm_get_checksum_alg index 6a68e8c..1869437 100755 --- a/test/fast/Unit tests/nvm_get_checksum_alg +++ b/test/fast/Unit tests/nvm_get_checksum_alg @@ -4,7 +4,9 @@ set -ex die () { echo "$@" ; exit 1; } +set +e # TODO: fix \. ../../../nvm.sh +set -e ALG="$(nvm_get_checksum_alg)" diff --git a/test/fast/Unit tests/nvm_get_mirror b/test/fast/Unit tests/nvm_get_mirror index f4116a6..8d4b192 100755 --- a/test/fast/Unit tests/nvm_get_mirror +++ b/test/fast/Unit tests/nvm_get_mirror @@ -7,7 +7,9 @@ die () { echo "$@" ; exit 1; } unset NVM_NODEJS_ORG_MIRROR unset NVM_IOJS_ORG_MIRROR +set +e # TODO: fix \. ../../../nvm.sh +set -e ! nvm_get_mirror || die 'unknown release type did not error' ! nvm_get_mirror node || die 'unknown release type did not error' @@ -28,4 +30,3 @@ unset NVM_NODEJS_ORG_MIRROR NVM_IOJS_ORG_MIRROR="test://domain" [ "$(nvm_get_mirror iojs std)" = "test://domain" ] || die "iojs-std mirror should respect NVM_IOJS_ORG_MIRROR" unset NVM_IOJS_ORG_MIRROR -