nodejs-20250106.eselect 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. # -*-eselect-*- vim: ft=eselect
  2. # Copyright 2019-2020 Orson Teodoro
  3. # Copyright 2005-2012 Gentoo Foundation
  4. # Distributed under the terms of the GNU GPL version 2 or later
  5. DESCRIPTION="Manages the nodejs include symlink"
  6. MAINTAINER="orsonteodoro@hotmail.com"
  7. VERSION="20250106"
  8. PN="nodejs"
  9. # find a list of Node.js symlink targets
  10. find_targets() {
  11. local f
  12. for f in /usr/include/node[0-9]*; do
  13. [[ -d ${f} ]] && echo $(basename "${f}")
  14. done
  15. }
  16. # remove the Node.js symlinks
  17. remove_symlink() {
  18. rm "/usr/include/node" 2>/dev/null 1>/dev/null || true
  19. }
  20. # set the Node.js symlinks
  21. set_symlink() {
  22. local target=$1
  23. if is_number "${target}"; then
  24. local targets=( $(find_targets) )
  25. target=${targets[target-1]}
  26. ln -s "/usr/include/${target}" "/usr/include/node"
  27. corepack disable 2>/dev/null || true
  28. mkdir -p "/usr/$(get_libdir)/node_modules"
  29. rm "/usr/$(get_libdir)/node_modules/corepack"
  30. rm "/usr/bin/corepack"
  31. ln -sf "/usr/$(get_libdir)/corepack/${target}" "/usr/$(get_libdir)/node_modules/corepack"
  32. ln -sf "/usr/$(get_libdir)/node_modules/corepack/dist/corepack.js" "/usr/bin/corepack"
  33. corepack enable
  34. elif [[ -d "/usr/include/${target}" && "${target}" =~ ^node[0-9]* ]] ; then
  35. ln -s "/usr/include/${target}" "/usr/include/node"
  36. corepack disable 2>/dev/null || true
  37. mkdir -p "/usr/$(get_libdir)/node_modules"
  38. rm "/usr/$(get_libdir)/node_modules/corepack"
  39. rm "/usr/bin/corepack"
  40. ln -sf "/usr/$(get_libdir)/corepack/${target}" "/usr/$(get_libdir)/node_modules/corepack"
  41. ln -sf "/usr/$(get_libdir)/node_modules/corepack/dist/corepack.js" "/usr/bin/corepack"
  42. corepack enable
  43. else
  44. echo "Failed to set symlink and is now gone."
  45. return
  46. fi
  47. }
  48. ### show action ###
  49. describe_show() {
  50. echo "Show the current Node.js symlink"
  51. }
  52. get_libdir() {
  53. realpath /usr/lib*/corepack | cut -f 3 -d "/"
  54. }
  55. do_show() {
  56. write_list_start "Current Node.js symlinks:"
  57. if [[ -L /usr/include/node ]]; then
  58. local k_node_src="/usr/include/node"
  59. local k_node_dest=$(canonicalise "/usr/include/node")
  60. write_kv_list_entry "${k_node_src%/} ->" "${k_node_dest%/}"
  61. local k_corepack_src="/usr/bin/corepack"
  62. local k_corepack_dest=$(canonicalise "/usr/bin/corepack")
  63. write_kv_list_entry "${k_corepack_src%/} ->" "${k_corepack_dest%/}"
  64. else
  65. write_kv_list_entry "(unset)" ""
  66. fi
  67. }
  68. ### list action ###
  69. describe_list() {
  70. echo "List available Node.js symlink targets"
  71. }
  72. do_list() {
  73. local i targets=( $(find_targets) )
  74. write_list_start "Available Node.js symlink targets:"
  75. for (( i = 0; i < ${#targets[@]}; i++ )); do
  76. # highlight the target where the symlink is pointing to
  77. [[ ${targets[i]} = \
  78. $(basename "$(canonicalise "/usr/include/node")") ]] \
  79. && targets[i]=$(highlight_marker "${targets[i]}")
  80. done
  81. write_numbered_list -m "(none found)" "${targets[@]}"
  82. }
  83. ### set action ###
  84. describe_set() {
  85. echo "Set a new Node.js symlink target"
  86. }
  87. describe_set_parameters() {
  88. echo "<target>"
  89. }
  90. describe_set_options() {
  91. echo "target : Target name or number (from 'list' action)"
  92. }
  93. do_set() {
  94. [[ -z $1 ]] && die -q "You didn't tell me what to set the symlink to"
  95. [[ $# -gt 1 ]] && die -q "Too many parameters"
  96. if [[ -L /usr/include/node ]]; then
  97. # existing symlink
  98. remove_symlink || die -q "Couldn't remove existing symlink"
  99. set_symlink "$1" || die -q "Couldn't set a new symlink"
  100. elif [[ -e /usr/include/node ]]; then
  101. # we have something strange
  102. die -q "/usr/include/node exists but is not a symlink"
  103. else
  104. set_symlink "$1" || die -q "Couldn't set a new symlink"
  105. fi
  106. }