huminmin
19 小时以前 ca8ade3bdcca8d4c9fa58ddd42dd0a9a3cd66383
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
@echo off
chcp 65001 >nul
setlocal enabledelayedexpansion
 
:: ================= Config =================
set "USER=dell"
set "PASS=xd123456@.."
set "IP=1.15.17.182"
set "PORT=10000"
set "DIST_DIR=dist"
set "REMOTE_DIR=/home/vue/newpro-product"
set "SSH_OPTS=-o StrictHostKeyChecking=no -o ConnectTimeout=10"
:: ===========================================
 
echo ==========================================
echo        Vue Dist Auto Deploy Script
echo ==========================================
echo.
 
:: Check if sshpass is available
where sshpass >nul 2>&1
if %errorlevel% neq 0 (
    echo [WARN] sshpass not found, trying to install...
    echo.
 
    where bash >nul 2>&1
    if %errorlevel% equ 0 (
        echo [INFO] Git Bash detected, trying pacman install...
        bash -c "pacman -S --noconfirm sshpass" 2>nul
        if !errorlevel! equ 0 (
            echo [OK] sshpass installed via pacman!
            goto :check_dist
        )
    )
 
    set "SSHPASS_DIR=%~dp0bin"
    if not exist "!SSHPASS_DIR!" mkdir "!SSHPASS_DIR!"
 
    echo [INFO] Downloading sshpass...
    powershell -NoProfile -Command "$url='https://github.com/xhcoding/sshpass-win32/releases/download/v1.0.3/sshpass.exe'; $out='!SSHPASS_DIR!\sshpass.exe'; Write-Host \"Downloading to: $out\"; try { Invoke-WebRequest -Uri $url -OutFile $out -UseBasicParsing; exit 0 } catch { Write-Host $_.Exception.Message; exit 1 }"
    if !errorlevel! neq 0 (
        echo [ERROR] Failed to download sshpass!
        echo [HINT] Please install sshpass manually or use Git Bash: pacman -S sshpass
        goto :error_exit
    )
 
    set "PATH=!SSHPASS_DIR!;%PATH%"
    echo [OK] sshpass downloaded to: !SSHPASS_DIR!
    echo.
)
 
:check_dist
if not exist "%DIST_DIR%" (
    echo [ERROR] Dist directory not found: %DIST_DIR%
    echo [HINT] Please run 'npm run build' first!
    goto :error_exit
)
 
:: Count files and calculate size
set "FILE_COUNT=0"
set "TOTAL_SIZE=0"
for /r "%DIST_DIR%" %%F in (*) do (
    set /a "FILE_COUNT+=1"
    set /a "TOTAL_SIZE+=%%~zF"
)
set /a "TOTAL_SIZE_MB=TOTAL_SIZE/1024/1024"
 
echo [INFO] Dist directory: %DIST_DIR%
echo [INFO] Files count: %FILE_COUNT%
echo [INFO] Total size: %TOTAL_SIZE_MB% MB
echo [INFO] Target: %USER%@%IP%:%PORT%
echo [INFO] Remote dir: %REMOTE_DIR%
echo.
 
:: Compress dist to zip
set "ZIP_FILE=dist.zip"
echo [STEP 1/3] Compressing dist to %ZIP_FILE%...
if exist "%ZIP_FILE%" del "%ZIP_FILE%"
powershell -NoProfile -Command "Compress-Archive -Path '%DIST_DIR%\*' -DestinationPath '%ZIP_FILE%' -CompressionLevel Optimal"
if %errorlevel% neq 0 (
    echo [ERROR] Compression failed!
    goto :error_exit
)
for %%F in ("%ZIP_FILE%") do set "ZIP_SIZE_MB=%%~zF"
set /a "ZIP_SIZE_MB=ZIP_SIZE_MB/1024/1024"
echo [OK] Compressed: %ZIP_SIZE_MB% MB
echo.
 
:: Upload zip file
echo [STEP 2/3] Uploading %ZIP_FILE%...
sshpass -p "%PASS%" scp -P %PORT% %SSH_OPTS% "%ZIP_FILE%" %USER%@%IP%:%REMOTE_DIR%/
if %errorlevel% neq 0 (
    echo [ERROR] Upload failed! Check network or credentials.
    goto :error_exit
)
echo [OK] Upload completed!
echo.
 
:: Extract on remote server
echo [STEP 3/3] Extracting on remote server...
sshpass -p "%PASS%" ssh -p %PORT% %SSH_OPTS% %USER%@%IP% "cd '%REMOTE_DIR%' && rm -rf dist && unzip -o '%ZIP_FILE%' -d dist && rm -f '%ZIP_FILE%'"
if %errorlevel% neq 0 (
    echo [ERROR] Extraction failed!
    goto :error_exit
)
echo [OK] Extraction completed!
 
:: Cleanup local zip
del "%ZIP_FILE%"
 
echo.
echo ==========================================
echo [SUCCESS] Deploy completed!
echo ==========================================
goto :end
 
:error_exit
echo.
echo ==========================================
echo [FAILED] Deploy failed!
echo ==========================================
 
:end
pause