lib_nvm.sh 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. #!/usr/bin/env bash
  2. # -*- coding: utf-8; mode: sh indent-tabs-mode: nil -*-
  3. # SPDX-License-Identifier: AGPL-3.0-or-later
  4. #
  5. # Tools to install and maintain NVM versions manager for Node.js
  6. #
  7. # [1] https://github.com/nvm-sh/nvm
  8. # https://github.com/koalaman/shellcheck/issues/356#issuecomment-853515285
  9. # shellcheck source=utils/lib.sh
  10. . /dev/null
  11. declare main_cmd
  12. # configure nvm environment
  13. # -------------------------
  14. NVM_LOCAL_FOLDER=.nvm
  15. [[ -z "${NVM_GIT_URL}" ]] && NVM_GIT_URL="https://github.com/nvm-sh/nvm.git"
  16. [[ -z "${NVM_MIN_NODE_VER}" ]] && NVM_MIN_NODE_VER="16.13.0"
  17. # initalize nvm environment
  18. # -------------------------
  19. nvm.env() {
  20. source "${NVM_DIR}/nvm.sh"
  21. source "${NVM_DIR}/bash_completion"
  22. [ "$VERBOSE" = "1" ] && info_msg "sourced NVM environment from ${NVM_DIR}"
  23. }
  24. nvm.is_installed() {
  25. # is true if NVM is installed / in $HOME or even in <repo-root>/.nvm
  26. [[ -f "${NVM_DIR}/nvm.sh" ]]
  27. }
  28. if [[ -z "${NVM_DIR}" ]]; then
  29. # nvm is not pre-intalled in $HOME. Prepare for using nvm from <repo-root>
  30. NVM_DIR="$(git rev-parse --show-toplevel)/${NVM_LOCAL_FOLDER}"
  31. fi
  32. export NVM_DIR
  33. if nvm.is_installed; then
  34. nvm.env
  35. else
  36. # if nvm is not installed, use this function as a wrapper
  37. nvm() {
  38. nvm.ensure
  39. nvm "$@"
  40. }
  41. fi
  42. # implement nvm functions
  43. # -----------------------
  44. nvm.is_local() {
  45. # is true if NVM is installed in <repo-root>/.nvm
  46. [ "${NVM_DIR}" = "$(git rev-parse --show-toplevel)/${NVM_LOCAL_FOLDER}" ]
  47. }
  48. nvm.min_node() {
  49. # usage: nvm.min_node 16.3.0
  50. #
  51. # Is true if minimal Node.js version is installed.
  52. local min_v
  53. local node_v
  54. local higher_v
  55. if ! command -v node >/dev/null; then
  56. wanr_msg "Node.js is not yet installed"
  57. return 42
  58. fi
  59. min_v="${1}"
  60. node_v="$(node --version)"
  61. node_v="${node_v:1}" # remove 'v' from 'v16.3.0'
  62. if ! [ "${min_v}" = "${node_v}" ]; then
  63. higher_v="$(echo -e "$min_v\n${node_v}" | sort -Vr | head -1)"
  64. if [ "${min_v}" = "${higher_v}" ]; then
  65. return 42
  66. fi
  67. fi
  68. }
  69. # implement nvm command line
  70. # --------------------------
  71. nvm.help() {
  72. cat <<EOF
  73. nvm.: use nvm (without dot) to execute nvm commands directly
  74. install : install NVM locally at $(git rev-parse --show-toplevel)/${NVM_LOCAL_FOLDER}
  75. clean : remove NVM installation
  76. status : prompt some status informations about nvm & node
  77. nodejs : install Node.js latest LTS
  78. cmd ... : run command ... in NVM environment
  79. bash : start bash interpreter with NVM environment sourced
  80. EOF
  81. }
  82. nvm.install() {
  83. local NVM_VERSION_TAG
  84. info_msg "install (update) NVM at ${NVM_DIR}"
  85. if [[ -d "${NVM_DIR}" ]] ; then
  86. info_msg "already cloned at: ${NVM_DIR}"
  87. pushd "${NVM_DIR}" &> /dev/null
  88. git fetch --all | prefix_stdout " ${_Yellow}||${_creset} "
  89. else
  90. info_msg "clone: ${NVM_GIT_URL}"
  91. git clone "${NVM_GIT_URL}" "${NVM_DIR}" 2>&1 | prefix_stdout " ${_Yellow}||${_creset} "
  92. pushd "${NVM_DIR}" &> /dev/null
  93. git config --local advice.detachedHead false
  94. fi
  95. NVM_VERSION_TAG="$(git rev-list --tags --max-count=1)"
  96. NVM_VERSION_TAG="$(git describe --abbrev=0 --tags --match "v[0-9]*" "${NVM_VERSION_TAG}")"
  97. info_msg "checkout ${NVM_VERSION_TAG}"
  98. git checkout "${NVM_VERSION_TAG}" 2>&1 | prefix_stdout " ${_Yellow}||${_creset} "
  99. popd &> /dev/null
  100. if [ -f "${REPO_ROOT}/.nvm_packages" ]; then
  101. cp "${REPO_ROOT}/.nvm_packages" "${NVM_DIR}/default-packages"
  102. fi
  103. nvm.env
  104. }
  105. nvm.clean() {
  106. if ! nvm.is_installed; then
  107. build_msg CLEAN "[NVM] not installed"
  108. return
  109. fi
  110. if ! nvm.is_local; then
  111. build_msg CLEAN "[NVM] can't remove nvm from ${NVM_DIR}"
  112. return
  113. fi
  114. if [ -n "${NVM_DIR}" ]; then
  115. build_msg CLEAN "[NVM] drop $(realpath --relative-to=. "${NVM_DIR}")/"
  116. rm -rf "${NVM_DIR}"
  117. fi
  118. }
  119. nvm.status() {
  120. if command -v node >/dev/null; then
  121. info_msg "Node.js is installed at $(command -v node)"
  122. info_msg "Node.js is version $(node --version)"
  123. if ! nvm.min_node "${NVM_MIN_NODE_VER}"; then
  124. warn_msg "minimal Node.js version is ${NVM_MIN_NODE_VER}"
  125. fi
  126. else
  127. warn_msg "Node.js is mot installed"
  128. fi
  129. if command -v npm >/dev/null; then
  130. info_msg "npm is installed at $(command -v npm)"
  131. info_msg "npm is version $(npm --version)"
  132. else
  133. warn_msg "npm is not installed"
  134. fi
  135. if nvm.is_installed; then
  136. info_msg "NVM is installed at ${NVM_DIR}"
  137. else
  138. warn_msg "NVM is not installed"
  139. info_msg "to install NVM and Node.js (LTS) use: ${main_cmd} nvm.nodejs"
  140. fi
  141. }
  142. nvm.nodejs() {
  143. nvm install
  144. nvm.status
  145. }
  146. nvm.bash() {
  147. nvm.ensure
  148. bash --init-file <(cat "${NVM_DIR}/nvm.sh" "${NVM_DIR}/bash_completion")
  149. }
  150. nvm.cmd() {
  151. nvm.ensure
  152. "$@"
  153. }
  154. nvm.ensure() {
  155. if ! nvm.is_installed; then
  156. nvm.install
  157. fi
  158. }