Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for ~/nvm/default-packages file #1463

Merged
merged 1 commit into from
Apr 28, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ npm-debug.log

.DS_Store
current
default-packages
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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/):

Expand Down
56 changes: 51 additions & 5 deletions nvm.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2202,6 +2202,7 @@ nvm() {
nvm_echo ' --reinstall-packages-from=<version> When installing, reinstall packages installed in <node|iojs|node version number>'
nvm_echo ' --lts When installing, only select from LTS (long-term support) versions'
nvm_echo ' --lts=<LTS name> 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 <version> Uninstall a version'
nvm_echo ' nvm uninstall --lts Uninstall using automatic LTS (long-term support) alias `lts/*`, if available.'
nvm_echo ' nvm uninstall --lts=<LTS name> Uninstall using automatic alias for provided LTS line, if available.'
Expand Down Expand Up @@ -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
Expand All @@ -2433,13 +2436,40 @@ 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"
;;
esac
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
Expand All @@ -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}"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 \
Expand All @@ -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" ]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down
115 changes: 115 additions & 0 deletions test/fast/Unit tests/nvm_default_packages
Original file line number Diff line number Diff line change
@@ -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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cute

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
2 changes: 2 additions & 0 deletions test/fast/Unit tests/nvm_get_checksum_alg
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ set -ex

die () { echo "$@" ; exit 1; }

set +e # TODO: fix
\. ../../../nvm.sh
set -e

ALG="$(nvm_get_checksum_alg)"

Expand Down
3 changes: 2 additions & 1 deletion test/fast/Unit tests/nvm_get_mirror
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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