Files
TodoList/restart-web.ps1
ShaoHua ceb77e624e feat:基础功能实现
feat: 重构 TodoList 架构,新增动态 API 与 MAUI 内嵌 Web 服务
feat:优化交互逻辑,优化发布流程
2026-04-05 00:53:42 +08:00

102 lines
3.2 KiB
PowerShell

param(
[switch]$Force = $false
)
$ErrorActionPreference = "Stop"
Write-Host "====================================" -ForegroundColor Cyan
Write-Host " TodoList.Web Restart Script" -ForegroundColor Cyan
Write-Host "====================================" -ForegroundColor Cyan
Write-Host ""
$webProjectPath = Join-Path $PSScriptRoot "src\TodoList.Web"
if (!(Test-Path $webProjectPath)) {
Write-Host "ERROR: Web project path not found: $webProjectPath" -ForegroundColor Red
exit 1
}
function Get-TodoListWebProcesses {
param(
[Parameter(Mandatory = $true)]
[string]$WebProjectPath
)
Get-CimInstance Win32_Process -Filter "Name='node.exe'" | Where-Object {
$_.CommandLine -and
$_.CommandLine -like "*vite*" -and
$_.CommandLine -like "*$WebProjectPath*"
}
}
Write-Host "[1/3] Stopping existing service..." -ForegroundColor Yellow
$webProcesses = @(Get-TodoListWebProcesses -WebProjectPath $webProjectPath)
if ($webProcesses.Count -eq 0) {
Write-Host "OK: TodoList.Web is not running" -ForegroundColor Green
} else {
Write-Host "Found $($webProcesses.Count) process(es)" -ForegroundColor Yellow
foreach ($process in $webProcesses) {
$processId = $process.ProcessId
Write-Host "Stopping PID: $processId" -ForegroundColor Gray
try {
Stop-Process -Id $processId -Force:$Force -ErrorAction Stop
Write-Host "OK: Stopped PID: $processId" -ForegroundColor Green
} catch {
Write-Host "WARN: Failed to stop PID: $processId. $_" -ForegroundColor Yellow
}
}
}
Write-Host ""
Write-Host "[2/3] Waiting for processes to exit..." -ForegroundColor Yellow
$timeout = 10
$elapsed = 0
while ($elapsed -lt $timeout) {
$webRunning = @(Get-TodoListWebProcesses -WebProjectPath $webProjectPath)
if ($webRunning.Count -eq 0) {
Write-Host "OK: All processes exited" -ForegroundColor Green
break
}
Start-Sleep -Seconds 1
$elapsed++
if ($elapsed -lt $timeout) {
Write-Host "Waiting... ($elapsed/$timeout sec)" -ForegroundColor Gray
}
}
if ($elapsed -ge $timeout) {
Write-Host "WARN: Timeout reached, continuing to start..." -ForegroundColor Yellow
}
Write-Host ""
Write-Host "[3/3] Starting service..." -ForegroundColor Yellow
try {
Write-Host "Working directory: $webProjectPath" -ForegroundColor Gray
Write-Host "Running: npm run dev" -ForegroundColor Green
$webProcess = Start-Process -FilePath "npm.cmd" -ArgumentList @("run", "dev") -WorkingDirectory $webProjectPath -PassThru
Write-Host "OK: Started TodoList.Web" -ForegroundColor Green
Write-Host "PID: $($webProcess.Id)" -ForegroundColor Gray
} catch {
Write-Host "ERROR: Failed to start service. $_" -ForegroundColor Red
exit 1
}
Write-Host ""
Write-Host "====================================" -ForegroundColor Cyan
Write-Host " Done" -ForegroundColor Green
Write-Host "====================================" -ForegroundColor Cyan
Write-Host ""
Write-Host "Notes:" -ForegroundColor Yellow
Write-Host " - Vite will choose an available port automatically (no explicit port required)" -ForegroundColor Gray
Write-Host " - Check the npm/vite output for the Local URL" -ForegroundColor Gray