2021-11-14 17:10:14 +00:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
# -*- coding: utf-8; mode: sh indent-tabs-mode: nil -*-
|
|
|
|
# SPDX-License-Identifier: AGPL-3.0-or-later
|
|
|
|
#
|
|
|
|
# Tools to install and maintain NVM versions manager for Node.js
|
|
|
|
#
|
|
|
|
# [1] https://github.com/nvm-sh/nvm
|
|
|
|
|
|
|
|
# https://github.com/koalaman/shellcheck/issues/356#issuecomment-853515285
|
|
|
|
# shellcheck source=utils/lib.sh
|
|
|
|
. /dev/null
|
|
|
|
|
|
|
|
declare main_cmd
|
|
|
|
|
|
|
|
# configure nvm environment
|
|
|
|
# -------------------------
|
|
|
|
|
|
|
|
NVM_LOCAL_FOLDER=.nvm
|
|
|
|
|
|
|
|
[[ -z "${NVM_GIT_URL}" ]] && NVM_GIT_URL="https://github.com/nvm-sh/nvm.git"
|
|
|
|
[[ -z "${NVM_MIN_NODE_VER}" ]] && NVM_MIN_NODE_VER="16.13.0"
|
|
|
|
|
2022-09-27 15:01:00 +00:00
|
|
|
# initialize nvm environment
|
2021-11-14 17:10:14 +00:00
|
|
|
# -------------------------
|
|
|
|
|
|
|
|
nvm.env() {
|
|
|
|
source "${NVM_DIR}/nvm.sh"
|
|
|
|
source "${NVM_DIR}/bash_completion"
|
2022-01-20 19:31:56 +00:00
|
|
|
[ "$VERBOSE" = "1" ] && info_msg "sourced NVM environment from ${NVM_DIR}"
|
2022-09-23 16:21:41 +00:00
|
|
|
return 0
|
2021-11-14 17:10:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
nvm.is_installed() {
|
|
|
|
# is true if NVM is installed / in $HOME or even in <repo-root>/.nvm
|
2022-01-20 19:31:56 +00:00
|
|
|
[[ -f "${NVM_DIR}/nvm.sh" ]]
|
2021-11-14 17:10:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if [[ -z "${NVM_DIR}" ]]; then
|
2024-10-09 09:59:31 +00:00
|
|
|
# nvm is not pre-installed in $HOME. Prepare for using nvm from <repo-root>
|
2021-11-14 17:10:14 +00:00
|
|
|
NVM_DIR="$(git rev-parse --show-toplevel)/${NVM_LOCAL_FOLDER}"
|
|
|
|
fi
|
|
|
|
export NVM_DIR
|
|
|
|
|
|
|
|
if nvm.is_installed; then
|
|
|
|
nvm.env
|
|
|
|
else
|
|
|
|
# if nvm is not installed, use this function as a wrapper
|
|
|
|
nvm() {
|
|
|
|
nvm.ensure
|
|
|
|
nvm "$@"
|
|
|
|
}
|
|
|
|
fi
|
|
|
|
|
|
|
|
# implement nvm functions
|
|
|
|
# -----------------------
|
|
|
|
|
|
|
|
nvm.is_local() {
|
|
|
|
# is true if NVM is installed in <repo-root>/.nvm
|
|
|
|
[ "${NVM_DIR}" = "$(git rev-parse --show-toplevel)/${NVM_LOCAL_FOLDER}" ]
|
|
|
|
}
|
|
|
|
|
2022-01-20 19:31:56 +00:00
|
|
|
nvm.min_node() {
|
2021-11-14 17:10:14 +00:00
|
|
|
|
|
|
|
# usage: nvm.min_node 16.3.0
|
|
|
|
#
|
|
|
|
# Is true if minimal Node.js version is installed.
|
|
|
|
|
|
|
|
local min_v
|
|
|
|
local node_v
|
|
|
|
local higher_v
|
|
|
|
|
|
|
|
if ! command -v node >/dev/null; then
|
2022-04-13 12:28:28 +00:00
|
|
|
warn_msg "Node.js is not yet installed"
|
2021-11-14 17:10:14 +00:00
|
|
|
return 42
|
|
|
|
fi
|
|
|
|
|
|
|
|
min_v="${1}"
|
|
|
|
node_v="$(node --version)"
|
|
|
|
node_v="${node_v:1}" # remove 'v' from 'v16.3.0'
|
|
|
|
if ! [ "${min_v}" = "${node_v}" ]; then
|
|
|
|
higher_v="$(echo -e "$min_v\n${node_v}" | sort -Vr | head -1)"
|
|
|
|
if [ "${min_v}" = "${higher_v}" ]; then
|
|
|
|
return 42
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
# implement nvm command line
|
|
|
|
# --------------------------
|
|
|
|
|
2022-01-20 19:31:56 +00:00
|
|
|
nvm.help() {
|
2021-11-14 17:10:14 +00:00
|
|
|
cat <<EOF
|
|
|
|
nvm.: use nvm (without dot) to execute nvm commands directly
|
|
|
|
install : install NVM locally at $(git rev-parse --show-toplevel)/${NVM_LOCAL_FOLDER}
|
|
|
|
clean : remove NVM installation
|
2024-10-09 09:59:31 +00:00
|
|
|
status : prompt some status information about nvm & node
|
2021-11-14 17:10:14 +00:00
|
|
|
nodejs : install Node.js latest LTS
|
2021-11-20 14:40:18 +00:00
|
|
|
cmd ... : run command ... in NVM environment
|
2021-11-14 17:10:14 +00:00
|
|
|
bash : start bash interpreter with NVM environment sourced
|
|
|
|
EOF
|
|
|
|
}
|
|
|
|
|
|
|
|
nvm.install() {
|
|
|
|
local NVM_VERSION_TAG
|
|
|
|
info_msg "install (update) NVM at ${NVM_DIR}"
|
2022-09-23 16:21:41 +00:00
|
|
|
if nvm.is_installed; then
|
2021-11-14 17:10:14 +00:00
|
|
|
info_msg "already cloned at: ${NVM_DIR}"
|
|
|
|
pushd "${NVM_DIR}" &> /dev/null
|
|
|
|
git fetch --all | prefix_stdout " ${_Yellow}||${_creset} "
|
|
|
|
else
|
2024-10-09 09:59:31 +00:00
|
|
|
# delete any leftovers from previous installations
|
2022-09-23 16:21:41 +00:00
|
|
|
if nvm.is_local; then
|
|
|
|
rm -rf "${NVM_DIR}"
|
|
|
|
fi
|
2021-11-14 17:10:14 +00:00
|
|
|
info_msg "clone: ${NVM_GIT_URL}"
|
|
|
|
git clone "${NVM_GIT_URL}" "${NVM_DIR}" 2>&1 | prefix_stdout " ${_Yellow}||${_creset} "
|
|
|
|
pushd "${NVM_DIR}" &> /dev/null
|
|
|
|
git config --local advice.detachedHead false
|
|
|
|
fi
|
|
|
|
NVM_VERSION_TAG="$(git rev-list --tags --max-count=1)"
|
|
|
|
NVM_VERSION_TAG="$(git describe --abbrev=0 --tags --match "v[0-9]*" "${NVM_VERSION_TAG}")"
|
|
|
|
info_msg "checkout ${NVM_VERSION_TAG}"
|
|
|
|
git checkout "${NVM_VERSION_TAG}" 2>&1 | prefix_stdout " ${_Yellow}||${_creset} "
|
2021-11-20 11:55:15 +00:00
|
|
|
popd &> /dev/null
|
2022-01-22 08:26:17 +00:00
|
|
|
if [ -f "${REPO_ROOT}/.nvm_packages" ]; then
|
|
|
|
cp "${REPO_ROOT}/.nvm_packages" "${NVM_DIR}/default-packages"
|
|
|
|
fi
|
2021-11-14 17:10:14 +00:00
|
|
|
nvm.env
|
|
|
|
}
|
|
|
|
|
|
|
|
nvm.clean() {
|
|
|
|
if ! nvm.is_installed; then
|
2021-11-19 12:14:02 +00:00
|
|
|
build_msg CLEAN "[NVM] not installed"
|
|
|
|
return
|
2021-11-14 17:10:14 +00:00
|
|
|
fi
|
|
|
|
if ! nvm.is_local; then
|
2021-11-19 12:14:02 +00:00
|
|
|
build_msg CLEAN "[NVM] can't remove nvm from ${NVM_DIR}"
|
|
|
|
return
|
|
|
|
fi
|
|
|
|
if [ -n "${NVM_DIR}" ]; then
|
|
|
|
build_msg CLEAN "[NVM] drop $(realpath --relative-to=. "${NVM_DIR}")/"
|
|
|
|
rm -rf "${NVM_DIR}"
|
2021-11-14 17:10:14 +00:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2021-11-20 11:55:15 +00:00
|
|
|
nvm.status() {
|
2021-11-14 17:10:14 +00:00
|
|
|
if command -v node >/dev/null; then
|
|
|
|
info_msg "Node.js is installed at $(command -v node)"
|
|
|
|
info_msg "Node.js is version $(node --version)"
|
|
|
|
if ! nvm.min_node "${NVM_MIN_NODE_VER}"; then
|
|
|
|
warn_msg "minimal Node.js version is ${NVM_MIN_NODE_VER}"
|
|
|
|
fi
|
|
|
|
else
|
|
|
|
warn_msg "Node.js is mot installed"
|
|
|
|
fi
|
|
|
|
if command -v npm >/dev/null; then
|
|
|
|
info_msg "npm is installed at $(command -v npm)"
|
|
|
|
info_msg "npm is version $(npm --version)"
|
|
|
|
else
|
|
|
|
warn_msg "npm is not installed"
|
|
|
|
fi
|
|
|
|
if nvm.is_installed; then
|
|
|
|
info_msg "NVM is installed at ${NVM_DIR}"
|
|
|
|
else
|
|
|
|
warn_msg "NVM is not installed"
|
2021-11-20 14:19:17 +00:00
|
|
|
info_msg "to install NVM and Node.js (LTS) use: ${main_cmd} nvm.nodejs"
|
2021-11-14 17:10:14 +00:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2021-11-20 11:55:15 +00:00
|
|
|
nvm.nodejs() {
|
2021-11-20 14:19:17 +00:00
|
|
|
nvm install
|
2021-11-14 17:10:14 +00:00
|
|
|
nvm.status
|
|
|
|
}
|
|
|
|
|
|
|
|
nvm.bash() {
|
|
|
|
nvm.ensure
|
|
|
|
bash --init-file <(cat "${NVM_DIR}/nvm.sh" "${NVM_DIR}/bash_completion")
|
|
|
|
}
|
|
|
|
|
2021-11-20 14:40:18 +00:00
|
|
|
nvm.cmd() {
|
|
|
|
nvm.ensure
|
|
|
|
"$@"
|
|
|
|
}
|
|
|
|
|
2021-11-14 17:10:14 +00:00
|
|
|
nvm.ensure() {
|
|
|
|
if ! nvm.is_installed; then
|
|
|
|
nvm.install
|
|
|
|
fi
|
|
|
|
}
|