2020-01-14 22:54:24 +08:00
|
|
|
# Helper functions
|
|
|
|
|
2020-09-24 07:33:03 +08:00
|
|
|
# When in China, set $NODEJS_ORG_MIRROR:
|
|
|
|
# export NODEJS_ORG_MIRROR=https://npm.taobao.org/mirrors/node/
|
|
|
|
NODEJS_ORG_MIRROR="${NODEJS_ORG_MIRROR:-https://nodejs.org/dist/}"
|
|
|
|
if [ ${NODEJS_ORG_MIRROR: -1} != / ]
|
|
|
|
then
|
|
|
|
NODEJS_ORG_MIRROR=$NODEJS_ORG_MIRROR/
|
|
|
|
fi
|
|
|
|
|
2021-11-24 08:23:30 +08:00
|
|
|
export ASDF_NODEJS_PLUGIN_DIR=$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)
|
|
|
|
|
2020-01-14 22:54:24 +08:00
|
|
|
# TODO: Replace with an asdf variable once asdf starts providing the plugin name
|
|
|
|
# as a variable
|
2021-11-24 08:23:30 +08:00
|
|
|
export ASDF_NODEJS_PLUGIN_NAME=$(basename "$ASDF_NODEJS_PLUGIN_DIR")
|
2020-01-14 22:54:24 +08:00
|
|
|
plugin_name() {
|
2021-05-17 08:06:10 +08:00
|
|
|
printf "%s\n" "$ASDF_NODEJS_PLUGIN_NAME"
|
2020-01-14 22:54:24 +08:00
|
|
|
}
|
2020-05-12 18:35:14 +08:00
|
|
|
|
2021-12-24 11:40:45 +08:00
|
|
|
asdf_data_dir() {
|
|
|
|
printf "%s\n" "${ASDF_DATA_DIR:-$HOME/.asdf}"
|
|
|
|
}
|
|
|
|
|
|
|
|
export ASDF_NODEJS_CACHE_DIR="$(asdf_data_dir)/tmp/$ASDF_NODEJS_PLUGIN_NAME/cache"
|
2021-05-17 08:06:10 +08:00
|
|
|
|
2021-11-30 02:00:39 +08:00
|
|
|
# Colors
|
|
|
|
colored() {
|
|
|
|
local color="$1" text="$2"
|
|
|
|
printf "\033[%sm%s\033[39;49m\n" "$color" "$text"
|
|
|
|
}
|
|
|
|
|
|
|
|
export RED=31 GREEN=32 YELLOW=33 BLUE=34 MAGENTA=35 CYAN=36
|
|
|
|
|
2020-09-24 07:33:03 +08:00
|
|
|
die() {
|
|
|
|
>&2 echo "$@"
|
|
|
|
exit 1
|
|
|
|
}
|
|
|
|
|
2021-01-22 04:37:23 +08:00
|
|
|
# Tab file needs to be piped as stdin
|
2020-09-24 07:33:03 +08:00
|
|
|
# Print all alias and correspondent versions in the format "$alias\t$version"
|
2021-01-22 04:37:23 +08:00
|
|
|
# Also prints versions as a alias of itself. Eg: "v10.0.0\tv10.0.0"
|
2020-09-24 07:33:03 +08:00
|
|
|
filter_version_candidates() {
|
2021-02-03 00:43:11 +08:00
|
|
|
local curr_line="" aliases=""
|
2020-09-24 07:33:03 +08:00
|
|
|
|
2021-01-22 04:37:23 +08:00
|
|
|
# Skip headers
|
|
|
|
IFS= read -r curr_line
|
2020-09-24 07:33:03 +08:00
|
|
|
|
2021-01-22 04:37:23 +08:00
|
|
|
while IFS= read -r curr_line; do
|
|
|
|
# Just expanding the string should work because tabs are considered array separators
|
|
|
|
local -a fields=($curr_line)
|
2020-09-24 07:33:03 +08:00
|
|
|
|
2021-01-22 04:37:23 +08:00
|
|
|
# Version without `v` prefix
|
|
|
|
local version="${fields[0]#v}"
|
2022-03-12 01:48:07 +08:00
|
|
|
# Lowercase lts codename, `-` if not a lts version
|
|
|
|
local lts_codename=$(echo "${fields[9]}" | tr '[:upper:]' '[:lower:]')
|
|
|
|
|
|
|
|
if [ "$lts_codename" != - ]; then
|
|
|
|
# No lts read yet, so this must be the more recent
|
|
|
|
if ! grep -q "^lts:" <<< "$aliases"; then
|
|
|
|
printf "lts\t%s\n" "$version"
|
|
|
|
aliases="$aliases"$'\n'"lts:$version"
|
2021-01-22 04:37:23 +08:00
|
|
|
fi
|
2020-09-24 07:33:03 +08:00
|
|
|
|
2022-03-12 01:48:07 +08:00
|
|
|
# No lts read for this codename yet, so this must be the more recent
|
|
|
|
if ! grep -q "^$lts_codename:" <<< "$aliases"; then
|
|
|
|
printf "lts-$lts_codename\t%s\n" "$version"
|
|
|
|
aliases="$aliases"$'\n'"$lts_codename:$version"
|
|
|
|
fi
|
2022-03-11 22:25:24 +08:00
|
|
|
fi
|
2022-03-12 01:48:07 +08:00
|
|
|
|
|
|
|
printf "%s\t%s\n" "$version" "$version"
|
2021-01-22 04:37:23 +08:00
|
|
|
done
|
2020-09-24 07:33:03 +08:00
|
|
|
}
|
2021-01-22 04:37:23 +08:00
|
|
|
|
2021-11-24 08:23:30 +08:00
|
|
|
versions_cache_dir="$ASDF_NODEJS_CACHE_DIR/versions-tab"
|
2021-04-07 22:56:03 +08:00
|
|
|
mkdir -p "$versions_cache_dir"
|
|
|
|
|
|
|
|
etag_file="$versions_cache_dir/etag"
|
|
|
|
index_file="$versions_cache_dir/index"
|
|
|
|
touch "$etag_file" "$index_file"
|
|
|
|
|
|
|
|
print_index_tab(){
|
|
|
|
local temp_headers_file="$(mktemp)"
|
|
|
|
|
|
|
|
if [ -f "$etag_file" ]; then
|
|
|
|
etag_flag='--header If-None-Match:'"$(cat "$etag_file")"
|
|
|
|
fi
|
|
|
|
|
|
|
|
index="$(curl --fail --silent --dump-header "$temp_headers_file" $etag_flag "${NODEJS_ORG_MIRROR}index.tab")"
|
|
|
|
if [ -z "$index" ]; then
|
|
|
|
cat "$index_file"
|
|
|
|
else
|
|
|
|
cat "$temp_headers_file" | awk 'tolower($1) == "etag:" { print $2 }' > "$etag_file"
|
2022-03-12 01:48:07 +08:00
|
|
|
echo "$index" | filter_version_candidates > "$index_file"
|
2021-10-09 06:11:40 +08:00
|
|
|
cat "$index_file"
|
2021-04-07 22:56:03 +08:00
|
|
|
fi
|
|
|
|
|
|
|
|
rm "$temp_headers_file"
|
|
|
|
}
|