|
1 |
| -#!/bin/bash |
| 1 | +#!/usr/bin/env bash |
2 | 2 | set -o errexit # Exit the script with error if any of the commands fail
|
3 | 3 |
|
4 |
| -NVM_WINDOWS_URL="https://github.com/coreybutler/nvm-windows/releases/download/1.1.9/nvm-noinstall.zip" |
5 |
| -NVM_URL="https://raw.githubusercontent.com/nvm-sh/nvm/v0.38.0/install.sh" |
6 |
| - |
7 |
| -NODE_LTS_NAME=${NODE_LTS_NAME:-fermium} |
8 |
| -NODE_ARTIFACTS_PATH="${PROJECT_DIRECTORY}/node-artifacts" |
9 |
| - |
10 |
| -# this needs to be explicitly exported for the nvm install below |
11 |
| -export NVM_DIR="${NODE_ARTIFACTS_PATH}/nvm" |
12 |
| -export XDG_CONFIG_HOME=${NODE_ARTIFACTS_PATH} |
13 |
| - |
14 |
| -# create node artifacts path if needed |
15 |
| -mkdir -p "${NODE_ARTIFACTS_PATH}" |
16 |
| - |
17 |
| -function node_lts_to_version() { |
18 |
| - case $1 in |
19 |
| - "erbium") |
20 |
| - echo 12 |
21 |
| - ;; |
22 |
| - "fermium") |
23 |
| - echo 14 |
24 |
| - ;; |
25 |
| - "gallium") |
26 |
| - echo 16 |
27 |
| - ;; |
28 |
| - "hydrogen") |
29 |
| - echo 18 |
30 |
| - ;; |
31 |
| - "iron") |
32 |
| - echo 20 |
33 |
| - ;; |
34 |
| - "latest") |
35 |
| - echo 'latest' |
36 |
| - ;; |
37 |
| - *) |
38 |
| - echo "Unsupported Node LTS version $1" |
39 |
| - ;; |
40 |
| - esac |
41 |
| -} |
42 |
| - |
43 |
| -function latest_version_for_node_major() { |
44 |
| - local __NODE_MAJOR_VERSION=$1 |
45 |
| - local NODE_DOWNLOAD_URI="https://nodejs.org/download/release/latest-v${__NODE_MAJOR_VERSION}.x/SHASUMS256.txt" |
46 |
| - |
47 |
| - if [ $__NODE_MAJOR_VERSION == 'latest' ] |
48 |
| - then |
49 |
| - NODE_DOWNLOAD_URI="https://nodejs.org/download/release/latest/SHASUMS256.txt" |
50 |
| - fi |
51 |
| - |
52 |
| - # check that the requested version does exist |
53 |
| - curl --silent --fail $NODE_DOWNLOAD_URI &> /dev/null |
54 |
| - |
55 |
| - echo $(curl --retry 8 --retry-delay 5 --max-time 50 --silent -o- $NODE_DOWNLOAD_URI | head -n 1 | awk '{print $2};' | cut -d- -f2) |
56 |
| -} |
57 |
| - |
58 |
| -NODE_MAJOR_VERSION=$(node_lts_to_version $NODE_LTS_NAME) |
59 |
| -NODE_VERSION=$(latest_version_for_node_major $NODE_MAJOR_VERSION) |
60 |
| -NODE_VERSION=${NODE_VERSION:1} # :1 gets rid of the leading 'v' |
61 |
| - |
62 |
| -echo "set version to $NODE_VERSION" |
63 |
| - |
64 |
| -# output node version to expansions file for use in subsequent scripts |
65 |
| -cat <<EOT > deps-expansion.yml |
66 |
| - NODE_VERSION: "$NODE_VERSION" |
67 |
| -EOT |
| 4 | +NODE_LTS_NAME=${NODE_LTS_NAME:-erbium} |
| 5 | +NODE_ARTIFACTS_PATH="${PROJECT_DIRECTORY:-$(pwd)}/node-artifacts" |
| 6 | +if [[ "$OS" = "Windows_NT" ]]; then NODE_ARTIFACTS_PATH=$(cygpath --unix "$NODE_ARTIFACTS_PATH"); fi |
| 7 | + |
| 8 | +CURL_FLAGS=( |
| 9 | + --fail # Exit code 1 if request fails |
| 10 | + --compressed # Request a compressed response should keep fetching fast |
| 11 | + --location # Follow a redirect |
| 12 | + --retry 8 # Retry HTTP 408, 429, 500, 502, 503 or 504, 8 times |
| 13 | + --silent # Do not print a progress bar |
| 14 | + --show-error # Despite the silent flag still print out errors |
| 15 | + --max-time 900 # 900 seconds is 15 minutes, evergreen times out at 20 |
| 16 | + --continue-at - # If a download is interrupted it can figure out where to resume |
| 17 | +) |
| 18 | + |
| 19 | +mkdir -p "$NODE_ARTIFACTS_PATH/npm_global" |
| 20 | + |
| 21 | +# Comparisons are all case insensitive |
| 22 | +shopt -s nocasematch |
| 23 | + |
| 24 | +# index.tab is a sorted tab separated values file with the following headers |
| 25 | +# 0 1 2 3 4 5 6 7 8 9 10 |
| 26 | +# version date files npm v8 uv zlib openssl modules lts security |
| 27 | +curl "${CURL_FLAGS[@]}" "https://nodejs.org/dist/index.tab" --output node_index.tab |
| 28 | + |
| 29 | +while IFS=$'\t' read -r -a row; do |
| 30 | + node_index_version="${row[0]}" |
| 31 | + node_index_date="${row[1]}" |
| 32 | + node_index_lts="${row[9]}" |
| 33 | + [[ "$node_index_version" = "version" ]] && continue # skip tsv header |
| 34 | + [[ "$NODE_LTS_NAME" = "latest" ]] && break # first line is latest |
| 35 | + [[ "$NODE_LTS_NAME" = "$node_index_lts" ]] && break # case insensitive compare |
| 36 | +done < node_index.tab |
| 37 | + |
| 38 | +if [[ "$OS" = "Windows_NT" ]]; then |
| 39 | + operating_system="win" |
| 40 | +elif [[ $(uname) = "darwin" ]]; then |
| 41 | + operating_system="darwin" |
| 42 | +elif [[ $(uname) = "linux" ]]; then |
| 43 | + operating_system="linux" |
| 44 | +else |
| 45 | + echo "Unable to determine operating system: $operating_system" |
| 46 | + exit 1 |
| 47 | +fi |
68 | 48 |
|
69 |
| -# install Node.js on Windows |
70 |
| -if [[ "$OS" == "Windows_NT" ]]; then |
71 |
| - # Delete pre-existing node to avoid version conflicts |
72 |
| - rm -rf "/cygdrive/c/Program Files/nodejs" |
| 49 | +architecture=$(uname -m) |
| 50 | +if [[ $architecture = "x86_64" ]]; then |
| 51 | + architecture="x64" |
| 52 | +elif [[ $architecture = "arm64" ]]; then |
| 53 | + architecture="arm64" |
| 54 | +elif [[ $architecture = "aarch64" ]]; then |
| 55 | + architecture="arm64" |
| 56 | +elif [[ $architecture == s390* ]]; then |
| 57 | + architecture="s390x" |
| 58 | +elif [[ $architecture == ppc* ]]; then |
| 59 | + architecture="ppc64le" |
| 60 | +else |
| 61 | + echo "Unable to determine operating system: $architecture" |
| 62 | + exit 1 |
| 63 | +fi |
73 | 64 |
|
| 65 | +file_extension="tar.gz" |
| 66 | +if [[ "$OS" = "Windows_NT" ]]; then file_extension="zip"; fi |
74 | 67 |
|
75 |
| - NVM_HOME=$(cygpath -w "$NVM_DIR") |
76 |
| - export NVM_HOME |
77 |
| - NVM_SYMLINK=$(cygpath -w "$NODE_ARTIFACTS_PATH/bin") |
78 |
| - export NVM_SYMLINK |
79 |
| - NVM_ARTIFACTS_PATH=$(cygpath -w "$NODE_ARTIFACTS_PATH/bin") |
80 |
| - export NVM_ARTIFACTS_PATH |
81 |
| - PATH=$(cygpath $NVM_SYMLINK):$(cygpath $NVM_HOME):$PATH |
82 |
| - export PATH |
| 68 | +node_directory="node-${node_index_version}-${operating_system}-${architecture}" |
| 69 | +node_archive="${node_directory}.${file_extension}" |
| 70 | +node_archive_path="$NODE_ARTIFACTS_PATH/${node_archive}" |
| 71 | +node_download_url="https://nodejs.org/dist/${node_index_version}/${node_archive}" |
83 | 72 |
|
84 |
| - curl -L $NVM_WINDOWS_URL -o nvm.zip |
85 |
| - unzip -d "$NVM_DIR" nvm.zip |
86 |
| - rm nvm.zip |
| 73 | +echo "Node.js ${node_index_version} for ${operating_system}-${architecture} released on ${node_index_date}" |
87 | 74 |
|
88 |
| - chmod 777 "$NVM_DIR" |
89 |
| - chmod -R a+rx "$NVM_DIR" |
| 75 | +set -o xtrace |
90 | 76 |
|
91 |
| - cat <<EOT > "$NVM_DIR/settings.txt" |
92 |
| -root: $NVM_HOME |
93 |
| -path: $NVM_SYMLINK |
94 |
| -EOT |
95 |
| - nvm install "$NODE_VERSION" |
96 |
| - nvm use "$NODE_VERSION" |
97 |
| - which node || echo "node not found, PATH=$PATH" |
98 |
| - which npm || echo "npm not found, PATH=$PATH" |
99 |
| - npm cache clear --force # Fixes: Cannot read properties of null (reading 'pickAlgorithm') error on windows |
| 77 | +curl "${CURL_FLAGS[@]}" "${node_download_url}" --output "$node_archive_path" |
100 | 78 |
|
101 |
| -# install Node.js on Linux/MacOS |
| 79 | +if [[ "$file_extension" = "zip" ]]; then |
| 80 | + unzip -q "$node_archive_path" -d "${NODE_ARTIFACTS_PATH}" |
| 81 | + mkdir -p "${NODE_ARTIFACTS_PATH}/nodejs" |
| 82 | + # Windows "bins" are at the top level |
| 83 | + mv "${NODE_ARTIFACTS_PATH}/${node_directory}" "${NODE_ARTIFACTS_PATH}/nodejs/bin" |
| 84 | + # Need to add executable flag ourselves |
| 85 | + chmod +x "${NODE_ARTIFACTS_PATH}/nodejs/bin/node.exe" |
| 86 | + chmod +x "${NODE_ARTIFACTS_PATH}/nodejs/bin/npm" |
102 | 87 | else
|
103 |
| - curl -o- $NVM_URL | bash |
104 |
| - [ -s "${NVM_DIR}/nvm.sh" ] && source "${NVM_DIR}/nvm.sh" |
105 |
| - nvm install --no-progress "$NODE_VERSION" |
| 88 | + tar -xf "$node_archive_path" -C "${NODE_ARTIFACTS_PATH}" |
| 89 | + mv "${NODE_ARTIFACTS_PATH}/${node_directory}" "${NODE_ARTIFACTS_PATH}/nodejs" |
106 | 90 | fi
|
107 | 91 |
|
108 |
| -npm install ${NPM_OPTIONS} |
| 92 | +export PATH="$NODE_ARTIFACTS_PATH/npm_global/bin:$NODE_ARTIFACTS_PATH/nodejs/bin:$PATH" |
| 93 | +hash -r |
| 94 | + |
| 95 | +# Set npm -g prefix to our local artifacts directory |
| 96 | +cat <<EOT > .npmrc |
| 97 | +prefix=$NODE_ARTIFACTS_PATH/npm_global |
| 98 | +EOT |
| 99 | + |
| 100 | +# Cannot upgrade npm version for node 12 |
| 101 | +if [[ $operating_system != "win" ]] && [[ $NODE_LTS_NAME != "erbium" ]]; then |
| 102 | + # Update npm to latest when we can |
| 103 | + npm install --global npm@latest |
| 104 | + hash -r |
| 105 | +elif [[ $NODE_LTS_NAME == "erbium" ]]; then |
| 106 | + # Node.js 12 can run up to npm v8 |
| 107 | + npm install --global npm@8 |
| 108 | + hash -r |
| 109 | +fi |
| 110 | + |
| 111 | +echo "npm version: $(npm -v)" |
| 112 | + |
| 113 | +npm install "${NPM_OPTIONS}" |
0 commit comments