| 1 | function nvm --description "Node version manager" |
| 2 | for silent in --silent -s |
| 3 | if set --local index (contains --index -- $silent $argv) |
| 4 | set --erase argv[$index] && break |
| 5 | end |
| 6 | set --erase silent |
| 7 | end |
| 8 | |
| 9 | set --local cmd $argv[1] |
| 10 | set --local ver $argv[2] |
| 11 | |
| 12 | if set --query silent && ! set --query cmd[1] |
| 13 | echo "nvm: Version number not specified (see nvm -h for usage)" >&2 |
| 14 | return 1 |
| 15 | end |
| 16 | |
| 17 | if ! set --query ver[1] && contains -- "$cmd" install use |
| 18 | for file in .nvmrc .node-version |
| 19 | set file (_nvm_find_up $PWD $file) && read ver <$file && break |
| 20 | end |
| 21 | |
| 22 | if ! set --query ver[1] |
| 23 | echo "nvm: Invalid version or missing \".nvmrc\" file" >&2 |
| 24 | return 1 |
| 25 | end |
| 26 | end |
| 27 | |
| 28 | set --local their_version $ver |
| 29 | |
| 30 | switch "$cmd" |
| 31 | case -v --version |
| 32 | echo "nvm, version 2.2.13" |
| 33 | case "" -h --help |
| 34 | echo "Usage: nvm install <version> Download and activate the specified Node version" |
| 35 | echo " nvm install Install the version specified in the nearest .nvmrc file" |
| 36 | echo " nvm use <version> Activate the specified Node version in the current shell" |
| 37 | echo " nvm use Activate the version specified in the nearest .nvmrc file" |
| 38 | echo " nvm list List installed Node versions" |
| 39 | echo " nvm list-remote List available Node versions to install" |
| 40 | echo " nvm list-remote <regex> List Node versions matching a given regex pattern" |
| 41 | echo " nvm current Print the currently-active Node version" |
| 42 | echo " nvm uninstall <version> Uninstall the specified Node version" |
| 43 | echo "Options:" |
| 44 | echo " -s, --silent Suppress standard output" |
| 45 | echo " -v, --version Print the version of nvm" |
| 46 | echo " -h, --help Print this help message" |
| 47 | echo "Variables:" |
| 48 | echo " nvm_arch Override architecture, e.g. x64-musl" |
| 49 | echo " nvm_mirror Use a mirror for downloading Node binaries" |
| 50 | echo " nvm_default_version Set the default version for new shells" |
| 51 | echo " nvm_default_packages Install a list of packages every time a Node version is installed" |
| 52 | echo "Examples:" |
| 53 | echo " nvm install latest Install the latest version of Node" |
| 54 | echo " nvm use 14.15.1 Use Node version 14.15.1" |
| 55 | echo " nvm use system Activate the system's Node version" |
| 56 | |
| 57 | case install |
| 58 | _nvm_index_update |
| 59 | |
| 60 | string match --entire --regex -- (_nvm_version_match $ver) <$nvm_data/.index | read ver alias |
| 61 | |
| 62 | if ! set --query ver[1] |
| 63 | echo "nvm: Invalid version number or alias: \"$their_version\"" >&2 |
| 64 | return 1 |
| 65 | end |
| 66 | |
| 67 | if test ! -e $nvm_data/$ver |
| 68 | set --local os (command uname -s | string lower) |
| 69 | set --local ext tar.gz |
| 70 | set --local arch (command uname -m) |
| 71 | |
| 72 | switch $os |
| 73 | case aix |
| 74 | set arch ppc64 |
| 75 | case sunos |
| 76 | case linux |
| 77 | case darwin |
| 78 | case {MSYS_NT,MINGW\*_NT}\* |
| 79 | set os win |
| 80 | set ext zip |
| 81 | case \* |
| 82 | echo "nvm: Unsupported operating system: \"$os\"" >&2 |
| 83 | return 1 |
| 84 | end |
| 85 | |
| 86 | switch $arch |
| 87 | case i\*86 |
| 88 | set arch x86 |
| 89 | case x86_64 |
| 90 | set arch x64 |
| 91 | case arm64 |
| 92 | string match --regex --quiet "v(?<major>\d+)" $ver |
| 93 | if test "$os" = darwin -a $major -lt 16 |
| 94 | set arch x64 |
| 95 | end |
| 96 | case armv6 armv6l |
| 97 | set arch armv6l |
| 98 | case armv7 armv7l |
| 99 | set arch armv7l |
| 100 | case armv8 armv8l aarch64 |
| 101 | set arch arm64 |
| 102 | end |
| 103 | |
| 104 | set --query nvm_arch && set arch $nvm_arch |
| 105 | |
| 106 | set --local dir "node-$ver-$os-$arch" |
| 107 | set --local url $nvm_mirror/$ver/$dir.$ext |
| 108 | |
| 109 | command mkdir -p $nvm_data/$ver |
| 110 | |
| 111 | if ! set --query silent |
| 112 | echo -e "Installing Node \x1b[1m$ver\x1b[22m $alias" |
| 113 | echo -e "Fetching \x1b[4m$url\x1b[24m\x1b[7m" |
| 114 | end |
| 115 | |
| 116 | if ! command curl $silent --progress-bar --location $url | |
| 117 | command tar --extract --gzip --directory $nvm_data/$ver 2>/dev/null |
| 118 | command rm -rf $nvm_data/$ver |
| 119 | echo -e "\033[F\33[2K\x1b[0mnvm: Invalid mirror or host unavailable: \"$url\"" >&2 |
| 120 | return 1 |
| 121 | end |
| 122 | |
| 123 | set --query silent || echo -en "\033[F\33[2K\x1b[0m" |
| 124 | |
| 125 | if test "$os" = win |
| 126 | command mv $nvm_data/$ver/$dir $nvm_data/$ver/bin |
| 127 | else |
| 128 | command mv $nvm_data/$ver/$dir/* $nvm_data/$ver |
| 129 | command rm -rf $nvm_data/$ver/$dir |
| 130 | end |
| 131 | end |
| 132 | |
| 133 | if test $ver != "$nvm_current_version" |
| 134 | set --query nvm_current_version && _nvm_version_deactivate $nvm_current_version |
| 135 | _nvm_version_activate $ver |
| 136 | |
| 137 | set --query nvm_default_packages[1] && npm install --global $silent $nvm_default_packages |
| 138 | end |
| 139 | |
| 140 | set --query silent || printf "Now using Node %s (npm %s) %s\n" (_nvm_node_info) |
| 141 | case use |
| 142 | test $ver = default && set ver $nvm_default_version |
| 143 | _nvm_list | string match --entire --regex -- (_nvm_version_match $ver) | read ver __ |
| 144 | |
| 145 | if ! set --query ver[1] |
| 146 | echo "nvm: Can't use Node \"$their_version\", version must be installed first" >&2 |
| 147 | return 1 |
| 148 | end |
| 149 | |
| 150 | if test $ver != "$nvm_current_version" |
| 151 | set --query nvm_current_version && _nvm_version_deactivate $nvm_current_version |
| 152 | test $ver != system && _nvm_version_activate $ver |
| 153 | end |
| 154 | |
| 155 | set --query silent || printf "Now using Node %s (npm %s) %s\n" (_nvm_node_info) |
| 156 | case uninstall |
| 157 | if test -z "$ver" |
| 158 | echo "nvm: Not enough arguments for command: \"$cmd\"" >&2 |
| 159 | return 1 |
| 160 | end |
| 161 | |
| 162 | test $ver = default && test ! -z "$nvm_default_version" && set ver $nvm_default_version |
| 163 | |
| 164 | _nvm_list | string match --entire --regex -- (_nvm_version_match $ver) | read ver __ |
| 165 | |
| 166 | if ! set -q ver[1] |
| 167 | echo "nvm: Node version not installed or invalid: \"$their_version\"" >&2 |
| 168 | return 1 |
| 169 | end |
| 170 | |
| 171 | set --query silent || printf "Uninstalling Node %s %s\n" $ver (string replace ~ \~ "$nvm_data/$ver/bin/node") |
| 172 | |
| 173 | _nvm_version_deactivate $ver |
| 174 | |
| 175 | command rm -rf $nvm_data/$ver |
| 176 | case current |
| 177 | _nvm_current |
| 178 | case ls list |
| 179 | _nvm_list | _nvm_list_format (_nvm_current) $argv[2] |
| 180 | case lsr {ls,list}-remote |
| 181 | _nvm_index_update || return |
| 182 | _nvm_list | command awk ' |
| 183 | FILENAME == "-" && (is_local[$1] = FNR == NR) { next } { |
| 184 | print $0 (is_local[$1] ? " ✓" : "") |
| 185 | } |
| 186 | ' - $nvm_data/.index | _nvm_list_format (_nvm_current) $argv[2] |
| 187 | case \* |
| 188 | echo "nvm: Unknown command or option: \"$cmd\" (see nvm -h for usage)" >&2 |
| 189 | return 1 |
| 190 | end |
| 191 | end |
| 192 | |
| 193 | function _nvm_find_up --argument-names path file |
| 194 | test -e "$path/$file" && echo $path/$file || begin |
| 195 | test ! -z "$path" || return |
| 196 | _nvm_find_up (string replace --regex -- '/[^/]*$' "" $path) $file |
| 197 | end |
| 198 | end |
| 199 | |
| 200 | function _nvm_version_match --argument-names ver |
| 201 | string replace --regex -- '^v?(\d+|\d+\.\d+)$' 'v$1.' $ver | |
| 202 | string replace --filter --regex -- '^v?(\d+)' 'v$1' | |
| 203 | string escape --style=regex || string lower '\b'$ver'(?:/\w+)?$' |
| 204 | end |
| 205 | |
| 206 | function _nvm_list_format --argument-names current regex |
| 207 | command awk -v current="$current" -v regex="$regex" ' |
| 208 | $0 ~ regex { |
| 209 | aliases[versions[i++] = $1] = $2 " " $3 |
| 210 | pad = (n = length($1)) > pad ? n : pad |
| 211 | } |
| 212 | END { |
| 213 | if (!i) exit 1 |
| 214 | while (i--) |
| 215 | printf((current == versions[i] ? " ▶ " : " ") "%"pad"s %s\n", |
| 216 | versions[i], aliases[versions[i]]) |
| 217 | } |
| 218 | ' |
| 219 | end |
| 220 | |
| 221 | function _nvm_current |
| 222 | command --search --quiet node || return |
| 223 | set --query nvm_current_version && echo $nvm_current_version || echo system |
| 224 | end |
| 225 | |
| 226 | function _nvm_node_info |
| 227 | set --local npm_path (string replace bin/npm-cli.js "" (realpath (command --search npm))) |
| 228 | test -f $npm_path/package.json || set --local npm_version_default (command npm --version) |
| 229 | command node --eval " |
| 230 | console.log(process.version) |
| 231 | console.log('$npm_version_default' ? '$npm_version_default': require('$npm_path/package.json').version) |
| 232 | console.log(process.execPath) |
| 233 | " | string replace -- ~ \~ |
| 234 | end |