asdf-nodejs/bin/install

119 lines
3.1 KiB
Plaintext
Raw Normal View History

2015-05-24 12:26:04 +08:00
#!/usr/bin/env bash
set -eu -o pipefail
# shellcheck source=../lib/utils.sh
source "$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/../lib/utils.sh"
2015-05-24 12:26:04 +08:00
install_nodejs() {
local install_type="$1" version_query="$2" install_path="$3"
local version=
version="$(resolve_version_query "$version_query")"
2015-05-24 12:26:04 +08:00
if [ "$version" != "$version_query" ]; then
install_aliased_version "$version" "$version_query" "$install_path"
else
install_canon_version "$install_type" "$version" "$install_path"
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 "
%s: Updating node-build failed with exit code %s. The installation will
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\`
\n" "$(colored $YELLOW WARNING)" "$exit_code" "$ASDF_NODEJS_PLUGIN_NAME"
fi
}
nodebuild_wrapped() {
"$ASDF_NODEJS_PLUGIN_DIR/lib/commands/command-nodebuild.bash" "$@"
}
install_canon_version() {
local install_type="$1" version="$2" install_path="$3"
local nodebuild_args=()
if [ "$install_type" = ref ]; then
nodebuild_args+=(-c)
fi
2021-11-30 02:00:39 +08:00
try_to_update_nodebuild
NODE_BUILD_BUILD_PATH="$ASDF_DOWNLOAD_PATH" NODE_BUILD_CACHE_PATH="$ASDF_DOWNLOAD_PATH" \
nodebuild_wrapped "${nodebuild_args[@]}" "$version" "$install_path"
}
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"
fi
>&2 echo "Linking \"$version_query\" to \"$version\""
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
resolve_version_query() {
local version_query="$1"
local canon_version="$(
2020-09-24 08:09:50 +08:00
# Find the first candidate which the alias match, then print it version
print_index_tab \
| awk -F'\t' -v "alias=$version_query" '$1 == alias { print $2; exit }'
)"
if [ -z "$canon_version" ]; then
echo "$version_query"
else
echo "$canon_version"
fi
}
2015-05-24 12:26:04 +08:00
2020-09-24 08:09:50 +08:00
install_default_npm_packages() {
local default_npm_packages="${ASDF_NPM_DEFAULT_PACKAGES_FILE:=$HOME/.default-npm-packages}"
2021-11-30 02:00:39 +08:00
local name
2019-05-03 19:16:52 +08:00
if [ ! -f "$default_npm_packages" ]; then return; fi
2021-11-30 02:00:39 +08:00
while read -r name; do
(
printf "Installing $(colord $YELLOW %s) npm package...\n" "$name"
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"
}
install_nodejs "$ASDF_INSTALL_TYPE" "$ASDF_INSTALL_VERSION" "$ASDF_INSTALL_PATH"
install_default_npm_packages
asdf reshim "$(plugin_name)" "$ASDF_INSTALL_VERSION"