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-12-14 09:50:58 +08:00
|
|
|
source "$(dirname "$0")/../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-30 02:00:39 +08:00
|
|
|
try_to_update_nodebuild() {
|
|
|
|
local exit_code=0
|
|
|
|
|
|
|
|
"$ASDF_NODEJS_PLUGIN_DIR/lib/commands/command-update-nodebuild.bash" 2> /dev/null || exit_code=$?
|
|
|
|
|
|
|
|
if [ "$exit_code" != 0 ]; then
|
|
|
|
printf "
|
2021-12-07 11:03:28 +08:00
|
|
|
$(colored $YELLOW WARNING): Updating node-build failed with exit code %s. The installation will
|
2021-11-30 02:00:39 +08:00
|
|
|
try to continue with already installed local defintions. To debug what went
|
|
|
|
wrong try to manually updating node-build by running: \`asdf %s update nodebuild\`
|
2021-12-07 11:03:28 +08:00
|
|
|
\n" "$exit_code" "$ASDF_NODEJS_PLUGIN_NAME"
|
2021-11-30 02:00:39 +08:00
|
|
|
fi
|
2021-11-24 10:08:14 +08:00
|
|
|
}
|
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"
|
2021-12-17 08:10:58 +08:00
|
|
|
local args=()
|
2021-10-30 09:40:13 +08:00
|
|
|
|
2021-11-24 10:08:14 +08:00
|
|
|
if [ "$install_type" = ref ]; then
|
2021-12-17 08:10:58 +08:00
|
|
|
args+=(-c)
|
2021-10-30 09:40:13 +08:00
|
|
|
fi
|
2020-10-03 00:11:26 +08:00
|
|
|
|
2021-11-30 02:00:39 +08:00
|
|
|
try_to_update_nodebuild
|
2021-11-24 10:35:25 +08:00
|
|
|
|
|
|
|
NODE_BUILD_BUILD_PATH="$ASDF_DOWNLOAD_PATH" NODE_BUILD_CACHE_PATH="$ASDF_DOWNLOAD_PATH" \
|
2021-12-17 08:10:58 +08:00
|
|
|
nodebuild_wrapped ${args+"${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}"
|
2021-11-30 02:00:39 +08:00
|
|
|
local name
|
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
|
|
|
|
2021-11-30 02:00:39 +08:00
|
|
|
while read -r name; do
|
|
|
|
(
|
2021-12-14 09:50:58 +08:00
|
|
|
printf "Installing $(colored $YELLOW %s) npm package...\n" "$name"
|
2021-11-30 02:00:39 +08:00
|
|
|
source "$(dirname "$0")/exec-env"
|
|
|
|
PATH="$ASDF_INSTALL_PATH/bin:$PATH" npm install -g "$name" > /dev/null 2>&1 && rc=$? || rc=$?
|
|
|
|
if [[ $rc -eq 0 ]]; then
|
|
|
|
printf "$(colored $GREEN SUCCESS)\n"
|
|
|
|
else
|
|
|
|
printf "$(colored $RED FAIL)\n"
|
|
|
|
fi
|
|
|
|
)
|
|
|
|
done < "$default_npm_packages"
|
2017-07-31 12:37:03 +08:00
|
|
|
}
|
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"
|