[New] add support for `$NVM_DIR/default-packages` file

Weston Ganger 2017-03-28 09:37:14 -07:00 committed by Jordan Harband
parent dc5dfe3bc1
commit 7f3145bc98
No known key found for this signature in database
GPG Key ID: 64A196AEE0916D55
7 changed files with 186 additions and 6 deletions

1
.gitignore vendored
View File

@ -14,3 +14,4 @@ npm-debug.log
.DS_Store .DS_Store
current current
default-packages

View File

@ -13,6 +13,7 @@
- [Usage](#usage) - [Usage](#usage)
- [Long-term support](#long-term-support) - [Long-term support](#long-term-support)
- [Migrating global packages while installing](#migrating-global-packages-while-installing) - [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) - [io.js](#iojs)
- [System version of node](#system-version-of-node) - [System version of node](#system-version-of-node)
- [Listing versions](#listing-versions) - [Listing versions](#listing-versions)
@ -228,6 +229,18 @@ nvm install 6 --reinstall-packages-from=5
nvm install v4.2 --reinstall-packages-from=iojs 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 ### io.js
If you want to install [io.js](https://github.com/iojs/io.js/): If you want to install [io.js](https://github.com/iojs/io.js/):

54
nvm.sh
View File

@ -2202,6 +2202,7 @@ nvm() {
nvm_echo ' --reinstall-packages-from=<version> When installing, reinstall packages installed in <node|iojs|node version number>' 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 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 ' --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 <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 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.' nvm_echo ' nvm uninstall --lts=<LTS name> Uninstall using automatic alias for provided LTS line, if available.'
@ -2421,6 +2422,8 @@ nvm() {
ADDITIONAL_PARAMETERS='' ADDITIONAL_PARAMETERS=''
local PROVIDED_REINSTALL_PACKAGES_FROM local PROVIDED_REINSTALL_PACKAGES_FROM
local REINSTALL_PACKAGES_FROM local REINSTALL_PACKAGES_FROM
local SKIP_DEFAULT_PACKAGES
local DEFAULT_PACKAGES
while [ $# -ne 0 ] while [ $# -ne 0 ]
do do
@ -2433,6 +2436,9 @@ nvm() {
PROVIDED_REINSTALL_PACKAGES_FROM="$(nvm_echo "$1" | command cut -c 22-)" PROVIDED_REINSTALL_PACKAGES_FROM="$(nvm_echo "$1" | command cut -c 22-)"
REINSTALL_PACKAGES_FROM="$(nvm_version "$PROVIDED_REINSTALL_PACKAGES_FROM")" ||: REINSTALL_PACKAGES_FROM="$(nvm_version "$PROVIDED_REINSTALL_PACKAGES_FROM")" ||:
;; ;;
--skip-default-packages)
SKIP_DEFAULT_PACKAGES=true
;;
*) *)
ADDITIONAL_PARAMETERS="$ADDITIONAL_PARAMETERS $1" ADDITIONAL_PARAMETERS="$ADDITIONAL_PARAMETERS $1"
;; ;;
@ -2440,6 +2446,30 @@ nvm() {
shift shift
done 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 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." nvm_err "You can't reinstall global packages from the same version of node you're installing."
return 4 return 4
@ -2457,9 +2487,14 @@ nvm() {
if nvm_is_version_installed "$VERSION"; then if nvm_is_version_installed "$VERSION"; then
nvm_err "$VERSION is already installed." nvm_err "$VERSION is already installed."
if nvm use "$VERSION" && [ -n "${REINSTALL_PACKAGES_FROM-}" ] && [ "_$REINSTALL_PACKAGES_FROM" != "_N/A" ]; then 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" nvm reinstall-packages "$REINSTALL_PACKAGES_FROM"
fi fi
fi
if [ -n "${LTS-}" ]; then if [ -n "${LTS-}" ]; then
nvm_ensure_default_set "lts/${LTS}" nvm_ensure_default_set "lts/${LTS}"
else else
@ -2525,8 +2560,10 @@ nvm() {
else else
nvm_ensure_default_set "$provided_version" nvm_ensure_default_set "$provided_version"
fi fi
if [ -n "${REINSTALL_PACKAGES_FROM-}" ] \ if [ -z "${SKIP_DEFAULT_PACKAGES-}" ] && [ -n "${DEFAULT_PACKAGES-}" ]; then
&& [ "_$REINSTALL_PACKAGES_FROM" != "_N/A" ]; 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" nvm reinstall-packages "$REINSTALL_PACKAGES_FROM"
EXIT_CODE=$? EXIT_CODE=$?
fi fi
@ -3247,7 +3284,7 @@ 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_npm_global_modules \ nvm_print_npm_version 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_download nvm_get_latest nvm_has nvm_install_default_packages \
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 \
@ -3270,6 +3307,15 @@ nvm() {
esac 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() { nvm_supports_source_options() {
# shellcheck disable=SC1091 # shellcheck disable=SC1091
[ "_$(echo '[ $# -gt 0 ] && echo $1' | . /dev/stdin yes 2> /dev/null)" = "_yes" ] [ "_$(echo '[ $# -gt 0 ] && echo $1' | . /dev/stdin yes 2> /dev/null)" = "_yes" ]

View File

@ -10,7 +10,9 @@ die () { echo "$@" ; cleanup ; exit 1; }
typeset -f | awk '/ \(\) $/ && !/^main / {print $1}' > "${BEFORE}" typeset -f | awk '/ \(\) $/ && !/^main / {print $1}' > "${BEFORE}"
set +e # TODO: fix
\. ../../nvm.sh \. ../../nvm.sh
set -e
type nvm > /dev/null 2>&1 || die "nvm not loaded" type nvm > /dev/null 2>&1 || die "nvm not loaded"

View File

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

View File

@ -4,7 +4,9 @@ set -ex
die () { echo "$@" ; exit 1; } die () { echo "$@" ; exit 1; }
set +e # TODO: fix
\. ../../../nvm.sh \. ../../../nvm.sh
set -e
ALG="$(nvm_get_checksum_alg)" ALG="$(nvm_get_checksum_alg)"

View File

@ -7,7 +7,9 @@ die () { echo "$@" ; exit 1; }
unset NVM_NODEJS_ORG_MIRROR unset NVM_NODEJS_ORG_MIRROR
unset NVM_IOJS_ORG_MIRROR unset NVM_IOJS_ORG_MIRROR
set +e # TODO: fix
\. ../../../nvm.sh \. ../../../nvm.sh
set -e
! nvm_get_mirror || die 'unknown release type did not error' ! nvm_get_mirror || die 'unknown release type did not error'
! nvm_get_mirror node || 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_IOJS_ORG_MIRROR="test://domain"
[ "$(nvm_get_mirror iojs std)" = "test://domain" ] || die "iojs-std mirror should respect NVM_IOJS_ORG_MIRROR" [ "$(nvm_get_mirror iojs std)" = "test://domain" ] || die "iojs-std mirror should respect NVM_IOJS_ORG_MIRROR"
unset NVM_IOJS_ORG_MIRROR unset NVM_IOJS_ORG_MIRROR