You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
249 lines
5.8 KiB
249 lines
5.8 KiB
|
4 months ago
|
@echo off
|
||
|
|
setlocal enabledelayedexpansion
|
||
|
|
chcp 65001 >nul
|
||
|
|
|
||
|
|
REM 设置错误时退出
|
||
|
|
set "EXIT_CODE=0"
|
||
|
|
|
||
|
|
REM 解析命令行参数
|
||
|
|
call :parse_arguments %*
|
||
|
|
|
||
|
|
REM 主函数
|
||
|
|
call :main
|
||
|
|
goto :end
|
||
|
|
|
||
|
|
REM 主函数
|
||
|
|
:main
|
||
|
|
call :log_info "Starting X1.WebUI build process..."
|
||
|
|
|
||
|
|
call :check_node_version
|
||
|
|
if !EXIT_CODE! neq 0 goto :end
|
||
|
|
|
||
|
|
call :detect_package_manager
|
||
|
|
if !EXIT_CODE! neq 0 goto :end
|
||
|
|
|
||
|
|
call :install_dependencies
|
||
|
|
if !EXIT_CODE! neq 0 goto :end
|
||
|
|
|
||
|
|
call :build_project
|
||
|
|
if !EXIT_CODE! neq 0 goto :end
|
||
|
|
|
||
|
|
call :create_publish_dir
|
||
|
|
call :show_publish_info
|
||
|
|
call :show_deployment_instructions
|
||
|
|
|
||
|
|
call :log_success "Build completed!"
|
||
|
|
call :log_info "Please upload the publish directory contents to server /home/hyh/x1_web/"
|
||
|
|
goto :eof
|
||
|
|
|
||
|
|
REM 检查Node.js版本
|
||
|
|
:check_node_version
|
||
|
|
call :log_info "Checking Node.js version..."
|
||
|
|
node --version >nul 2>&1
|
||
|
|
if errorlevel 1 (
|
||
|
|
call :log_error "Node.js not installed, please install Node.js 18+"
|
||
|
|
set "EXIT_CODE=1"
|
||
|
|
goto :end
|
||
|
|
)
|
||
|
|
|
||
|
|
for /f "tokens=*" %%i in ('node --version') do set "NODE_VERSION=%%i"
|
||
|
|
call :log_success "Node.js version check passed: !NODE_VERSION!"
|
||
|
|
goto :eof
|
||
|
|
|
||
|
|
REM 解析命令行参数
|
||
|
|
:parse_arguments
|
||
|
|
set "PACKAGE_MANAGER="
|
||
|
|
set "SHOW_HELP=false"
|
||
|
|
|
||
|
|
:parse_loop
|
||
|
|
if "%~1"=="" goto :parse_done
|
||
|
|
if /i "%~1"=="--npm" (
|
||
|
|
set "PACKAGE_MANAGER=npm"
|
||
|
|
call :log_info "Package manager set to npm via command line"
|
||
|
|
) else if /i "%~1"=="--yarn" (
|
||
|
|
set "PACKAGE_MANAGER=yarn"
|
||
|
|
call :log_info "Package manager set to yarn via command line"
|
||
|
|
) else if /i "%~1"=="--help" (
|
||
|
|
set "SHOW_HELP=true"
|
||
|
|
) else (
|
||
|
|
call :log_error "Unknown parameter: %~1"
|
||
|
|
call :show_help
|
||
|
|
set "EXIT_CODE=1"
|
||
|
|
goto :end
|
||
|
|
)
|
||
|
|
shift
|
||
|
|
goto :parse_loop
|
||
|
|
|
||
|
|
:parse_done
|
||
|
|
if "!SHOW_HELP!"=="true" (
|
||
|
|
call :show_help
|
||
|
|
set "EXIT_CODE=0"
|
||
|
|
goto :end
|
||
|
|
)
|
||
|
|
goto :eof
|
||
|
|
|
||
|
|
REM 显示帮助信息
|
||
|
|
:show_help
|
||
|
|
echo.
|
||
|
|
echo Usage: build.bat [options]
|
||
|
|
echo.
|
||
|
|
echo Options:
|
||
|
|
echo --npm Force using npm package manager
|
||
|
|
echo --yarn Force using yarn package manager
|
||
|
|
echo --help Show this help message
|
||
|
|
echo.
|
||
|
|
echo Examples:
|
||
|
|
echo build.bat - Auto detect package manager
|
||
|
|
echo build.bat --npm - Force using npm
|
||
|
|
echo build.bat --yarn - Force using yarn
|
||
|
|
echo build.bat --help - Show help
|
||
|
|
echo.
|
||
|
|
goto :eof
|
||
|
|
|
||
|
|
REM 检测包管理器
|
||
|
|
:detect_package_manager
|
||
|
|
call :log_info "Detecting package manager..."
|
||
|
|
if not "!PACKAGE_MANAGER!"=="" (
|
||
|
|
call :log_info "Package manager already set to: !PACKAGE_MANAGER!"
|
||
|
|
goto :eof
|
||
|
|
)
|
||
|
|
|
||
|
|
if exist "yarn.lock" (
|
||
|
|
set "PACKAGE_MANAGER=yarn"
|
||
|
|
call :log_info "Found yarn.lock, using yarn"
|
||
|
|
) else if exist "package-lock.json" (
|
||
|
|
set "PACKAGE_MANAGER=npm"
|
||
|
|
call :log_info "Found package-lock.json, using npm"
|
||
|
|
) else (
|
||
|
|
set "PACKAGE_MANAGER=npm"
|
||
|
|
call :log_info "No lock file found, defaulting to npm"
|
||
|
|
)
|
||
|
|
goto :eof
|
||
|
|
|
||
|
|
REM 安装依赖
|
||
|
|
:install_dependencies
|
||
|
|
call :log_info "Installing project dependencies..."
|
||
|
|
if not exist "node_modules" (
|
||
|
|
if "!PACKAGE_MANAGER!"=="yarn" (
|
||
|
|
call yarn install --frozen-lockfile
|
||
|
|
) else (
|
||
|
|
call npm ci
|
||
|
|
)
|
||
|
|
) else (
|
||
|
|
if "!PACKAGE_MANAGER!"=="yarn" (
|
||
|
|
call yarn install --frozen-lockfile --production
|
||
|
|
) else (
|
||
|
|
call npm ci --only=production
|
||
|
|
)
|
||
|
|
)
|
||
|
|
if errorlevel 1 (
|
||
|
|
call :log_error "Dependency installation failed"
|
||
|
|
set "EXIT_CODE=1"
|
||
|
|
goto :end
|
||
|
|
)
|
||
|
|
call :log_success "Dependencies installed successfully"
|
||
|
|
goto :eof
|
||
|
|
|
||
|
|
REM 构建项目
|
||
|
|
:build_project
|
||
|
|
call :log_info "Building production version..."
|
||
|
|
|
||
|
|
REM 直接尝试构建,如果失败则安装TypeScript
|
||
|
|
call :log_info "Attempting to build project..."
|
||
|
|
if "!PACKAGE_MANAGER!"=="yarn" (
|
||
|
|
call yarn build
|
||
|
|
) else (
|
||
|
|
call npm run build
|
||
|
|
)
|
||
|
|
if errorlevel 1 (
|
||
|
|
call :log_warning "Build failed, checking if TypeScript is needed..."
|
||
|
|
call :log_info "Installing TypeScript as dev dependency..."
|
||
|
|
if "!PACKAGE_MANAGER!"=="yarn" (
|
||
|
|
call yarn add -D typescript
|
||
|
|
) else (
|
||
|
|
call npm install --save-dev typescript
|
||
|
|
)
|
||
|
|
|
||
|
|
call :log_info "Retrying build after TypeScript installation..."
|
||
|
|
if "!PACKAGE_MANAGER!"=="yarn" (
|
||
|
|
call yarn build
|
||
|
|
) else (
|
||
|
|
call npm run build
|
||
|
|
)
|
||
|
|
if errorlevel 1 (
|
||
|
|
call :log_error "Project build failed even after TypeScript installation"
|
||
|
|
set "EXIT_CODE=1"
|
||
|
|
goto :end
|
||
|
|
)
|
||
|
|
)
|
||
|
|
call :log_success "Project build completed"
|
||
|
|
goto :eof
|
||
|
|
|
||
|
|
REM 创建发布目录
|
||
|
|
:create_publish_dir
|
||
|
|
call :log_info "Creating publish directory..."
|
||
|
|
if exist "publish" rmdir /s /q "publish"
|
||
|
|
mkdir "publish"
|
||
|
|
|
||
|
|
REM 复制构建产物到发布目录根目录
|
||
|
|
xcopy "dist\*" "publish\" /E /Y /Q
|
||
|
|
|
||
|
|
REM 复制Dockerfile和nginx配置到发布目录根目录
|
||
|
|
copy "Dockerfile" "publish\" >nul
|
||
|
|
copy "nginx.conf" "publish\" >nul
|
||
|
|
|
||
|
|
REM 复制部署脚本到发布目录根目录
|
||
|
|
copy "deploy.sh" "publish\" >nul
|
||
|
|
|
||
|
|
REM 注意:.dockerignore 文件不需要复制到发布目录,只在本地构建时使用
|
||
|
|
|
||
|
|
call :log_success "Publish directory created: publish/"
|
||
|
|
goto :eof
|
||
|
|
|
||
|
|
REM 显示文件大小
|
||
|
|
:show_publish_info
|
||
|
|
call :log_info "Publish directory info:"
|
||
|
|
for /f "tokens=1" %%i in ('dir publish /s ^| find "个文件"') do echo File count: %%i
|
||
|
|
echo.
|
||
|
|
call :log_info "Publish directory contents:"
|
||
|
|
dir publish /B
|
||
|
|
echo.
|
||
|
|
call :log_info "Publish directory structure:"
|
||
|
|
tree publish /F
|
||
|
|
goto :eof
|
||
|
|
|
||
|
|
REM 显示部署说明
|
||
|
|
:show_deployment_instructions
|
||
|
|
call :log_info "Deployment instructions:"
|
||
|
|
echo.
|
||
|
|
echo 1. Upload all contents of publish directory to server /home/hyh/x1_web/
|
||
|
|
echo 2. Ensure the following files are in /home/hyh/x1_web/ root directory:
|
||
|
|
echo - Dockerfile
|
||
|
|
echo - nginx.conf
|
||
|
|
echo - deploy.sh
|
||
|
|
echo - index.html
|
||
|
|
echo - assets/ directory
|
||
|
|
echo 3. On server run: chmod +x deploy.sh ^&^& ./deploy.sh
|
||
|
|
echo.
|
||
|
|
goto :eof
|
||
|
|
|
||
|
|
REM 日志函数(简化版本,不使用颜色)
|
||
|
|
:log_info
|
||
|
|
echo [INFO] %~1
|
||
|
|
goto :eof
|
||
|
|
|
||
|
|
:log_success
|
||
|
|
echo [SUCCESS] %~1
|
||
|
|
goto :eof
|
||
|
|
|
||
|
|
:log_warning
|
||
|
|
echo [WARNING] %~1
|
||
|
|
goto :eof
|
||
|
|
|
||
|
|
:log_error
|
||
|
|
echo [ERROR] %~1
|
||
|
|
goto :eof
|
||
|
|
|
||
|
|
:end
|
||
|
|
exit /b %EXIT_CODE%
|