devs/bin/npm_install.sh

171 lines
5.6 KiB
Bash
Executable file

#!/bin/bash
#Script : npm_install.sh
#Apps : MRDEVS TOOLS
#Description : Instalador interactivo para Node.js y npm en múltiples sistemas operativos
#Author : Mauro Rosero Pérez
#Company Email : mauro@rosero.one
#Personal Email : mauro.rosero@gmail.com
#Created : 2025/03/09 10:27:00
#Modified : 2025/03/19 11:57:08
#Version : 1.2.0
#Use Notes :
#==============================================================================
# Derechos de Autor [2025] [Mauro Rosero P. <mauro@rosero.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/>.
# Configuración inicial
# Usar DEVELOPER_DIR de base.lib
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
BIN_BASE="bin"
BIN_LIBS="lib"
BIN_MESG="msg"
BIN_CFGS="config"
# Leer DEVSPATH desde el archivo de configuración o usar "devs" por defecto
if [ -f "$SCRIPT_DIR/$BIN_CFGS/devspath.dat" ]; then
DEVSPATH=$(cat "$SCRIPT_DIR/$BIN_CFGS/devspath.dat")
else
DEVSPATH="devs"
fi
BIN_HOME="$HOME/$DEVSPATH"
VERSION=$(cat "$BIN_HOME/$BIN_BASE/$BIN_CFGS/version")
# CHECK SHELL LANGUAGE
BIN_LANG=${LANG:0:2}
# Importar bibliotecas necesarias
source "${BIN_HOME}/${BIN_BASE}/${BIN_LIBS}/base.lib"
# Cargar mensajes en el idioma del sistema o español por defecto
load_messages "${BIN_HOME}/${BIN_BASE}" "${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