Merge pull request #631 from ELLIOTTCABLE/notify-about-global-modules
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Notify the user that they're losing access to global modules
authored by Jordan Harband · committed by Jordan Harband · parents 40c9d6f, 4508f7c · Browse files · 3 files·+169 added−4 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+169 added−4 removed
- tests:+87 added−0 removed
- other:+82 added−4 removed
Provenance
- Commit
4768973- Tree
b0c78c9- Author
Jordan Harband <ljharb@gmail.com>- Committer
Jordan Harband <ljharb@gmail.com>- Parent 1
40c9d6f- Parent 2
4508f7c
3 files changed+169 added−4 removed
added:test/install_script/nvm_check_global_modules+87 added
1+#!/bin/sh2+puts() (IFS=" "; printf %s\\n "$*" ;)3+4+cleanup () {5+ rm -rf "$npm_config_prefix/lib" >/dev/null 2>&16+ unset npm_config_prefix7+8+ rm -f npm9+ PATH="$ORIGINAL_PATH"10+11+ unset -f setup cleanup die12+ unset message ORIGINAL_PATH13+}14+15+die () { puts "!! $@" ; cleanup ; exit 1; }16+17+NVM_ENV=testing . ../../install.sh18+19+setup () {20+ ORIGINAL_PATH="$PATH"21+22+ npm_config_prefix="$(pwd)"23+ export npm_config_prefix24+ mkdir -p "$npm_config_prefix/lib"25+}26+27+setup28+29+30+npm install -g nop >/dev/null || die 'nvm_check_global_modules cannot be tested because `npm` cannot install the `nop` package'31+message=$(nvm_check_global_modules)32+[ ! -z "$message" ] || {33+ puts '-- `npm --version`: '"$(npm --version)"34+ puts '-- `npm list -g`:'35+ npm list -g --depth=036+ puts '-- Printed message:'37+ puts "'''$message'''"38+39+ die "nvm_check_global_modules should have printed a notice when npm had global modules installed" ;}40+41+42+npm uninstall -g nop >/dev/null43+message=$(nvm_check_global_modules)44+[ -z "$message" ] || {45+ puts '-- `npm --version`: '"$(npm --version)"46+ puts '-- `npm list -g`:'47+ npm list -g --depth=048+ puts '-- Printed message:'49+ puts "'''$message'''"50+51+ die "nvm_check_global_modules should not have printed a notice when npm had no global modules installed" ;}52+53+54+# Faking an installation of npm55+mkdir -p "$npm_config_prefix/lib/node_modules/npm"56+cat <<'JSON' >"$npm_config_prefix/lib/node_modules/npm/package.json"57+{ "name": "npm", "version": "0.0.1fake" }58+JSON59+60+message=$(nvm_check_global_modules)61+[ -z "$message" ] || {62+ puts '-- `which npm`: ' "$(which npm)"63+ puts '-- `npm --version`: ' "$(npm --version)"64+ puts '-- `npm list -g`:'65+ npm list -g --depth=066+ puts '-- Printed message:'67+ puts "'''$message'''"68+69+ die "nvm_check_global_modules should not have printed a notice when npm had only itself installed as a global module" ;}70+71+72+# Faking the absence of npm73+PATH=".:$PATH"74+touch npm75+chmod +x npm76+77+message=$(nvm_check_global_modules)78+[ -z "$message" ] || {79+ puts '-- `which npm`: ' "$(which npm)"80+ puts '-- `npm --version`: ' "$(npm --version)"81+ puts '-- Printed message:'82+ puts "'''$message'''"83+84+ die "nvm_check_global_modules should not have printed a notice when npm was unavailable" ;}85+86+87+cleanupmodified:.travis.yml+1 added-1 removed
3 unmodified lines456788910111227 unmodified lines3 unmodified lines - (mkdir /tmp/urchin && cd /tmp/urchin && curl -s "$(curl -s https://registry.npmjs.com/urchin | grep -Eo '"tarball":\s*"[^"]+"' | tail -n 1 | awk -F\" '{ print $4 }')" -O && tar -x -f urchin*) - 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-$SHELLenv: - SHELL=sh TEST_SUITE=install_script - SHELL=dash TEST_SUITE=install_script - SHELL=bash TEST_SUITE=install_script27 unmodified linesmodified:install.sh+81 added-3 removed
1 #!/bin/bash2-2 set -e3 4+puts() (IFS=" "; printf %s\\n "$*" ;)5+error() (IFS=" "; printf %s\\n "$*" >&2 ;)6+debug() (IFS=" "; printf %s\\n ".. $*" >&2 ;)7+8+if [ "$DEBUG" = 'nvm:*' ] || [ "$DEBUG" = 'nvm:install' ]; then9+ NVM_DEBUG=010+ debug 'Script debugging enabled (in: `install.sh`).'11+fi12+13+14 nvm_has() {15 type "$1" > /dev/null 2>&116 }17 32 unmodified lines50 }51 52 nvm_download() {53 if nvm_has "curl"; then54+ [ "$NVM_DEBUG" = 0 ] && set +x55 curl $*56+ [ "$NVM_DEBUG" = 0 ] && set -x57 elif nvm_has "wget"; then58 # Emulate curl with wget59 ARGS=$(echo "$*" | command sed -e 's/--progress-bar /--progress=bar /' \60 -e 's/-L //' \61 -e 's/-I /--server-response /' \62 -e 's/-s /-q /' \63 -e 's/-o /-O /' \64 -e 's/-C - /-c /')65+ [ "$NVM_DEBUG" = 0 ] && set +x66 wget $ARGS67+ [ "$NVM_DEBUG" = 0 ] && set +x68 fi69 }70 71 install_nvm_from_git() {6 unmodified lines78 else79 # Cloning to $NVM_DIR80 echo "=> Downloading nvm from git to '$NVM_DIR'"81 printf "\r=> "82+ [ "$NVM_DEBUG" = 0 ] && set +x83 mkdir -p "$NVM_DIR"84 command git clone "$(nvm_source git)" "$NVM_DIR"85+ [ "$NVM_DEBUG" = 0 ] && set -x86 fi72- cd "$NVM_DIR" && command git checkout --quiet $(nvm_latest_version) && command git branch --quiet -D master >/dev/null 2>&187+ 88+ [ "$NVM_DEBUG" = 0 ] && set +x89+ cd "$NVM_DIR" || return $?90+ command git checkout --quiet $(nvm_latest_version) || return $?91+ command git branch --quiet -D master >/dev/null 2>&1 || return $?92+ [ "$NVM_DEBUG" = 0 ] && set +x93+ 94 return95 }96 97 install_nvm_as_script() {42 unmodified lines140 echo "$HOME/.profile"141 fi142 }143 144+#145+# Check whether the user has any globally-installed npm modules in their system146+# Node, and warn them if so.147+#148+nvm_check_global_modules() {149+ command -v npm >/dev/null 2>&1 || return 0150+151+ local NPM_VERSION152+ NPM_VERSION="$(npm --version)"153+ NPM_VERSION="${NPM_VERSION:--1}"154+ [ "$NVM_DEBUG" = 0 ] && debug "NPM detected (at version ${NPM_VERSION}.)"155+ [ "${NPM_VERSION%%[!-0-9]*}" -gt 0 ] || return 0156+157+ local NPM_GLOBAL_MODULES158+ NPM_GLOBAL_MODULES="$(159+ npm list -g --depth=0 |160+ sed '/ npm@/d' |161+ sed '/ (empty)$/d'162+ )"163+164+ local MODULE_COUNT165+ MODULE_COUNT="$(166+ printf %s\\n "$NPM_GLOBAL_MODULES" |167+ sed -ne '1!p' | # Remove the first line168+ wc -l | tr -d ' ' # Count entries169+ )"170+ [ "$NVM_DEBUG" = 0 ] && {171+ debug "(${MODULE_COUNT}) global modules detected:"172+ error "$NPM_GLOBAL_MODULES" ;}173+174+ if [ $MODULE_COUNT -ne 0 ]; then175+ cat <<-'END_MESSAGE'176+ => You currently have modules installed globally with `npm`. These will no177+ => longer be linked to the active version of Node when you install a new node178+ => with `nvm`; and they may (depending on how you construct your `$PATH`)179+ => override the binaries of modules installed with `nvm`:180+181+ END_MESSAGE182+ printf %s\\n "$NPM_GLOBAL_MODULES"183+ cat <<-'END_MESSAGE'184+185+ => If you wish to uninstall them at a later point (or re-install them under your186+ => `nvm` Nodes), you can remove them from the system Node as follows:187+188+ $ nvm use system189+ $ npm uninstall -g a_module190+191+ END_MESSAGE192+ fi193+}194+195 nvm_do_install() {196 if [ -z "$METHOD" ]; then197 # Autodetect install method198 if nvm_has "git"; then41 unmodified lines240 echo "=> Source string already in $NVM_PROFILE"241 fi242 fi243 244+ nvm_check_global_modules245+246 echo "=> Close and reopen your terminal to start using nvm"247 nvm_reset248 }249 1 unmodified line251 # Unsets the various functions defined252 # during the execution of the install script253 #254 nvm_reset() {181- unset -f nvm_do_install nvm_has nvm_download install_nvm_as_script install_nvm_from_git nvm_reset nvm_detect_profile nvm_latest_version255+ unset -f puts error debug \256+ nvm_reset nvm_has nvm_latest_version \257+ nvm_source nvm_download install_nvm_as_script install_nvm_from_git \258+ nvm_detect_profile nvm_check_global_modules nvm_do_install 259+ unset NPM_DEBUG260 }261 262 [ "_$NVM_ENV" = "_testing" ] || nvm_do_install