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