lib_nvm.sh 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. }
  23. nvm.is_installed() {
  24. # is true if NVM is installed / in $HOME or even in <repo-root>/.nvm
  25. [[ -d "${NVM_DIR}" ]]
  26. }
  27. if [[ -z "${NVM_DIR}" ]]; then
  28. # nvm is not pre-intalled in $HOME. Prepare for using nvm from <repo-root>
  29. NVM_DIR="$(git rev-parse --show-toplevel)/${NVM_LOCAL_FOLDER}"
  30. fi
  31. export NVM_DIR
  32. if nvm.is_installed; then
  33. [ "$VERBOSE" = "1" ] && info_msg "source NVM environment from ${NVM_DIR}"
  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. bash : start bash interpreter with NVM environment sourced
  79. EOF
  80. }
  81. nvm.install() {
  82. local NVM_VERSION_TAG
  83. info_msg "install (update) NVM at ${NVM_DIR}"
  84. if [[ -d "${NVM_DIR}" ]] ; then
  85. info_msg "already cloned at: ${NVM_DIR}"
  86. pushd "${NVM_DIR}" &> /dev/null
  87. git fetch --all | prefix_stdout " ${_Yellow}||${_creset} "
  88. else
  89. info_msg "clone: ${NVM_GIT_URL}"
  90. git clone "${NVM_GIT_URL}" "${NVM_DIR}" 2>&1 | prefix_stdout " ${_Yellow}||${_creset} "
  91. pushd "${NVM_DIR}" &> /dev/null
  92. git config --local advice.detachedHead false
  93. fi
  94. NVM_VERSION_TAG="$(git rev-list --tags --max-count=1)"
  95. NVM_VERSION_TAG="$(git describe --abbrev=0 --tags --match "v[0-9]*" "${NVM_VERSION_TAG}")"
  96. info_msg "checkout ${NVM_VERSION_TAG}"
  97. git checkout "${NVM_VERSION_TAG}" 2>&1 | prefix_stdout " ${_Yellow}||${_creset} "
  98. popd &> /dev/null
  99. nvm.env
  100. }
  101. nvm.clean() {
  102. if ! nvm.is_installed; then
  103. build_msg CLEAN "[NVM] not installed"
  104. return
  105. fi
  106. if ! nvm.is_local; then
  107. build_msg CLEAN "[NVM] can't remove nvm from ${NVM_DIR}"
  108. return
  109. fi
  110. if [ -n "${NVM_DIR}" ]; then
  111. build_msg CLEAN "[NVM] drop $(realpath --relative-to=. "${NVM_DIR}")/"
  112. rm -rf "${NVM_DIR}"
  113. fi
  114. }
  115. nvm.status() {
  116. if command -v node >/dev/null; then
  117. info_msg "Node.js is installed at $(command -v node)"
  118. info_msg "Node.js is version $(node --version)"
  119. if ! nvm.min_node "${NVM_MIN_NODE_VER}"; then
  120. warn_msg "minimal Node.js version is ${NVM_MIN_NODE_VER}"
  121. fi
  122. else
  123. warn_msg "Node.js is mot installed"
  124. fi
  125. if command -v npm >/dev/null; then
  126. info_msg "npm is installed at $(command -v npm)"
  127. info_msg "npm is version $(npm --version)"
  128. else
  129. warn_msg "npm is not installed"
  130. fi
  131. if nvm.is_installed; then
  132. info_msg "NVM is installed at ${NVM_DIR}"
  133. else
  134. warn_msg "NVM is not installed"
  135. info_msg "to install NVM and Node.js (LTS) use: ${main_cmd} nvm.nodejs"
  136. fi
  137. }
  138. nvm.nodejs() {
  139. nvm install
  140. nvm.status
  141. }
  142. nvm.bash() {
  143. nvm.ensure
  144. bash --init-file <(cat "${NVM_DIR}/nvm.sh" "${NVM_DIR}/bash_completion")
  145. }
  146. nvm.ensure() {
  147. if ! nvm.is_installed; then
  148. nvm.install
  149. fi
  150. }