2015-05-24 12:26:04 +08:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
|
2021-11-24 10:08:14 +08:00
|
|
|
set -eu -o pipefail
|
2017-02-13 03:44:21 +08:00
|
|
|
|
2020-01-14 22:54:24 +08:00
|
|
|
# shellcheck source=../lib/utils.sh
|
2021-11-24 10:08:14 +08:00
|
|
|
source "$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/../lib/utils.sh"
|
2020-10-02 23:33:24 +08:00
|
|
|
|
2015-05-24 12:26:04 +08:00
|
|
|
install_nodejs() {
|
2021-11-24 10:08:14 +08:00
|
|
|
local install_type="$1" version_query="$2" install_path="$3"
|
|
|
|
local version=
|
2020-10-03 00:11:26 +08:00
|
|
|
|
2021-11-24 10:08:14 +08:00
|
|
|
version="$(resolve_version_query "$version_query")"
|
2015-05-24 12:26:04 +08:00
|
|
|
|
2020-10-02 23:33:24 +08:00
|
|
|
if [ "$version" != "$version_query" ]; then
|
2021-11-24 10:08:14 +08:00
|
|
|
install_aliased_version "$version" "$version_query" "$install_path"
|
2020-10-03 00:11:26 +08:00
|
|
|
else
|
2021-11-24 10:08:14 +08:00
|
|
|
install_canon_version "$install_type" "$version" "$install_path"
|
2020-10-03 00:11:26 +08:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-11-24 10:08:14 +08:00
|
|
|
ensure_nodebuild_updated() {
|
|
|
|
"$ASDF_NODEJS_PLUGIN_DIR/lib/commands/command-update-nodebuild.bash"
|
|
|
|
}
|
2020-10-03 00:11:26 +08:00
|
|
|
|
2021-10-30 09:40:13 +08:00
|
|
|
|
2021-11-24 10:08:14 +08:00
|
|
|
nodebuild_wrapped() {
|
|
|
|
"$ASDF_NODEJS_PLUGIN_DIR/lib/commands/command-nodebuild.bash" "$@"
|
|
|
|
}
|
2021-10-30 09:40:13 +08:00
|
|
|
|
2020-10-03 00:11:26 +08:00
|
|
|
|
2021-11-24 10:08:14 +08:00
|
|
|
install_canon_version() {
|
|
|
|
local install_type="$1" version="$2" install_path="$3"
|
|
|
|
local nodebuild_args=()
|
2021-10-30 09:40:13 +08:00
|
|
|
|
2021-11-24 10:08:14 +08:00
|
|
|
if [ "$install_type" = ref ]; then
|
|
|
|
nodebuild_args+=(-c)
|
2021-10-30 09:40:13 +08:00
|
|
|
fi
|
2020-10-03 00:11:26 +08:00
|
|
|
|
2021-11-24 10:08:14 +08:00
|
|
|
ensure_nodebuild_updated
|
2021-11-24 10:35:25 +08:00
|
|
|
|
|
|
|
NODE_BUILD_BUILD_PATH="$ASDF_DOWNLOAD_PATH" NODE_BUILD_CACHE_PATH="$ASDF_DOWNLOAD_PATH" \
|
2021-11-24 10:08:14 +08:00
|
|
|
nodebuild_wrapped -v "${nodebuild_args[@]}" "$version" "$install_path"
|
2020-10-03 00:11:26 +08:00
|
|
|
}
|
|
|
|
|
2021-11-24 10:08:14 +08:00
|
|
|
|
2020-10-03 00:11:26 +08:00
|
|
|
install_aliased_version() {
|
|
|
|
local version=$1
|
|
|
|
local version_query=$2
|
|
|
|
local install_path=$3
|
|
|
|
|
|
|
|
# install the true version and only symlink it to the alias
|
|
|
|
>&2 echo "Installing alias $version_query as $version"
|
|
|
|
asdf install "$(plugin_name)" "$version" \
|
|
|
|
|| die "Could not install version $version"
|
|
|
|
|
|
|
|
if [ -L "$install_path" ]; then
|
|
|
|
rm "$install_path"
|
|
|
|
else
|
|
|
|
rmdir "$install_path"
|
2020-10-02 23:33:24 +08:00
|
|
|
fi
|
2020-10-03 00:11:26 +08:00
|
|
|
|
|
|
|
>&2 echo "Linking \"$version_query\" to \"$version\""
|
2021-04-07 22:56:03 +08:00
|
|
|
ln -s "$(asdf where "$(plugin_name)" "$version")" "$install_path"
|
2015-05-24 12:26:04 +08:00
|
|
|
}
|
|
|
|
|
2020-09-24 08:09:50 +08:00
|
|
|
|
2020-09-24 07:33:03 +08:00
|
|
|
resolve_version_query() {
|
|
|
|
local version_query="$1"
|
2020-10-03 00:11:26 +08:00
|
|
|
|
|
|
|
local canon_version="$(
|
2020-09-24 08:09:50 +08:00
|
|
|
# Find the first candidate which the alias match, then print it version
|
2020-09-24 07:33:03 +08:00
|
|
|
print_index_tab \
|
2021-10-07 20:15:13 +08:00
|
|
|
| awk -F'\t' -v "alias=$version_query" '$1 == alias { print $2; exit }'
|
2020-09-24 07:33:03 +08:00
|
|
|
)"
|
|
|
|
|
2020-10-03 00:11:26 +08:00
|
|
|
if [ -z "$canon_version" ]; then
|
2020-09-24 07:33:03 +08:00
|
|
|
echo "$version_query"
|
2020-10-03 00:11:26 +08:00
|
|
|
else
|
|
|
|
echo "$canon_version"
|
2020-09-24 07:33:03 +08:00
|
|
|
fi
|
|
|
|
}
|
2015-05-24 12:26:04 +08:00
|
|
|
|
2020-09-24 08:09:50 +08:00
|
|
|
|
2017-07-31 12:37:03 +08:00
|
|
|
install_default_npm_packages() {
|
2020-08-03 02:27:53 +08:00
|
|
|
local default_npm_packages="${ASDF_NPM_DEFAULT_PACKAGES_FILE:=$HOME/.default-npm-packages}"
|
2017-07-31 12:37:03 +08:00
|
|
|
|
2019-05-03 19:16:52 +08:00
|
|
|
if [ ! -f "$default_npm_packages" ]; then return; fi
|
2017-07-31 12:37:03 +08:00
|
|
|
|
2019-05-03 19:16:52 +08:00
|
|
|
cat "$default_npm_packages" | while read -r name; do
|
2019-05-03 19:14:59 +08:00
|
|
|
echo -ne "\nInstalling \033[33m${name}\033[39m npm package... "
|
2020-05-08 21:01:03 +08:00
|
|
|
source "$(dirname "$0")/exec-env"
|
2019-05-03 19:16:52 +08:00
|
|
|
PATH="$ASDF_INSTALL_PATH/bin:$PATH" npm install -g "$name" > /dev/null 2>&1 && rc=$? || rc=$?
|
|
|
|
if [[ $rc -eq 0 ]]; then
|
2019-05-03 19:14:59 +08:00
|
|
|
echo -e "\033[32mSUCCESS\033[39m"
|
2017-07-31 12:37:03 +08:00
|
|
|
else
|
2019-05-03 19:14:59 +08:00
|
|
|
echo -e "\033[31mFAIL\033[39m"
|
2017-07-31 12:37:03 +08:00
|
|
|
fi
|
|
|
|
done
|
|
|
|
}
|
2017-02-13 03:44:21 +08:00
|
|
|
|
2021-11-24 10:08:14 +08:00
|
|
|
install_nodejs "$ASDF_INSTALL_TYPE" "$ASDF_INSTALL_VERSION" "$ASDF_INSTALL_PATH"
|
|
|
|
install_default_npm_packages
|
2020-01-14 22:54:24 +08:00
|
|
|
|
|
|
|
asdf reshim "$(plugin_name)" "$ASDF_INSTALL_VERSION"
|