|
1 | 1 | #!/usr/bin/env bash
|
2 |
| - |
3 |
| -set -o xtrace # Write all commands first to stderr |
4 | 2 | set -o errexit # Exit the script with error if any of the commands fail
|
5 | 3 |
|
6 |
| -NODE_BINDINGS_PATH="${PROJECT_DIRECTORY}/bindings/node" |
7 |
| -NODE_ARTIFACTS_PATH="${NODE_BINDINGS_PATH}/node-artifacts" |
8 |
| -NPM_CACHE_DIR="${NODE_ARTIFACTS_PATH}/npm" |
9 |
| -NPM_TMP_DIR="${NODE_ARTIFACTS_PATH}/tmp" |
10 |
| -BIN_DIR="$(pwd)/bin" |
11 |
| - |
12 |
| -NVM_WINDOWS_URL="https://github.com/coreybutler/nvm-windows/releases/download/1.1.9/nvm-noinstall.zip" |
13 |
| -NVM_URL="https://raw.githubusercontent.com/creationix/nvm/v0.38.0/install.sh" |
14 |
| - |
15 |
| -# this needs to be explicitly exported for the nvm install below |
16 |
| -export NVM_DIR="${NODE_ARTIFACTS_PATH}/nvm" |
17 |
| - |
18 |
| -# create node artifacts path if needed |
19 |
| -mkdir -p "${NODE_ARTIFACTS_PATH}" |
20 |
| -mkdir -p "${NPM_CACHE_DIR}" |
21 |
| -mkdir -p "${NPM_TMP_DIR}" |
22 |
| -mkdir -p "${BIN_DIR}" |
| 4 | +NODE_LTS_NAME=${NODE_LTS_NAME:-fermium} |
| 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 |
23 | 48 |
|
24 |
| -export NVM_DIR="${NODE_ARTIFACTS_PATH}/nvm" |
25 |
| -mkdir -p "${NVM_DIR}" |
| 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 |
26 | 64 |
|
27 |
| -# install Node.js |
28 |
| -if [ "$OS" == "Windows_NT" ]; then |
29 |
| - set +o xtrace |
| 65 | +file_extension="tar.gz" |
| 66 | +if [[ "$OS" = "Windows_NT" ]]; then file_extension="zip"; fi |
30 | 67 |
|
31 |
| - export NVM_HOME=`cygpath -w "$NVM_DIR"` |
32 |
| - export NVM_SYMLINK=`cygpath -w "$NODE_ARTIFACTS_PATH/bin"` |
33 |
| - export PATH=`cygpath $NVM_SYMLINK`:`cygpath $NVM_HOME`:$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}" |
34 | 72 |
|
35 |
| - # download and install nvm |
36 |
| - curl -L $NVM_WINDOWS_URL -o nvm.zip |
37 |
| - unzip -d $NVM_DIR nvm.zip |
38 |
| - rm nvm.zip |
| 73 | +echo "Node.js ${node_index_version} for ${operating_system}-${architecture} released on ${node_index_date}" |
39 | 74 |
|
40 |
| - chmod 777 $NVM_DIR |
41 |
| - chmod -R a+rx $NVM_DIR |
| 75 | +set -o xtrace |
42 | 76 |
|
43 |
| - cat <<EOT > $NVM_DIR/settings.txt |
44 |
| -root: $NVM_HOME |
45 |
| -path: $NVM_SYMLINK |
46 |
| -EOT |
| 77 | +curl "${CURL_FLAGS[@]}" "${node_download_url}" --output "$node_archive_path" |
47 | 78 |
|
48 |
| - echo "Running: nvm install lts" |
49 |
| - nvm install lts |
50 |
| - echo "Running: nvm use lts" |
51 |
| - nvm use lts |
52 |
| - echo "Running: npm install -g [email protected]" |
53 |
| - npm install -g [email protected] # https://github.com/npm/cli/issues/4341 |
54 |
| - set -o xtrace |
| 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" |
55 | 87 | else
|
56 |
| - set +o xtrace |
| 88 | + tar -xf "$node_archive_path" -C "${NODE_ARTIFACTS_PATH}" |
| 89 | + mv "${NODE_ARTIFACTS_PATH}/${node_directory}" "${NODE_ARTIFACTS_PATH}/nodejs" |
| 90 | +fi |
57 | 91 |
|
58 |
| - echo " Downloading nvm" |
59 |
| - curl -o- $NVM_URL | bash |
60 |
| - [ -s "${NVM_DIR}/nvm.sh" ] && source "${NVM_DIR}/nvm.sh" |
| 92 | +export PATH="$NODE_ARTIFACTS_PATH/npm_global/bin:$NODE_ARTIFACTS_PATH/nodejs/bin:$PATH" |
| 93 | +hash -r |
61 | 94 |
|
62 |
| - echo "Running: nvm install --lts --latest-npm" |
63 |
| - nvm install --lts --latest-npm |
64 |
| - echo "Running: nvm use --lts" |
65 |
| - nvm use --lts |
| 95 | +# Set npm -g prefix to our local artifacts directory |
| 96 | +cat <<EOT > .npmrc |
| 97 | +prefix=$NODE_ARTIFACTS_PATH/npm_global |
| 98 | +EOT |
66 | 99 |
|
67 |
| - set -o xtrace |
| 100 | +if [[ $operating_system != "win" ]]; then |
| 101 | + # Update npm to latest when we can |
| 102 | + npm install --global npm@latest |
| 103 | + hash -r |
68 | 104 | fi
|
69 | 105 |
|
70 |
| -# setup npm cache in a local directory |
71 |
| -cat <<EOT > .npmrc |
72 |
| -devdir=${NPM_CACHE_DIR}/.node-gyp |
73 |
| -init-module=${NPM_CACHE_DIR}/.npm-init.js |
74 |
| -cache=${NPM_CACHE_DIR} |
75 |
| -tmp=${NPM_TMP_DIR} |
76 |
| -EOT |
| 106 | +echo "npm version: $(npm -v)" |
77 | 107 |
|
78 |
| -# install node dependencies |
79 |
| -npm install |
| 108 | +npm install "${NPM_OPTIONS}" |
0 commit comments