devs/bin/npm_install.sh
Mauro Rosero P. d32832b091
[IMPROVED] Centralizar versión en bin/config/version
- Modificar todos los scripts para leer la versión desde bin/config/version
- Añadir variable BIN_CFGS donde faltaba
- Eliminar definiciones locales de VERSION en los scripts

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-03-19 07:09:30 -05:00

157 lines
5 KiB
Bash
Executable file

#!/bin/bash
# npm_install.sh
# Script interactivo para instalar Node.js y npm en múltiples sistemas operativos
# Soporta: Arch Linux, Debian/Ubuntu, Fedora, FreeBSD y macOS (Homebrew)
# Requiere: dialog
# Modified: 2025/03/09 10:27:00
# Creado por [Cortana Rosero One <cortana@rosero.one>]
# Generado con inteligencia artificial Claude Source
# Derechos de Autor (C) [2024] [Mauro Rosero P. <mauro@roser.one>]
#
# Este programa es software libre: usted puede redistribuirlo y/o modificarlo
# bajo los términos de la Licencia Pública Affero General de GNU tal como
# lo publica la Free Software Foundation, ya sea la versión 3 de la licencia,
# o (a su elección) cualquier versión posterior.
#
# Este programa se distribuye con la esperanza de que sea útil,
# pero SIN NINGUNA GARANTÍA; sin siquiera la garantía implícita de
# COMERCIABILIDAD o IDONEIDAD PARA UN PROPÓSITO PARTICULAR. Consulte la
# Licencia Pública Affero General de GNU para obtener más detalles.
#
# Debería haber recibido una copia de la Licencia Pública Affero General
# junto con este programa. Si no la recibió, consulte <https://www.gnu.org/licenses/>.
DEVSPATH=devs
BIN_HOME=$HOME/$DEVSPATH
BIN_MESG=bin/msg
BIN_LIBS=bin/lib
BIN_CFGS=bin/config
VERSION=$(cat "$BIN_HOME/$BIN_CFGS/version")
# CHECK SHELL LANGUAGE
BIN_LANG=${LANG:0:2}
# LOAD BASE BASH LIBRARY
source $BIN_HOME/$BIN_LIBS/base.lib
#baselib_test
# Load head messages
load_messages $BIN_HOME $BIN_MESG $BIN_LANG "head"
title="${head_000} ${head_002}"
# Función para limpiar la pantalla y salir
function finish() {
clear
exit
}
# Verificar que dialog esté instalado
if ! command -v dialog &> /dev/null; then
echo "${npm_001}"
exit 1
fi
# Si no se ejecuta como root, solicitar contraseña para escalamiento vía dialog
if [ "$EUID" -ne 0 ]; then
SUDO_PASS=$(dialog --backtitle "${title}" --title "${head_superuser}" --insecure --passwordbox "${npm_002}" 10 60 3>&1 1>&2 2>&3 3>&-)
# Probar la contraseña
echo "$SUDO_PASS" | sudo -S true &> /dev/null
if [ $? -ne 0 ]; then
dialog --backtitle "${title}" --title "${head_error}" --msgbox "${npm_003}" 8 50
finish
fi
fi
# Función para ejecutar comandos con sudo si es necesario
run_cmd() {
if [ "$EUID" -eq 0 ]; then
"$@"
else
echo "$SUDO_PASS" | sudo -S "$@"
fi
}
# Mensaje de bienvenida
dialog --backtitle "${title}" --title "${npm_016}" \
--msgbox "${npm_004}" 10 60
# Detectar sistema operativo
OS_TYPE=$(uname -s)
DISTRO=""
UPDATE_CMD=()
INSTALL_CMD=()
if [[ "$OS_TYPE" == "Linux" ]]; then
if command -v apt-get &> /dev/null; then
DISTRO="Debian/Ubuntu (apt-get)"
UPDATE_CMD=("apt-get" "update")
INSTALL_CMD=("apt-get" "install" "-y" "nodejs" "npm")
elif command -v pacman &> /dev/null; then
DISTRO="Arch Linux (pacman)"
UPDATE_CMD=("pacman" "-Sy")
INSTALL_CMD=("pacman" "-S" "--noconfirm" "nodejs" "npm")
elif command -v dnf &> /dev/null; then
DISTRO="Fedora (dnf)"
# dnf actualiza automáticamente en muchos casos
INSTALL_CMD=("dnf" "install" "-y" "nodejs" "npm")
elif command -v yum &> /dev/null; then
DISTRO="Fedora/CentOS (yum)"
INSTALL_CMD=("yum" "install" "-y" "nodejs" "npm")
else
dialog --backtitle "${title}" --title "${head_error}" --msgbox "${npm_006}" 8 50
finish
fi
elif [[ "$OS_TYPE" == "FreeBSD" ]]; then
DISTRO="FreeBSD (pkg)"
INSTALL_CMD=("pkg" "install" "-y" "node" "npm")
elif [[ "$OS_TYPE" == "Darwin" ]]; then
DISTRO="macOS (Homebrew)"
INSTALL_CMD=("brew" "install" "node")
else
dialog --backtitle "${title}" --title "${head_error}" --msgbox "4{npm_007} ${OS_TYPE}" 8 50
finish
fi
# Confirmación del sistema detectado
dialog --backtitle "${title}" --title "${npm_017}" \
--yesno "${npm_005_1}\n\n ${DISTRO}\n\n" 10 60
if [ $? -ne 0 ]; then
dialog --backtitle "${title}" --title "${head_canceled}" --msgbox "${npm_008}" 8 50
finish
fi
# Mostrar un progress bar para indicar el avance de la instalación
(
# Inicializar progreso
echo 0; sleep 1
# Actualizar repositorios si corresponde
if [ ${#UPDATE_CMD[@]} -gt 0 ]; then
echo 5; echo "# ${npm_009}"
run_cmd "${UPDATE_CMD[@]}"
echo 25; echo "# ${npm_010}"
sleep 1
else
echo 25; echo "# ${npm_11}"
fi
# Instalar Node.js y npm
echo 40; echo "# ${npm_12}"
run_cmd "${INSTALL_CMD[@]}"
echo 80; echo "# ${npm_013}"
sleep 1
# Finalizar
echo 100; echo "# ${npm_014}"
sleep 1
) | dialog --backtitle "${title}" --title "i${npm_018}" --gauge "${npm_016}" 10 70 0
# Verificar versiones instaladas (algunos sistemas usan "node" en lugar de "nodejs")
node_ver=$(nodejs --version 2>/dev/null || node --version 2>/dev/null)
npm_ver=$(npm --version 2>/dev/null)
result_message="${npm_017}\n\n${npm_019}\nNode.js: ${node_ver:-${npm_020}}\nnpm: ${npm_ver:-${npm_020}}"
dialog --backtitle "${title}" --title "${npm_021}" --msgbox "$result_message" 10 60
finish