From 4ba7ee57978fc51c203c7ea00b535b9e73cc2196 Mon Sep 17 00:00:00 2001 From: elliottcable Date: Fri, 23 Jan 2015 20:38:50 -0600 Subject: [PATCH 1/8] install: Show a warning when global packages exist --- install.sh | 38 +++++++++++++++++++ test/install_script/nvm_check_global_modules | 40 ++++++++++++++++++++ 2 files changed, 78 insertions(+) create mode 100755 test/install_script/nvm_check_global_modules diff --git a/install.sh b/install.sh index 0e22f82..45070fc 100755 --- a/install.sh +++ b/install.sh @@ -120,6 +120,42 @@ nvm_detect_profile() { fi } +# +# Check whether the user has any globally-installed npm modules in their system +# Node, and warn them if so. +# +nvm_check_global_modules() { + local NPM_GLOBAL_MODULES + NPM_GLOBAL_MODULES=$(npm list -g --depth=0 | sed '/ npm@/d') + + local MODULE_COUNT + MODULE_COUNT=$( + printf %s\\n "$NPM_GLOBAL_MODULES" | + sed -ne '1!p' | # Remove the first line + wc -l | tr -d ' ' # Count entries + ) + + if [ $MODULE_COUNT -ne 0 ]; then + cat <<-'END_MESSAGE' + => You currently have modules installed globally with `npm`. These will no + => longer be linked to the active version of Node when you install a new node + => with `nvm`; and they may (depending on how you construct your `$PATH`) + => override the binaries of modules installed with `nvm`: + + END_MESSAGE + printf %s\\n "$NPM_GLOBAL_MODULES" + cat <<-'END_MESSAGE' + + => If you wish to uninstall them at a later point (or re-install them under your + => `nvm` Nodes), you can remove them from the system Node as follows: + + $ nvm use system + $ npm uninstall -g a_module + + END_MESSAGE + fi +} + nvm_do_install() { if [ -z "$METHOD" ]; then # Autodetect install method @@ -169,6 +205,8 @@ nvm_do_install() { fi fi + nvm_check_global_modules + echo "=> Close and reopen your terminal to start using nvm" nvm_reset } diff --git a/test/install_script/nvm_check_global_modules b/test/install_script/nvm_check_global_modules new file mode 100755 index 0000000..4a5661c --- /dev/null +++ b/test/install_script/nvm_check_global_modules @@ -0,0 +1,40 @@ +#!/bin/sh + +cleanup () { + rm -rf "$npm_config_prefix/lib" >/dev/null 2>&1 + unset npm_config_prefix + + unset -f setup cleanup die skip + unset message +} +die () { echo $@ ; cleanup ; exit 1; } + +NVM_ENV=testing . ../../install.sh + +setup () { + npm_config_prefix="$(pwd)" + export npm_config_prefix + mkdir -p "$npm_config_prefix/lib" +} + + +setup + +npm install -g nop >/dev/null 2>&1 +message=$(nvm_check_global_modules) +[ ! -z "$message" ] || die "nvm_check_global_modules should have printed a notice when npm had global modules installed" + +npm uninstall -g nop >/dev/null 2>&1 +message=$(nvm_check_global_modules) +[ -z "$message" ] || die "nvm_check_global_modules should not have printed a notice when npm had no global modules installed" + +# Faking an installation of npm +mkdir -p "$npm_config_prefix/lib/node_modules/npm" +cat <<'JSON' >"$npm_config_prefix/lib/node_modules/npm/package.json" +{ "name": "npm", "version": "0.0.1fake" } +JSON + +message=$(nvm_check_global_modules) +[ -z "$message" ] || die "nvm_check_global_modules should have not printed a notice when npm had only itself installed as a global module" + +cleanup From dd1a9ca6a0ae61289240d4317514fcf156b42362 Mon Sep 17 00:00:00 2001 From: elliottcable Date: Thu, 29 Jan 2015 21:23:16 -0600 Subject: [PATCH 2/8] install: Adding global-module check to function resets --- install.sh | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/install.sh b/install.sh index 45070fc..125e651 100755 --- a/install.sh +++ b/install.sh @@ -216,7 +216,9 @@ nvm_do_install() { # during the execution of the install script # nvm_reset() { - unset -f nvm_do_install nvm_has nvm_download install_nvm_as_script install_nvm_from_git nvm_reset nvm_detect_profile nvm_latest_version + unset -f nvm_reset nvm_has nvm_latest_version \ + nvm_source nvm_download install_nvm_as_script install_nvm_from_git \ + nvm_detect_profile nvm_check_global_modules nvm_do_install } [ "_$NVM_ENV" = "_testing" ] || nvm_do_install From 6cfc30933659e0976d21d4094b09664f2530b768 Mon Sep 17 00:00:00 2001 From: elliottcable Date: Mon, 2 Feb 2015 20:42:12 -0600 Subject: [PATCH 3/8] install: Pass global-module check in the absence of npm --- install.sh | 7 +++++++ test/install_script/nvm_check_global_modules | 16 +++++++++++++++- 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/install.sh b/install.sh index 125e651..5018540 100755 --- a/install.sh +++ b/install.sh @@ -125,6 +125,13 @@ nvm_detect_profile() { # Node, and warn them if so. # nvm_check_global_modules() { + command -v npm >/dev/null 2>&1 || return 0 + + local NPM_VERSION + NPM_VERSION="$(npm --version)" + NPM_VERSION="${NPM_VERSION:-0}" + [ "${NPM_VERSION%%[!0-9]*}" -gt 1 ] || return 0 + local NPM_GLOBAL_MODULES NPM_GLOBAL_MODULES=$(npm list -g --depth=0 | sed '/ npm@/d') diff --git a/test/install_script/nvm_check_global_modules b/test/install_script/nvm_check_global_modules index 4a5661c..10e0d40 100755 --- a/test/install_script/nvm_check_global_modules +++ b/test/install_script/nvm_check_global_modules @@ -4,14 +4,19 @@ cleanup () { rm -rf "$npm_config_prefix/lib" >/dev/null 2>&1 unset npm_config_prefix + rm -f npm + PATH="$ORIGINAL_PATH" + unset -f setup cleanup die skip - unset message + unset message ORIGINAL_PATH } die () { echo $@ ; cleanup ; exit 1; } NVM_ENV=testing . ../../install.sh setup () { + ORIGINAL_PATH="$PATH" + npm_config_prefix="$(pwd)" export npm_config_prefix mkdir -p "$npm_config_prefix/lib" @@ -37,4 +42,13 @@ JSON message=$(nvm_check_global_modules) [ -z "$message" ] || die "nvm_check_global_modules should have not printed a notice when npm had only itself installed as a global module" +# Faking the absence of npm +PATH=".:$PATH" +touch npm +chmod +x npm + +message=$(nvm_check_global_modules) +[ -z "$message" ] || die "nvm_check_global_modules should have not printed a notice when npm was unavailable" + + cleanup From 0717d5f99508c89c752e2f1b1532e2747c742696 Mon Sep 17 00:00:00 2001 From: elliottcable Date: Wed, 4 Feb 2015 16:39:57 -0600 Subject: [PATCH 4/8] install: some tweaks and clean-up --- install.sh | 8 ++++---- test/install_script/nvm_check_global_modules | 4 ++-- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/install.sh b/install.sh index 5018540..4339874 100755 --- a/install.sh +++ b/install.sh @@ -133,14 +133,14 @@ nvm_check_global_modules() { [ "${NPM_VERSION%%[!0-9]*}" -gt 1 ] || return 0 local NPM_GLOBAL_MODULES - NPM_GLOBAL_MODULES=$(npm list -g --depth=0 | sed '/ npm@/d') - + NPM_GLOBAL_MODULES="$(npm list -g --depth=0 | sed '/ npm@/d')" + local MODULE_COUNT - MODULE_COUNT=$( + MODULE_COUNT="$( printf %s\\n "$NPM_GLOBAL_MODULES" | sed -ne '1!p' | # Remove the first line wc -l | tr -d ' ' # Count entries - ) + )" if [ $MODULE_COUNT -ne 0 ]; then cat <<-'END_MESSAGE' diff --git a/test/install_script/nvm_check_global_modules b/test/install_script/nvm_check_global_modules index 10e0d40..5d5db22 100755 --- a/test/install_script/nvm_check_global_modules +++ b/test/install_script/nvm_check_global_modules @@ -25,11 +25,11 @@ setup () { setup -npm install -g nop >/dev/null 2>&1 +npm install -g nop >/dev/null message=$(nvm_check_global_modules) [ ! -z "$message" ] || die "nvm_check_global_modules should have printed a notice when npm had global modules installed" -npm uninstall -g nop >/dev/null 2>&1 +npm uninstall -g nop >/dev/null message=$(nvm_check_global_modules) [ -z "$message" ] || die "nvm_check_global_modules should not have printed a notice when npm had no global modules installed" From c8efe3d28ab862441f7f40e2683b1a91688aabc4 Mon Sep 17 00:00:00 2001 From: elliottcable Date: Sat, 7 Feb 2015 20:50:10 -0600 Subject: [PATCH 5/8] install: adding some debugging output --- .travis.yml | 2 +- install.sh | 33 ++++++++++++-- test/install_script/nvm_check_global_modules | 47 +++++++++++++++++--- 3 files changed, 71 insertions(+), 11 deletions(-) diff --git a/.travis.yml b/.travis.yml index 762181b..b396ea9 100644 --- a/.travis.yml +++ b/.travis.yml @@ -5,7 +5,7 @@ install: - chmod +x /tmp/urchin/package/urchin - '[ -z "$WITHOUT_CURL" ] || sudo apt-get remove curl -y' script: - - NVM_DIR=$TRAVIS_BUILD_DIR make TEST_SUITE=$TEST_SUITE URCHIN=/tmp/urchin/package/urchin test-$SHELL + - DEBUG='nvm:*' NVM_DIR=$TRAVIS_BUILD_DIR make TEST_SUITE=$TEST_SUITE URCHIN=/tmp/urchin/package/urchin test-$SHELL env: - SHELL=sh TEST_SUITE=install_script - SHELL=dash TEST_SUITE=install_script diff --git a/install.sh b/install.sh index 4339874..b3ca449 100755 --- a/install.sh +++ b/install.sh @@ -1,7 +1,16 @@ #!/bin/bash - set -e +puts() (IFS=" "; printf %s\\n "$*" ;) +error() (IFS=" "; printf %s\\n "$*" >&2 ;) +debug() (IFS=" "; printf %s\\n ".. $*" >&2 ;) + +if [ "$DEBUG" = 'nvm:*' ] || [ "$DEBUG" = 'nvm:install' ]; then + NVM_DEBUG=0 + debug 'Script debugging enabled (in: `install.sh`).' +fi + + nvm_has() { type "$1" > /dev/null 2>&1 } @@ -42,7 +51,9 @@ nvm_source() { nvm_download() { if nvm_has "curl"; then + [ "$NVM_DEBUG" = 0 ] && set +x curl $* + [ "$NVM_DEBUG" = 0 ] && set -x elif nvm_has "wget"; then # Emulate curl with wget ARGS=$(echo "$*" | command sed -e 's/--progress-bar /--progress=bar /' \ @@ -51,7 +62,9 @@ nvm_download() { -e 's/-s /-q /' \ -e 's/-o /-O /' \ -e 's/-C - /-c /') + [ "$NVM_DEBUG" = 0 ] && set +x wget $ARGS + [ "$NVM_DEBUG" = 0 ] && set +x fi } @@ -66,10 +79,18 @@ install_nvm_from_git() { # Cloning to $NVM_DIR echo "=> Downloading nvm from git to '$NVM_DIR'" printf "\r=> " + [ "$NVM_DEBUG" = 0 ] && set +x mkdir -p "$NVM_DIR" command git clone "$(nvm_source git)" "$NVM_DIR" + [ "$NVM_DEBUG" = 0 ] && set -x fi - cd "$NVM_DIR" && command git checkout --quiet $(nvm_latest_version) && command git branch --quiet -D master >/dev/null 2>&1 + + [ "$NVM_DEBUG" = 0 ] && set +x + cd "$NVM_DIR" || return $? + command git checkout --quiet $(nvm_latest_version) || return $? + command git branch --quiet -D master >/dev/null 2>&1 || return $? + [ "$NVM_DEBUG" = 0 ] && set +x + return } @@ -130,6 +151,7 @@ nvm_check_global_modules() { local NPM_VERSION NPM_VERSION="$(npm --version)" NPM_VERSION="${NPM_VERSION:-0}" + [ "$NVM_DEBUG" = 0 ] && debug "NPM detected (at version ${NPM_VERSION}.)" [ "${NPM_VERSION%%[!0-9]*}" -gt 1 ] || return 0 local NPM_GLOBAL_MODULES @@ -141,6 +163,9 @@ nvm_check_global_modules() { sed -ne '1!p' | # Remove the first line wc -l | tr -d ' ' # Count entries )" + [ "$NVM_DEBUG" = 0 ] && { + debug "(${MODULE_COUNT}) global modules detected:" + error "$NPM_GLOBAL_MODULES" ;} if [ $MODULE_COUNT -ne 0 ]; then cat <<-'END_MESSAGE' @@ -223,9 +248,11 @@ nvm_do_install() { # during the execution of the install script # nvm_reset() { - unset -f nvm_reset nvm_has nvm_latest_version \ + unset -f puts error debug \ + nvm_reset nvm_has nvm_latest_version \ nvm_source nvm_download install_nvm_as_script install_nvm_from_git \ nvm_detect_profile nvm_check_global_modules nvm_do_install + unset NPM_DEBUG } [ "_$NVM_ENV" = "_testing" ] || nvm_do_install diff --git a/test/install_script/nvm_check_global_modules b/test/install_script/nvm_check_global_modules index 5d5db22..5a44241 100755 --- a/test/install_script/nvm_check_global_modules +++ b/test/install_script/nvm_check_global_modules @@ -1,4 +1,5 @@ #!/bin/sh +puts() (IFS=" "; printf %s\\n "$*" ;) cleanup () { rm -rf "$npm_config_prefix/lib" >/dev/null 2>&1 @@ -7,10 +8,11 @@ cleanup () { rm -f npm PATH="$ORIGINAL_PATH" - unset -f setup cleanup die skip + unset -f setup cleanup die unset message ORIGINAL_PATH } -die () { echo $@ ; cleanup ; exit 1; } + +die () { puts "!! $@" ; cleanup ; exit 1; } NVM_ENV=testing . ../../install.sh @@ -22,16 +24,32 @@ setup () { mkdir -p "$npm_config_prefix/lib" } - setup + npm install -g nop >/dev/null message=$(nvm_check_global_modules) -[ ! -z "$message" ] || die "nvm_check_global_modules should have printed a notice when npm had global modules installed" +[ ! -z "$message" ] || { + puts '-- `npm --version`: '"$(npm --version)" + puts '-- `npm list -g`:' + npm list -g --depth=0 + puts '-- Printed message:' + puts "'''$message'''" + + die "nvm_check_global_modules should have printed a notice when npm had global modules installed" ;} + npm uninstall -g nop >/dev/null message=$(nvm_check_global_modules) -[ -z "$message" ] || die "nvm_check_global_modules should not have printed a notice when npm had no global modules installed" +[ -z "$message" ] || { + puts '-- `npm --version`: '"$(npm --version)" + puts '-- `npm list -g`:' + npm list -g --depth=0 + puts '-- Printed message:' + puts "'''$message'''" + + die "nvm_check_global_modules should not have printed a notice when npm had no global modules installed" ;} + # Faking an installation of npm mkdir -p "$npm_config_prefix/lib/node_modules/npm" @@ -40,7 +58,16 @@ cat <<'JSON' >"$npm_config_prefix/lib/node_modules/npm/package.json" JSON message=$(nvm_check_global_modules) -[ -z "$message" ] || die "nvm_check_global_modules should have not printed a notice when npm had only itself installed as a global module" +[ -z "$message" ] || { + puts '-- `which npm`: ' "$(which npm)" + puts '-- `npm --version`: ' "$(npm --version)" + puts '-- `npm list -g`:' + npm list -g --depth=0 + puts '-- Printed message:' + puts "'''$message'''" + + die "nvm_check_global_modules should not have printed a notice when npm had only itself installed as a global module" ;} + # Faking the absence of npm PATH=".:$PATH" @@ -48,7 +75,13 @@ touch npm chmod +x npm message=$(nvm_check_global_modules) -[ -z "$message" ] || die "nvm_check_global_modules should have not printed a notice when npm was unavailable" +[ -z "$message" ] || { + puts '-- `which npm`: ' "$(which npm)" + puts '-- `npm --version`: ' "$(npm --version)" + puts '-- Printed message:' + puts "'''$message'''" + + die "nvm_check_global_modules should not have printed a notice when npm was unavailable" ;} cleanup From a216f56443daf9e9ed3047be563f93de9ec1f083 Mon Sep 17 00:00:00 2001 From: elliottcable Date: Sat, 7 Feb 2015 20:53:53 -0600 Subject: [PATCH 6/8] tests: fail install_script test if npm can't install package --- test/install_script/nvm_check_global_modules | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/install_script/nvm_check_global_modules b/test/install_script/nvm_check_global_modules index 5a44241..0c6889a 100755 --- a/test/install_script/nvm_check_global_modules +++ b/test/install_script/nvm_check_global_modules @@ -27,7 +27,7 @@ setup () { setup -npm install -g nop >/dev/null +npm install -g nop >/dev/null || die 'nvm_check_global_modules cannot be tested because `npm` cannot install the `nop` package' message=$(nvm_check_global_modules) [ ! -z "$message" ] || { puts '-- `npm --version`: '"$(npm --version)" From ea4264645b07484dc071f4027f003804cd6e2893 Mon Sep 17 00:00:00 2001 From: elliottcable Date: Sat, 7 Feb 2015 21:12:46 -0600 Subject: [PATCH 7/8] install: support `npm`s lower than v2 --- install.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/install.sh b/install.sh index b3ca449..63a7af4 100755 --- a/install.sh +++ b/install.sh @@ -150,9 +150,9 @@ nvm_check_global_modules() { local NPM_VERSION NPM_VERSION="$(npm --version)" - NPM_VERSION="${NPM_VERSION:-0}" + NPM_VERSION="${NPM_VERSION:--1}" [ "$NVM_DEBUG" = 0 ] && debug "NPM detected (at version ${NPM_VERSION}.)" - [ "${NPM_VERSION%%[!0-9]*}" -gt 1 ] || return 0 + [ "${NPM_VERSION%%[!0-9]*}" -gt 0 ] || return 0 local NPM_GLOBAL_MODULES NPM_GLOBAL_MODULES="$(npm list -g --depth=0 | sed '/ npm@/d')" From 4508f7c33ea79f458eec3cbcaac7acf4c033032f Mon Sep 17 00:00:00 2001 From: elliottcable Date: Sun, 8 Feb 2015 02:43:05 -0600 Subject: [PATCH 8/8] install: further support for older npm versions --- install.sh | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/install.sh b/install.sh index 63a7af4..3b3590b 100755 --- a/install.sh +++ b/install.sh @@ -152,10 +152,14 @@ nvm_check_global_modules() { NPM_VERSION="$(npm --version)" NPM_VERSION="${NPM_VERSION:--1}" [ "$NVM_DEBUG" = 0 ] && debug "NPM detected (at version ${NPM_VERSION}.)" - [ "${NPM_VERSION%%[!0-9]*}" -gt 0 ] || return 0 + [ "${NPM_VERSION%%[!-0-9]*}" -gt 0 ] || return 0 local NPM_GLOBAL_MODULES - NPM_GLOBAL_MODULES="$(npm list -g --depth=0 | sed '/ npm@/d')" + NPM_GLOBAL_MODULES="$( + npm list -g --depth=0 | + sed '/ npm@/d' | + sed '/ (empty)$/d' + )" local MODULE_COUNT MODULE_COUNT="$(