lib_nvm.sh 5.0 KB

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