diff --git a/nodesource_setup.sh b/nodesource_setup.sh new file mode 100644 index 0000000..2d8bdb9 --- /dev/null +++ b/nodesource_setup.sh @@ -0,0 +1,121 @@ +#!/bin/bash + +# Logger Function +log() { + local message="$1" + local type="$2" + local timestamp=$(date '+%Y-%m-%d %H:%M:%S') + local color + local endcolor="\033[0m" + + case "$type" in + "info") color="\033[38;5;79m" ;; + "success") color="\033[1;32m" ;; + "error") color="\033[1;31m" ;; + *) color="\033[1;34m" ;; + esac + + echo -e "${color}${timestamp} - ${message}${endcolor}" +} + +# Error handler function +handle_error() { + local exit_code=$1 + local error_message="$2" + log "Error: $error_message (Exit Code: $exit_code)" "error" + exit $exit_code +} + +# Function to check for command availability +command_exists() { + command -v "$1" &> /dev/null +} + +check_os() { + if ! [ -f "/etc/debian_version" ]; then + echo "Error: This script is only supported on Debian-based systems." + exit 1 + fi +} + +# Function to Install the script pre-requisites +install_pre_reqs() { + log "Installing pre-requisites" "info" + + # Run 'apt update' + if ! apt update -y; then + handle_error "$?" "Failed to run 'apt update'" + fi + + # Run 'apt install' + if ! apt install -y apt-transport-https ca-certificates curl gnupg; then + handle_error "$?" "Failed to install packages" + fi + + if ! mkdir -p /usr/share/keyrings; then + handle_error "$?" "Makes sure the path /usr/share/keyrings exist or run ' mkdir -p /usr/share/keyrings' with sudo" + fi + + rm -f /usr/share/keyrings/nodesource.gpg || true + rm -f /etc/apt/sources.list.d/nodesource.list || true + rm -f /etc/apt/sources.list.d/nodesource.sources || true + + # Run 'curl' and 'gpg' to download and import the NodeSource signing key + if ! curl -fsSL https://deb.nodesource.com/gpgkey/nodesource-repo.gpg.key | gpg --dearmor -o /usr/share/keyrings/nodesource.gpg; then + handle_error "$?" "Failed to download and import the NodeSource signing key" + fi + + # Explicitly set the permissions to ensure the file is readable by all + if ! chmod 644 /usr/share/keyrings/nodesource.gpg; then + handle_error "$?" "Failed to set correct permissions on /usr/share/keyrings/nodesource.gpg" + fi +} + +# Function to configure the Repo +configure_repo() { + local node_version=$1 + + arch=$(dpkg --print-architecture) + if [ "$arch" != "amd64" ] && [ "$arch" != "arm64" ]; then + handle_error "1" "Unsupported architecture: $arch. Only amd64, arm64 are supported. Contact Nodesource for an extended support version https://nodesource.com/pages/contact-us.html." + fi + + cat < /dev/null +Types: deb +URIs: https://deb.nodesource.com/node_$node_version +Suites: nodistro +Components: main +Architectures: $arch +Signed-By: /usr/share/keyrings/nodesource.gpg +EOF + + # N|solid Config + echo "Package: nsolid" | tee /etc/apt/preferences.d/nsolid > /dev/null + echo "Pin: origin deb.nodesource.com" | tee -a /etc/apt/preferences.d/nsolid > /dev/null + echo "Pin-Priority: 600" | tee -a /etc/apt/preferences.d/nsolid > /dev/null + + # Nodejs Config + echo "Package: nodejs" | tee /etc/apt/preferences.d/nodejs > /dev/null + echo "Pin: origin deb.nodesource.com" | tee -a /etc/apt/preferences.d/nodejs > /dev/null + echo "Pin-Priority: 600" | tee -a /etc/apt/preferences.d/nodejs > /dev/null + + # Run 'apt update' + if ! apt update -y; then + handle_error "$?" "Failed to run 'apt update'" + else + log "Repository configured successfully." + log "To install Node.js, run: apt install nodejs -y" "info" + log "You can use N|solid Runtime as a node.js alternative" "info" + log "To install N|solid Runtime, run: apt install nsolid -y \n" "success" + fi +} + +# Define Node.js version +NODE_VERSION="22.x" + +# Check OS +check_os + +# Main execution +install_pre_reqs || handle_error $? "Failed installing pre-requisites" +configure_repo "$NODE_VERSION" || handle_error $? "Failed configuring repository" diff --git a/工程分析/经验记录.md b/工程分析/经验记录.md index 4147c4c..af51c1b 100644 --- a/工程分析/经验记录.md +++ b/工程分析/经验记录.md @@ -34,11 +34,13 @@ AI 助手运行的容器/环境与项目实际开发环境分离,后者才装 ### C. 解决方案 - 文档创建和 git 操作可在 AI 环境中完成 - 构建验证(`npm run lint`、`npm run build`、`npm start`)需由用户在本地开发环境执行,或 AI 提供精确命令由用户自行运行 -- 在经验记录中标注此限制,提醒后续流程 +- **实际解决**: 用户提供了 sudo 权限和密码后,AI 通过 NodeSource 安装了 Node.js 22.x(`curl -fsSL https://deb.nodesource.com/setup_22.x | sudo bash` + `sudo apt install -y nodejs`),随后成功执行了完整部署流程 +- 在经验记录中标注此限制和解决方案,提醒后续流程 ### D. 后续如何避免问题 - 每次执行阶段 6 和阶段 9 前,先检查 `which npm` -- 如 npm 不可用,向用户明确说明需手动执行的命令,并提供复制粘贴格式 +- 如 npm 不可用,向用户申请 sudo 权限安装 Node.js,或使用项目预设的容器/CI 环境 +- 如用户无法提供 sudo,则提供精确的手动执行命令清单 - 考虑在 CI/CD 流程中统一构建环境 ---