blob: 125e6515be714e941678edb73b88e0381f748a68 [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() {
128 local NPM_GLOBAL_MODULES
129 NPM_GLOBAL_MODULES=$(npm list -g --depth=0 | sed '/ npm@/d')
130
131 local MODULE_COUNT
132 MODULE_COUNT=$(
133 printf %s\\n "$NPM_GLOBAL_MODULES" |
134 sed -ne '1!p' | # Remove the first line
135 wc -l | tr -d ' ' # Count entries
136 )
137
138 if [ $MODULE_COUNT -ne 0 ]; then
139 cat <<-'END_MESSAGE'
140 => You currently have modules installed globally with `npm`. These will no
141 => longer be linked to the active version of Node when you install a new node
142 => with `nvm`; and they may (depending on how you construct your `$PATH`)
143 => override the binaries of modules installed with `nvm`:
144
145 END_MESSAGE
146 printf %s\\n "$NPM_GLOBAL_MODULES"
147 cat <<-'END_MESSAGE'
148
149 => If you wish to uninstall them at a later point (or re-install them under your
150 => `nvm` Nodes), you can remove them from the system Node as follows:
151
152 $ nvm use system
153 $ npm uninstall -g a_module
154
155 END_MESSAGE
156 fi
157}
158
Xavier Cambar3cdec8e2014-10-22 19:43:39 +0200159nvm_do_install() {
160 if [ -z "$METHOD" ]; then
161 # Autodetect install method
162 if nvm_has "git"; then
163 install_nvm_from_git
164 elif nvm_has "nvm_download"; then
165 install_nvm_as_script
166 else
167 echo >&2 "You need git, curl, or wget to install nvm"
168 exit 1
169 fi
170 elif [ "~$METHOD" = "~git" ]; then
171 if ! nvm_has "git"; then
172 echo >&2 "You need git to install nvm"
173 exit 1
174 fi
Jordan Harbandcce5df32014-07-05 13:44:00 -0700175 install_nvm_from_git
Xavier Cambar3cdec8e2014-10-22 19:43:39 +0200176 elif [ "~$METHOD" = "~script" ]; then
177 if ! nvm_has "nvm_download"; then
178 echo >&2 "You need curl or wget to install nvm"
179 exit 1
180 fi
Jordan Harbandcce5df32014-07-05 13:44:00 -0700181 install_nvm_as_script
Koen Punt2d0c0252014-03-26 09:16:26 +0100182 fi
Koen Punt3c4bf802012-10-30 00:03:01 +0100183
Antti Vähäkotamäki81d731d2013-07-26 14:58:47 +0300184 echo
Koen Punt3c4bf802012-10-30 00:03:01 +0100185
Xavier Cambarb9f15b02014-10-29 13:05:11 +0100186 local NVM_PROFILE
187 NVM_PROFILE=$(nvm_detect_profile)
Xavier Cambar3cdec8e2014-10-22 19:43:39 +0200188
189 SOURCE_STR="\nexport NVM_DIR=\"$NVM_DIR\"\n[ -s \"\$NVM_DIR/nvm.sh\" ] && . \"\$NVM_DIR/nvm.sh\" # This loads nvm"
190
Xavier Cambarb9f15b02014-10-29 13:05:11 +0100191 if [ -z "$NVM_PROFILE" ] ; then
192 echo "=> Profile not found. Tried $NVM_PROFILE (as defined in \$PROFILE), ~/.bashrc, ~/.bash_profile, ~/.zshrc, and ~/.profile."
193 echo "=> Create one of them and run this script again"
194 echo "=> Create it (touch $NVM_PROFILE) and run this script again"
Xavier Cambar3cdec8e2014-10-22 19:43:39 +0200195 echo " OR"
196 echo "=> Append the following lines to the correct file yourself:"
197 printf "$SOURCE_STR"
198 echo
199 else
Xavier Cambarb9f15b02014-10-29 13:05:11 +0100200 if ! grep -qc 'nvm.sh' "$NVM_PROFILE"; then
201 echo "=> Appending source string to $NVM_PROFILE"
202 printf "$SOURCE_STR\n" >> "$NVM_PROFILE"
Xavier Cambar3cdec8e2014-10-22 19:43:39 +0200203 else
Xavier Cambarb9f15b02014-10-29 13:05:11 +0100204 echo "=> Source string already in $NVM_PROFILE"
Xavier Cambar3cdec8e2014-10-22 19:43:39 +0200205 fi
206 fi
207
elliottcable4ba7ee52015-01-23 20:38:50 -0600208 nvm_check_global_modules
209
Xavier Cambar3cdec8e2014-10-22 19:43:39 +0200210 echo "=> Close and reopen your terminal to start using nvm"
211 nvm_reset
212}
213
214#
215# Unsets the various functions defined
216# during the execution of the install script
217#
218nvm_reset() {
elliottcabledd1a9ca2015-01-29 21:23:16 -0600219 unset -f nvm_reset nvm_has nvm_latest_version \
220 nvm_source nvm_download install_nvm_as_script install_nvm_from_git \
221 nvm_detect_profile nvm_check_global_modules nvm_do_install
Xavier Cambar3cdec8e2014-10-22 19:43:39 +0200222}
223
224[ "_$NVM_ENV" = "_testing" ] || nvm_do_install