Skip to content
Myr

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

3 files+169 added−4 removed

added:test/install_script/nvm_check_global_modules+87 added
1#!/bin/sh2puts() (IFS=" "; printf %s\\n "$*" ;)34cleanup () {5 rm -rf "$npm_config_prefix/lib" >/dev/null 2>&16 unset npm_config_prefix78 rm -f npm9 PATH="$ORIGINAL_PATH"1011 unset -f setup cleanup die12 unset message ORIGINAL_PATH13}1415die () { puts "!! $@" ; cleanup ; exit 1; }1617NVM_ENV=testing . ../../install.sh1819setup () {20 ORIGINAL_PATH="$PATH"2122 npm_config_prefix="$(pwd)"23 export npm_config_prefix24 mkdir -p "$npm_config_prefix/lib"25}2627setup282930npm install -g nop >/dev/null || die 'nvm_check_global_modules cannot be tested because `npm` cannot install the `nop` package'31message=$(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'''"3839 die "nvm_check_global_modules should have printed a notice when npm had global modules installed" ;}404142npm uninstall -g nop >/dev/null43message=$(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'''"5051 die "nvm_check_global_modules should not have printed a notice when npm had no global modules installed" ;}525354# Faking an installation of npm55mkdir -p "$npm_config_prefix/lib/node_modules/npm"56cat <<'JSON' >"$npm_config_prefix/lib/node_modules/npm/package.json"57{ "name": "npm", "version": "0.0.1fake" }58JSON5960message=$(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'''"6869 die "nvm_check_global_modules should not have printed a notice when npm had only itself installed as a global module" ;}707172# Faking the absence of npm73PATH=".:$PATH"74touch npm75chmod +x npm7677message=$(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'''"8384 die "nvm_check_global_modules should not have printed a notice when npm was unavailable" ;}858687cleanup
modified:.travis.yml+1 added-1 removed
modified:install.sh+81 added-3 removed
1#!/bin/bash22set -e34puts() (IFS=" "; printf %s\\n "$*" ;)5error() (IFS=" "; printf %s\\n "$*" >&2 ;)6debug() (IFS=" "; printf %s\\n ".. $*" >&2 ;)78if [ "$DEBUG" = 'nvm:*' ] || [ "$DEBUG" = 'nvm:install' ]; then9 NVM_DEBUG=010 debug 'Script debugging enabled (in: `install.sh`).'11fi121314nvm_has() {15 type "$1" > /dev/null 2>&116}17
32 unmodified lines
50}5152nvm_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}7071install_nvm_from_git() {
6 unmodified lines
78 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}9697install_nvm_as_script() {
42 unmodified lines
140 echo "$HOME/.profile"141 fi142}143144#145# Check whether the user has any globally-installed npm modules in their system146# Node, and warn them if so.147#148nvm_check_global_modules() {149 command -v npm >/dev/null 2>&1 || return 0150151 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 0156157 local NPM_GLOBAL_MODULES158 NPM_GLOBAL_MODULES="$(159 npm list -g --depth=0 |160 sed '/ npm@/d' |161 sed '/ (empty)$/d'162 )"163164 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" ;}173174 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`:180181 END_MESSAGE182 printf %s\\n "$NPM_GLOBAL_MODULES"183 cat <<-'END_MESSAGE'184185 => 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:187188 $ nvm use system189 $ npm uninstall -g a_module190191 END_MESSAGE192 fi193}194195nvm_do_install() {196 if [ -z "$METHOD" ]; then197 # Autodetect install method198 if nvm_has "git"; then
41 unmodified lines
240 echo "=> Source string already in $NVM_PROFILE"241 fi242 fi243244 nvm_check_global_modules245246 echo "=> Close and reopen your terminal to start using nvm"247 nvm_reset248}249
1 unmodified line
251# Unsets the various functions defined252# during the execution of the install script253#254nvm_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}261262[ "_$NVM_ENV" = "_testing" ] || nvm_do_install