DevOps/launcher.ps1
2026-03-23 18:27:26 +08:00

234 lines
10 KiB
PowerShell
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# ==============================================================================
# LAUNCHER.PS1 - Обработка файлов для Node.js (Windows PowerShell)
# Использует: Node.js + strip-comments для безопасной очистки
# Поддерживает: .js, .css, .ejs
# ВАЖНО: Файл создан с кодировкой UTF-8 BOM
# ==============================================================================
# --- Настройка путей ---
$ScriptDirectory = Split-Path -Parent $MyInvocation.MyCommand.Path
$DevDirectory = Join-Path $ScriptDirectory "!DEV"
$InputDirectory = Join-Path $ScriptDirectory "!DEV\input"
$OutputDirectory = Join-Path $ScriptDirectory "!DEV\output"
$CombinedFile = Join-Path $OutputDirectory "Combined_All_Files.txt"
$CleanerScript = Join-Path $ScriptDirectory "cleaner.js"
# --- Настройки ---
$Global:RemoveComments = $true
$SupportedExts = @('.js', '.css', '.ejs')
# --- Цвета ---
$C_CYAN = [ConsoleColor]::Cyan; $C_YELLOW = [ConsoleColor]::Yellow
$C_GREEN = [ConsoleColor]::Green; $C_RED = [ConsoleColor]::Red
$C_WHITE = [ConsoleColor]::White; $C_GRAY = [ConsoleColor]::DarkGray
$C_MAGENTA = [ConsoleColor]::Magenta
function Write-Color {
param([string]$Text, $Color = $C_WHITE)
Write-Host $Text -ForegroundColor $Color
}
# --- Проверка зависимостей ---
function Check-Deps {
if (-not (Get-Command node -ErrorAction SilentlyContinue)) {
Write-Color "[ERR] Node.js не установлен!" $C_RED
return $false
}
if (-not (Test-Path $CleanerScript)) {
Write-Color "[ERR] Не найден: cleaner.js" $C_RED
return $false
}
return $true
}
# --- Создание папок ---
function Setup-Folders {
if (-not (Test-Path $DevDirectory)) {
New-Item -ItemType Directory -Force -Path $InputDirectory,$OutputDirectory | Out-Null
Write-Color "[OK] Папка !DEV создана" $C_GREEN
} else {
if (-not (Test-Path $InputDirectory)) { New-Item -ItemType Directory -Force -Path $InputDirectory | Out-Null }
if (-not (Test-Path $OutputDirectory)) { New-Item -ItemType Directory -Force -Path $OutputDirectory | Out-Null }
}
}
# --- Очистка комментариев через Node.js ---
function Clean-AllComments {
Write-Color "=== ОЧИСТКА КОММЕНТАРИЕВ (.js/.css/.ejs) ===" $C_CYAN
Write-Color "Режим: $(if ($Global:RemoveComments) { 'ВКЛ' } else { 'ВЫКЛ' })" $C_GRAY
Write-Host ""
if (-not $Global:RemoveComments) { Write-Color "Очистка отключена" $C_YELLOW; Write-Host ""; return }
if (-not (Check-Deps)) { return }
if (-not (Test-Path $InputDirectory)) { Write-Color "[ERR] Папка не найдена" $C_RED; return }
$result = & node $CleanerScript dir $InputDirectory 2>&1 | Out-String
Write-Host $result
Write-Color "[OK] Очистка завершена" $C_GREEN
Write-Host ""
}
# --- Конвертация в .txt ---
function Convert-ToTxt {
Write-Color "=== КОНВЕРТАЦИЯ В .TXT ===" $C_CYAN; Write-Host ""
if (-not (Test-Path $InputDirectory)) { Write-Color "[ERR] Папка не найдена" $C_RED; return }
$Count = 0
Get-ChildItem -Path $InputDirectory -File -Recurse -ErrorAction SilentlyContinue |
Where-Object { $SupportedExts -contains $_.Extension -and $_.Extension -ne '.txt' } |
ForEach-Object {
$NewName = "$($_.BaseName).txt"
try {
Rename-Item -Path $_.FullName -NewName $NewName -Force
Write-Color "[OK] $($_.Name)" $C_GREEN
$Count++
} catch { Write-Color "[ERR] $($_.Name)" $C_RED }
}
Write-Color "Конвертировано: $Count" $C_WHITE; Write-Host ""
}
# --- Объединение файлов ---
function Combine-Files {
Write-Color "=== ОБЪЕДИНЕНИЕ ФАЙЛОВ ===" $C_CYAN; Write-Host ""
if (-not (Test-Path $InputDirectory)) { Write-Color "[ERR] Папка не найдена" $C_RED; return }
$TxtFiles = Get-ChildItem -Path $InputDirectory -Filter "*.txt" -File -Recurse -ErrorAction SilentlyContinue
if (-not $TxtFiles) { Write-Color "[ERR] Нет .txt файлов" $C_RED; return }
Write-Color "Найдено: $($TxtFiles.Count)" $C_WHITE; Write-Host ""
if (-not (Test-Path $OutputDirectory)) { New-Item -ItemType Directory -Force -Path $OutputDirectory | Out-Null }
$Header = @(
"ОБЪЕДИНЁННЫЙ ФАЙЛ (Node.js)",
"Создан: $(Get-Date -Format 'yyyy-MM-dd HH:mm:ss')",
"Источник: $InputDirectory",
"Файлов: $($TxtFiles.Count)",
"Комментарии: $(if ($Global:RemoveComments) { 'УДАЛЕНЫ' } else { 'ОСТАВЛЕНЫ' })",
""
)
$Header | Set-Content -Path $CombinedFile -Encoding UTF8
foreach ($File in $TxtFiles | Sort-Object FullName) {
try {
("=" * 80) | Add-Content -Path $CombinedFile -Encoding UTF8
"ФАЙЛ: $($File.FullName)" | Add-Content -Path $CombinedFile -Encoding UTF8
("=" * 80) | Add-Content -Path $CombinedFile -Encoding UTF8
$Content = Get-Content -Path $File.FullName -Encoding UTF8 | Where-Object { $_ -match '\S' }
if ($Content) { $Content | Add-Content -Path $CombinedFile -Encoding UTF8 }
"" | Add-Content -Path $CombinedFile -Encoding UTF8
Write-Color "[OK] $($File.Name)" $C_GREEN
} catch { Write-Color "[ERR] $($File.Name)" $C_RED }
}
if (Test-Path $CombinedFile) {
$SizeKB = [math]::Round((Get-Item $CombinedFile).Length / 1KB, 2)
Write-Host ""; Write-Color "[OK] Создан: $CombinedFile ($SizeKB KB)" $C_GREEN
}
Write-Host ""
}
# --- Переключить очистку ---
function Toggle-Comments {
Write-Color "=== НАСТРОЙКА ===" $C_CYAN; Write-Host ""
$Global:RemoveComments = -not $Global:RemoveComments
$Status = if ($Global:RemoveComments) { "ВКЛ" } else { "ВЫКЛ" }
$Color = if ($Global:RemoveComments) { $C_GREEN } else { $C_YELLOW }
Write-Color "Очистка комментариев: $Status" $Color
Write-Host ""
}
# --- Сброс проекта ---
function Clean-All {
Write-Color "=== СБРОС ПРОЕКТА ===" $C_RED; Write-Host ""
if (Test-Path $DevDirectory) {
Remove-Item -Path $DevDirectory -Recurse -Force
Write-Color "[OK] !DEV удалена" $C_GREEN
}
Setup-Folders
Write-Color "[OK] Структура восстановлена" $C_GREEN
Write-Host ""
}
# --- Открыть папку ---
function Open-Folder {
if (Test-Path $DevDirectory) {
Start-Process "explorer.exe" $DevDirectory
Write-Color "[OK] Открыта !DEV" $C_GREEN
} else {
Write-Color "[ERR] !DEV не найдена" $C_RED
}
Write-Host ""
}
# --- Показать структуру ---
function Show-Structure {
Write-Color "=== СТРУКТУРА ===" $C_CYAN; Write-Host ""
Write-Color "Корень: $ScriptDirectory" $C_WHITE
if (Test-Path $DevDirectory) {
Write-Color "!DEV/" $C_YELLOW
$In = (Get-ChildItem $InputDirectory -File -Recurse -ErrorAction SilentlyContinue).Count
$Out = (Get-ChildItem $OutputDirectory -File -Recurse -ErrorAction SilentlyContinue).Count
Write-Color " input/ ($In файлов)" $C_GREEN
Write-Color " output/ ($Out файлов)" $C_GREEN
}
Write-Color "Поддержка: $($SupportedExts -join ', ')" $C_CYAN
$Status = if ($Global:RemoveComments) { "ВКЛ" } else { "ВЫКЛ" }
$Color = if ($Global:RemoveComments) { $C_GREEN } else { $C_RED }
Write-Color "Очистка: $Status" $Color
Write-Host ""
}
# --- Заголовок и меню ---
function Show-Header {
Clear-Host
Write-Color "==================================================" $C_CYAN
Write-Color " ОБРАБОТКА ФАЙЛОВ (Node.js + PowerShell)" $C_CYAN
Write-Color "==================================================" $C_CYAN
Write-Color "Скрипт: $ScriptDirectory\launcher.ps1" $C_GRAY
Write-Host ""
}
function Show-Menu {
Write-Color "Действия:" $C_YELLOW
Write-Color "1. [ОЧИСТКА] Удалить комментарии (.js/.css/.ejs)" $C_MAGENTA
Write-Color "2. [КОНВЕРТ] Конвертировать в .txt" $C_CYAN
Write-Color "3. [ОБЪЕДИН] Объединить .txt в один файл" $C_CYAN
Write-Color "4. [ВСЁ] Очистка -> Конвертация -> Объединение" $C_GREEN
Write-Color "5. [НАСТР] Очистка $(if ($Global:RemoveComments) { '[ВКЛ]' } else { '[ВЫКЛ]' })" $C_WHITE
Write-Color "6. [ПАПКА] Открыть !DEV" $C_MAGENTA
Write-Color "7. [ИНФО] Показать структуру" $C_WHITE
Write-Color "8. [СБРОС] Полная очистка" $C_RED
Write-Color "0. [ВЫХОД] Завершить" $C_RED
Write-Host ""
}
function Wait-ForUser {
Write-Host ""
Write-Color "Нажмите любую клавишу..." $C_GRAY
$null = $Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
}
# ==============================================================================
# === ЗАПУСК ===
# ==============================================================================
Setup-Folders
do {
Show-Header
Show-Menu
$Choice = Read-Host "Выберите (0-8)"
Write-Host ""
switch ($Choice) {
"1" { Clean-AllComments; Wait-ForUser }
"2" { Convert-ToTxt; Wait-ForUser }
"3" { Combine-Files; Wait-ForUser }
"4" { Clean-AllComments; Convert-ToTxt; Combine-Files; Wait-ForUser }
"5" { Toggle-Comments; Wait-ForUser }
"6" { Open-Folder; Wait-ForUser }
"7" { Show-Structure; Wait-ForUser }
"8" { Clean-All; Wait-ForUser }
"0" { Write-Color "Выход..." $C_YELLOW; exit }
default { Write-Color "Неверный выбор!" $C_RED; Start-Sleep -Seconds 2 }
}
} while ($true)