···11+function _nvm_version_deactivate --argument-names ver
22+ test "$nvm_current_version" = "$ver" && set --erase nvm_current_version
33+ set --local index (contains --index -- $nvm_data/$ver/bin $PATH) &&
44+ set --erase PATH[$index]
55+end
···11+function nvm --description "Node version manager"
22+ for silent in --silent -s
33+ if set --local index (contains --index -- $silent $argv)
44+ set --erase argv[$index] && break
55+ end
66+ set --erase silent
77+ end
88+99+ set --local cmd $argv[1]
1010+ set --local ver $argv[2]
1111+1212+ if set --query silent && ! set --query cmd[1]
1313+ echo "nvm: Version number not specified (see nvm -h for usage)" >&2
1414+ return 1
1515+ end
1616+1717+ if ! set --query ver[1] && contains -- "$cmd" install use
1818+ for file in .nvmrc .node-version
1919+ set file (_nvm_find_up $PWD $file) && read ver <$file && break
2020+ end
2121+2222+ if ! set --query ver[1]
2323+ echo "nvm: Invalid version or missing \".nvmrc\" file" >&2
2424+ return 1
2525+ end
2626+ end
2727+2828+ set --local their_version $ver
2929+3030+ switch "$cmd"
3131+ case -v --version
3232+ echo "nvm, version 2.2.13"
3333+ case "" -h --help
3434+ echo "Usage: nvm install <version> Download and activate the specified Node version"
3535+ echo " nvm install Install the version specified in the nearest .nvmrc file"
3636+ echo " nvm use <version> Activate the specified Node version in the current shell"
3737+ echo " nvm use Activate the version specified in the nearest .nvmrc file"
3838+ echo " nvm list List installed Node versions"
3939+ echo " nvm list-remote List available Node versions to install"
4040+ echo " nvm list-remote <regex> List Node versions matching a given regex pattern"
4141+ echo " nvm current Print the currently-active Node version"
4242+ echo " nvm uninstall <version> Uninstall the specified Node version"
4343+ echo "Options:"
4444+ echo " -s, --silent Suppress standard output"
4545+ echo " -v, --version Print the version of nvm"
4646+ echo " -h, --help Print this help message"
4747+ echo "Variables:"
4848+ echo " nvm_arch Override architecture, e.g. x64-musl"
4949+ echo " nvm_mirror Use a mirror for downloading Node binaries"
5050+ echo " nvm_default_version Set the default version for new shells"
5151+ echo " nvm_default_packages Install a list of packages every time a Node version is installed"
5252+ echo "Examples:"
5353+ echo " nvm install latest Install the latest version of Node"
5454+ echo " nvm use 14.15.1 Use Node version 14.15.1"
5555+ echo " nvm use system Activate the system's Node version"
5656+5757+ case install
5858+ _nvm_index_update
5959+6060+ string match --entire --regex -- (_nvm_version_match $ver) <$nvm_data/.index | read ver alias
6161+6262+ if ! set --query ver[1]
6363+ echo "nvm: Invalid version number or alias: \"$their_version\"" >&2
6464+ return 1
6565+ end
6666+6767+ if test ! -e $nvm_data/$ver
6868+ set --local os (command uname -s | string lower)
6969+ set --local ext tar.gz
7070+ set --local arch (command uname -m)
7171+7272+ switch $os
7373+ case aix
7474+ set arch ppc64
7575+ case sunos
7676+ case linux
7777+ case darwin
7878+ case {MSYS_NT,MINGW\*_NT}\*
7979+ set os win
8080+ set ext zip
8181+ case \*
8282+ echo "nvm: Unsupported operating system: \"$os\"" >&2
8383+ return 1
8484+ end
8585+8686+ switch $arch
8787+ case i\*86
8888+ set arch x86
8989+ case x86_64
9090+ set arch x64
9191+ case arm64
9292+ string match --regex --quiet "v(?<major>\d+)" $ver
9393+ if test "$os" = darwin -a $major -lt 16
9494+ set arch x64
9595+ end
9696+ case armv6 armv6l
9797+ set arch armv6l
9898+ case armv7 armv7l
9999+ set arch armv7l
100100+ case armv8 armv8l aarch64
101101+ set arch arm64
102102+ end
103103+104104+ set --query nvm_arch && set arch $nvm_arch
105105+106106+ set --local dir "node-$ver-$os-$arch"
107107+ set --local url $nvm_mirror/$ver/$dir.$ext
108108+109109+ command mkdir -p $nvm_data/$ver
110110+111111+ if ! set --query silent
112112+ echo -e "Installing Node \x1b[1m$ver\x1b[22m $alias"
113113+ echo -e "Fetching \x1b[4m$url\x1b[24m\x1b[7m"
114114+ end
115115+116116+ if ! command curl $silent --progress-bar --location $url |
117117+ command tar --extract --gzip --directory $nvm_data/$ver 2>/dev/null
118118+ command rm -rf $nvm_data/$ver
119119+ echo -e "\033[F\33[2K\x1b[0mnvm: Invalid mirror or host unavailable: \"$url\"" >&2
120120+ return 1
121121+ end
122122+123123+ set --query silent || echo -en "\033[F\33[2K\x1b[0m"
124124+125125+ if test "$os" = win
126126+ command mv $nvm_data/$ver/$dir $nvm_data/$ver/bin
127127+ else
128128+ command mv $nvm_data/$ver/$dir/* $nvm_data/$ver
129129+ command rm -rf $nvm_data/$ver/$dir
130130+ end
131131+ end
132132+133133+ if test $ver != "$nvm_current_version"
134134+ set --query nvm_current_version && _nvm_version_deactivate $nvm_current_version
135135+ _nvm_version_activate $ver
136136+137137+ set --query nvm_default_packages[1] && npm install --global $silent $nvm_default_packages
138138+ end
139139+140140+ set --query silent || printf "Now using Node %s (npm %s) %s\n" (_nvm_node_info)
141141+ case use
142142+ test $ver = default && set ver $nvm_default_version
143143+ _nvm_list | string match --entire --regex -- (_nvm_version_match $ver) | read ver __
144144+145145+ if ! set --query ver[1]
146146+ echo "nvm: Can't use Node \"$their_version\", version must be installed first" >&2
147147+ return 1
148148+ end
149149+150150+ if test $ver != "$nvm_current_version"
151151+ set --query nvm_current_version && _nvm_version_deactivate $nvm_current_version
152152+ test $ver != system && _nvm_version_activate $ver
153153+ end
154154+155155+ set --query silent || printf "Now using Node %s (npm %s) %s\n" (_nvm_node_info)
156156+ case uninstall
157157+ if test -z "$ver"
158158+ echo "nvm: Not enough arguments for command: \"$cmd\"" >&2
159159+ return 1
160160+ end
161161+162162+ test $ver = default && test ! -z "$nvm_default_version" && set ver $nvm_default_version
163163+164164+ _nvm_list | string match --entire --regex -- (_nvm_version_match $ver) | read ver __
165165+166166+ if ! set -q ver[1]
167167+ echo "nvm: Node version not installed or invalid: \"$their_version\"" >&2
168168+ return 1
169169+ end
170170+171171+ set --query silent || printf "Uninstalling Node %s %s\n" $ver (string replace ~ \~ "$nvm_data/$ver/bin/node")
172172+173173+ _nvm_version_deactivate $ver
174174+175175+ command rm -rf $nvm_data/$ver
176176+ case current
177177+ _nvm_current
178178+ case ls list
179179+ _nvm_list | _nvm_list_format (_nvm_current) $argv[2]
180180+ case lsr {ls,list}-remote
181181+ _nvm_index_update || return
182182+ _nvm_list | command awk '
183183+ FILENAME == "-" && (is_local[$1] = FNR == NR) { next } {
184184+ print $0 (is_local[$1] ? " ✓" : "")
185185+ }
186186+ ' - $nvm_data/.index | _nvm_list_format (_nvm_current) $argv[2]
187187+ case \*
188188+ echo "nvm: Unknown command or option: \"$cmd\" (see nvm -h for usage)" >&2
189189+ return 1
190190+ end
191191+end
192192+193193+function _nvm_find_up --argument-names path file
194194+ test -e "$path/$file" && echo $path/$file || begin
195195+ test ! -z "$path" || return
196196+ _nvm_find_up (string replace --regex -- '/[^/]*$' "" $path) $file
197197+ end
198198+end
199199+200200+function _nvm_version_match --argument-names ver
201201+ string replace --regex -- '^v?(\d+|\d+\.\d+)$' 'v$1.' $ver |
202202+ string replace --filter --regex -- '^v?(\d+)' 'v$1' |
203203+ string escape --style=regex || string lower '\b'$ver'(?:/\w+)?$'
204204+end
205205+206206+function _nvm_list_format --argument-names current regex
207207+ command awk -v current="$current" -v regex="$regex" '
208208+ $0 ~ regex {
209209+ aliases[versions[i++] = $1] = $2 " " $3
210210+ pad = (n = length($1)) > pad ? n : pad
211211+ }
212212+ END {
213213+ if (!i) exit 1
214214+ while (i--)
215215+ printf((current == versions[i] ? " ▶ " : " ") "%"pad"s %s\n",
216216+ versions[i], aliases[versions[i]])
217217+ }
218218+ '
219219+end
220220+221221+function _nvm_current
222222+ command --search --quiet node || return
223223+ set --query nvm_current_version && echo $nvm_current_version || echo system
224224+end
225225+226226+function _nvm_node_info
227227+ set --local npm_path (string replace bin/npm-cli.js "" (realpath (command --search npm)))
228228+ test -f $npm_path/package.json || set --local npm_version_default (command npm --version)
229229+ command node --eval "
230230+ console.log(process.version)
231231+ console.log('$npm_version_default' ? '$npm_version_default': require('$npm_path/package.json').version)
232232+ console.log(process.execPath)
233233+ " | string replace -- ~ \~
234234+end