Files
2026-12-01 15:23:42 +08:00

52 lines
2.1 KiB
Batchfile

@echo off
rem ============================================================
rem Update photos in script.js
rem Scans assets\img for images and rewrites the photos array
rem between /* PHOTOS:START */ and /* PHOTOS:END */ markers.
rem Usage: update-photos.bat (interactive)
rem update-photos.bat --auto (no pause, for CI / scripts)
rem ============================================================
setlocal
cd /d "%~dp0"
set "AUTO=0"
if /i "%~1"=="--auto" set "AUTO=1"
if not exist "assets\img" (
echo [ERROR] assets\img folder not found.
if "%AUTO%"=="0" pause
exit /b 1
)
if not exist "script.js" (
echo [ERROR] script.js not found.
if "%AUTO%"=="0" pause
exit /b 1
)
powershell -NoProfile -ExecutionPolicy Bypass -Command ^
"$ErrorActionPreference='Stop';" ^
"$exts='.jpg','.jpeg','.png','.gif','.webp','.bmp','.avif';" ^
"$files=@(Get-ChildItem -LiteralPath 'assets/img' -File | Where-Object { $exts -contains $_.Extension.ToLower() } | Sort-Object Name);" ^
"if ($files.Count -eq 0) { Write-Host '[ERROR] No images found in assets/img'; exit 1 };" ^
"$q=[char]34;" ^
"$entries=@($files | ForEach-Object { ' { src: ' + $q + 'assets/img/' + $_.Name + $q + ' }' });" ^
"$nl=[Environment]::NewLine;" ^
"$block='/* PHOTOS:START */' + $nl + ' const photos = [' + $nl + ($entries -join (',' + $nl)) + $nl + ' ];' + $nl + ' /* PHOTOS:END */';" ^
"$content=Get-Content -LiteralPath 'script.js' -Raw -Encoding UTF8;" ^
"if ($content -notmatch 'PHOTOS:START') { Write-Host '[ERROR] PHOTOS markers not found in script.js'; exit 1 };" ^
"$pattern='(?s)/\* PHOTOS:START \*/.*?/\* PHOTOS:END \*/';" ^
"$new=[regex]::Replace($content, $pattern, $block.Replace('$','$$'));" ^
"[System.IO.File]::WriteAllText((Resolve-Path 'script.js').Path, $new, (New-Object System.Text.UTF8Encoding($false)));" ^
"Write-Host ('[OK] ' + $files.Count + ' image(s) written to script.js:');" ^
"$files | ForEach-Object { Write-Host (' - ' + $_.Name) }"
if errorlevel 1 (
echo.
echo Update failed.
if "%AUTO%"=="0" pause
exit /b 1
)
echo.
if "%AUTO%"=="0" pause