blob: 50185405907491d80d9e242f910d52dd18deec1a [file] [log] [blame]
Koen Punt3c4bf802012-10-30 00:03:01 +01001#!/bin/bash
2
Koen Punt0787a552013-12-22 17:46:26 +01003set -e
4
Jordan Harband7e269962014-07-05 13:42:44 -07005nvm_has() {
Koen Punt30002262014-03-15 04:32:35 +01006 type "$1" > /dev/null 2>&1
Koen Punt30002262014-03-15 04:32:35 +01007}
Koen Puntd2422a62013-12-22 18:23:59 +01008
Koen Punt30002262014-03-15 04:32:35 +01009if [ -z "$NVM_DIR" ]; then
Koen Puntd2422a62013-12-22 18:23:59 +010010 NVM_DIR="$HOME/.nvm"
11fi
Koen Punt3c4bf802012-10-30 00:03:01 +010012
Jordan Harband8e45afb2014-12-28 15:57:18 -080013nvm_latest_version() {
Jordan Harband0f1f3ed2015-02-02 20:26:00 -080014 echo "v0.23.3"
Jordan Harband8e45afb2014-12-28 15:57:18 -080015}
16
Xavier Cambar516e5532014-10-31 13:37:59 +010017#
18# Outputs the location to NVM depending on:
19# * The availability of $NVM_SOURCE
20# * The method used ("script" or "git" in the script, defaults to "git")
21# NVM_SOURCE always takes precedence
22#
23nvm_source() {
24 local NVM_METHOD
25 NVM_METHOD="$1"
26 if [ -z "$NVM_SOURCE" ]; then
27 local NVM_SOURCE
Jordan Harbandfd2fb242014-12-28 15:54:09 -080028 if [ "_$NVM_METHOD" = "_script" ]; then
Jordan Harband8e45afb2014-12-28 15:57:18 -080029 NVM_SOURCE="https://raw.githubusercontent.com/creationix/nvm/$(nvm_latest_version)/nvm.sh"
Jordan Harbandfd2fb242014-12-28 15:54:09 -080030 elif [ "_$NVM_METHOD" = "_script-nvm-exec" ]; then
Jordan Harband8e45afb2014-12-28 15:57:18 -080031 NVM_SOURCE="https://raw.githubusercontent.com/creationix/nvm/$(nvm_latest_version)/nvm-exec"
Jordan Harbandfd2fb242014-12-28 15:54:09 -080032 elif [ "_$NVM_METHOD" = "_git" ] || [ -z "$NVM_METHOD" ]; then
33 NVM_SOURCE="https://github.com/creationix/nvm.git"
34 else
35 echo >&2 "Unexpected value \"$NVM_METHOD\" for \$NVM_METHOD"
36 return 1
37 fi
Xavier Cambar516e5532014-10-31 13:37:59 +010038 fi
39 echo "$NVM_SOURCE"
40 return 0
41}
42
Jordan Harband63f72b32014-07-07 15:40:59 -070043nvm_download() {
Jordan Harband74cc1eb2014-07-07 10:04:20 -070044 if nvm_has "curl"; then
Jordan Harband25c0be12014-07-05 13:51:13 -070045 curl $*
Jordan Harband74cc1eb2014-07-07 10:04:20 -070046 elif nvm_has "wget"; then
Koen Punt5342b6a2014-03-26 09:29:05 +010047 # Emulate curl with wget
Jordan Harband3fc82d62015-01-09 01:50:05 -080048 ARGS=$(echo "$*" | command sed -e 's/--progress-bar /--progress=bar /' \
Koen Punt9c2127c2014-07-18 16:18:17 +020049 -e 's/-L //' \
Jordan Harband708ac802014-08-15 20:21:46 -070050 -e 's/-I /--server-response /' \
Koen Punt9c2127c2014-07-18 16:18:17 +020051 -e 's/-s /-q /' \
52 -e 's/-o /-O /' \
53 -e 's/-C - /-c /')
Jordan Harband74cc1eb2014-07-07 10:04:20 -070054 wget $ARGS
Koen Punt30002262014-03-15 04:32:35 +010055 fi
Jordan Harband74cc1eb2014-07-07 10:04:20 -070056}
Dennis Dryden34a06762013-06-07 23:42:28 +010057
Jordan Harbandcce5df32014-07-05 13:44:00 -070058install_nvm_from_git() {
Koen Punt30002262014-03-15 04:32:35 +010059 if [ -d "$NVM_DIR/.git" ]; then
Jordan Harband2d9494a2015-01-22 10:21:04 -080060 echo "=> nvm is already installed in $NVM_DIR, trying to update using git"
Jordan Harbandb6f1c152014-06-26 10:26:57 -070061 printf "\r=> "
Jordan Harbandbf794ff2015-01-11 11:53:24 -080062 cd "$NVM_DIR" && (command git fetch 2> /dev/null || {
Jordan Harband55d892a2014-07-17 00:19:01 -070063 echo >&2 "Failed to update nvm, run 'git fetch' in $NVM_DIR yourself." && exit 1
Jordan Harbande0537ce2014-07-05 13:47:22 -070064 })
Koen Punt30002262014-03-15 04:32:35 +010065 else
66 # Cloning to $NVM_DIR
Koen Punt6ed93f42014-03-26 09:08:37 +010067 echo "=> Downloading nvm from git to '$NVM_DIR'"
Jordan Harbandb6f1c152014-06-26 10:26:57 -070068 printf "\r=> "
Koen Punt30002262014-03-15 04:32:35 +010069 mkdir -p "$NVM_DIR"
Jordan Harbandbf794ff2015-01-11 11:53:24 -080070 command git clone "$(nvm_source git)" "$NVM_DIR"
Koen Punt30002262014-03-15 04:32:35 +010071 fi
Jordan Harbandbf794ff2015-01-11 11:53:24 -080072 cd "$NVM_DIR" && command git checkout --quiet $(nvm_latest_version) && command git branch --quiet -D master >/dev/null 2>&1
Jordan Harband5ad00f12014-09-25 22:10:48 -070073 return
Koen Punt30002262014-03-15 04:32:35 +010074}
75
Jordan Harbandcce5df32014-07-05 13:44:00 -070076install_nvm_as_script() {
Xavier Cambar516e5532014-10-31 13:37:59 +010077 local NVM_SOURCE
Jordan Harbandfd2fb242014-12-28 15:54:09 -080078 NVM_SOURCE=$(nvm_source script)
Jordan Harband689c52c2014-11-22 10:31:42 -080079 local NVM_EXEC_SOURCE
Jordan Harbandfd2fb242014-12-28 15:54:09 -080080 NVM_EXEC_SOURCE=$(nvm_source script-nvm-exec)
Koen Punt30002262014-03-15 04:32:35 +010081
82 # Downloading to $NVM_DIR
Koen Puntd2422a62013-12-22 18:23:59 +010083 mkdir -p "$NVM_DIR"
Koen Punt30002262014-03-15 04:32:35 +010084 if [ -d "$NVM_DIR/nvm.sh" ]; then
Jordan Harband2d9494a2015-01-22 10:21:04 -080085 echo "=> nvm is already installed in $NVM_DIR, trying to update the script"
Koen Punt30002262014-03-15 04:32:35 +010086 else
Koen Punt6ed93f42014-03-26 09:08:37 +010087 echo "=> Downloading nvm as script to '$NVM_DIR'"
Koen Punt30002262014-03-15 04:32:35 +010088 fi
Jordan Harband5904d412014-11-22 10:29:48 -080089 nvm_download -s "$NVM_SOURCE" -o "$NVM_DIR/nvm.sh" || {
Jordan Harband689c52c2014-11-22 10:31:42 -080090 echo >&2 "Failed to download '$NVM_SOURCE'"
Koen Punt30002262014-03-15 04:32:35 +010091 return 1
92 }
Jordan Harband689c52c2014-11-22 10:31:42 -080093 nvm_download -s "$NVM_EXEC_SOURCE" -o "$NVM_DIR/nvm-exec" || {
94 echo >&2 "Failed to download '$NVM_EXEC_SOURCE'"
95 return 2
96 }
97 chmod a+x "$NVM_DIR/nvm-exec" || {
98 echo >&2 "Failed to mark '$NVM_DIR/nvm-exec' as executable"
99 return 3
100 }
Koen Punt30002262014-03-15 04:32:35 +0100101}
102
Xavier Cambarb9f15b02014-10-29 13:05:11 +0100103#
104# Detect profile file if not specified as environment variable
105# (eg: PROFILE=~/.myprofile)
106# The echo'ed path is guaranteed to be an existing file
107# Otherwise, an empty string is returned
108#
109nvm_detect_profile() {
110 if [ -f "$PROFILE" ]; then
111 echo "$PROFILE"
112 elif [ -f "$HOME/.bashrc" ]; then
113 echo "$HOME/.bashrc"
114 elif [ -f "$HOME/.bash_profile" ]; then
115 echo "$HOME/.bash_profile"
116 elif [ -f "$HOME/.zshrc" ]; then
117 echo "$HOME/.zshrc"
118 elif [ -f "$HOME/.profile" ]; then
119 echo "$HOME/.profile"
120 fi
121}
122
elliottcable4ba7ee52015-01-23 20:38:50 -0600123#
124# Check whether the user has any globally-installed npm modules in their system
125# Node, and warn them if so.
126#
127nvm_check_global_modules() {
elliottcable6cfc3092015-02-02 20:42:12 -0600128 command -v npm >/dev/null 2>&1 || return 0
129
130 local NPM_VERSION
131 NPM_VERSION="$(npm --version)"
132 NPM_VERSION="${NPM_VERSION:-0}"
133 [ "${NPM_VERSION%%[!0-9]*}" -gt 1 ] || return 0
134
elliottcable4ba7ee52015-01-23 20:38:50 -0600135 local NPM_GLOBAL_MODULES
136 NPM_GLOBAL_MODULES=$(npm list -g --depth=0 | sed '/ npm@/d')
137
138 local MODULE_COUNT
139 MODULE_COUNT=$(
140 printf %s\\n "$NPM_GLOBAL_MODULES" |
141 sed -ne '1!p' | # Remove the first line
142 wc -l | tr -d ' ' # Count entries
143 )
144
145 if [ $MODULE_COUNT -ne 0 ]; then
146 cat <<-'END_MESSAGE'
147 => You currently have modules installed globally with `npm`. These will no
148 => longer be linked to the active version of Node when you install a new node
149 => with `nvm`; and they may (depending on how you construct your `$PATH`)
150 => override the binaries of modules installed with `nvm`:
151
152 END_MESSAGE
153 printf %s\\n "$NPM_GLOBAL_MODULES"
154 cat <<-'END_MESSAGE'
155
156 => If you wish to uninstall them at a later point (or re-install them under your
157 => `nvm` Nodes), you can remove them from the system Node as follows:
158
159 $ nvm use system
160 $ npm uninstall -g a_module
161
162 END_MESSAGE
163 fi
164}
165
Xavier Cambar3cdec8e2014-10-22 19:43:39 +0200166nvm_do_install() {
167 if [ -z "$METHOD" ]; then
168 # Autodetect install method
169 if nvm_has "git"; then
170 install_nvm_from_git
171 elif nvm_has "nvm_download"; then
172 install_nvm_as_script
173 else
174 echo >&2 "You need git, curl, or wget to install nvm"
175 exit 1
176 fi
177 elif [ "~$METHOD" = "~git" ]; then
178 if ! nvm_has "git"; then
179 echo >&2 "You need git to install nvm"
180 exit 1
181 fi
Jordan Harbandcce5df32014-07-05 13:44:00 -0700182 install_nvm_from_git
Xavier Cambar3cdec8e2014-10-22 19:43:39 +0200183 elif [ "~$METHOD" = "~script" ]; then
184 if ! nvm_has "nvm_download"; then
185 echo >&2 "You need curl or wget to install nvm"
186 exit 1
187 fi
Jordan Harbandcce5df32014-07-05 13:44:00 -0700188 install_nvm_as_script
Koen Punt2d0c0252014-03-26 09:16:26 +0100189 fi
Koen Punt3c4bf802012-10-30 00:03:01 +0100190
Antti Vähäkotamäki81d731d2013-07-26 14:58:47 +0300191 echo
Koen Punt3c4bf802012-10-30 00:03:01 +0100192
Xavier Cambarb9f15b02014-10-29 13:05:11 +0100193 local NVM_PROFILE
194 NVM_PROFILE=$(nvm_detect_profile)
Xavier Cambar3cdec8e2014-10-22 19:43:39 +0200195
196 SOURCE_STR="\nexport NVM_DIR=\"$NVM_DIR\"\n[ -s \"\$NVM_DIR/nvm.sh\" ] && . \"\$NVM_DIR/nvm.sh\" # This loads nvm"
197
Xavier Cambarb9f15b02014-10-29 13:05:11 +0100198 if [ -z "$NVM_PROFILE" ] ; then
199 echo "=> Profile not found. Tried $NVM_PROFILE (as defined in \$PROFILE), ~/.bashrc, ~/.bash_profile, ~/.zshrc, and ~/.profile."
200 echo "=> Create one of them and run this script again"
201 echo "=> Create it (touch $NVM_PROFILE) and run this script again"
Xavier Cambar3cdec8e2014-10-22 19:43:39 +0200202 echo " OR"
203 echo "=> Append the following lines to the correct file yourself:"
204 printf "$SOURCE_STR"
205 echo
206 else
Xavier Cambarb9f15b02014-10-29 13:05:11 +0100207 if ! grep -qc 'nvm.sh' "$NVM_PROFILE"; then
208 echo "=> Appending source string to $NVM_PROFILE"
209 printf "$SOURCE_STR\n" >> "$NVM_PROFILE"
Xavier Cambar3cdec8e2014-10-22 19:43:39 +0200210 else
Xavier Cambarb9f15b02014-10-29 13:05:11 +0100211 echo "=> Source string already in $NVM_PROFILE"
Xavier Cambar3cdec8e2014-10-22 19:43:39 +0200212 fi
213 fi
214
elliottcable4ba7ee52015-01-23 20:38:50 -0600215 nvm_check_global_modules
216
Xavier Cambar3cdec8e2014-10-22 19:43:39 +0200217 echo "=> Close and reopen your terminal to start using nvm"
218 nvm_reset
219}
220
221#
222# Unsets the various functions defined
223# during the execution of the install script
224#
225nvm_reset() {
elliottcabledd1a9ca2015-01-29 21:23:16 -0600226 unset -f nvm_reset nvm_has nvm_latest_version \
227 nvm_source nvm_download install_nvm_as_script install_nvm_from_git \
228 nvm_detect_profile nvm_check_global_modules nvm_do_install
Xavier Cambar3cdec8e2014-10-22 19:43:39 +0200229}
230
231[ "_$NVM_ENV" = "_testing" ] || nvm_do_install