feat(types): 添加类型检查脚本和迁移指南

添加 check-types.ps1 脚本用于检查项目各模块的类型定义
添加 migrate-types.ps1 脚本用于自动迁移类型导入路径
添加类型迁移指南文档说明迁移步骤和最佳实践
添加共享类型测试用例验证 schema 定义
This commit is contained in:
2026-03-20 18:00:31 +08:00
parent 427becbc8f
commit 888d3844f3
4 changed files with 837 additions and 0 deletions

85
scripts/check-types.ps1 Normal file
View File

@@ -0,0 +1,85 @@
#!/usr/bin/env pwsh
# check-types.ps1 - 类型检查脚本
# 用法: ./scripts/check-types.ps1
$ErrorActionPreference = "Stop"
$ProjectRoot = Split-Path -Parent $PSScriptRoot
Write-Host "========================================" -ForegroundColor Cyan
Write-Host " TypeScript Type Check Script" -ForegroundColor Cyan
Write-Host "========================================" -ForegroundColor Cyan
Write-Host ""
$ServerExit = 0
$DashboardExit = 0
$ExtensionExit = 0
$NodeAgentExit = 0
# 检查 server
Write-Host "[1/4] Checking server types..." -ForegroundColor Yellow
Set-Location "$ProjectRoot/server"
try {
npx tsc --noEmit --skipLibCheck 2>&1 | Out-Null
Write-Host " ✅ Server types OK" -ForegroundColor Green
} catch {
Write-Host " ❌ Server types FAILED" -ForegroundColor Red
npx tsc --noEmit --skipLibCheck 2>&1
$ServerExit = 1
}
# 检查 dashboard
Write-Host "[2/4] Checking dashboard types..." -ForegroundColor Yellow
Set-Location "$ProjectRoot/dashboard"
try {
npx tsc --noEmit --skipLibCheck 2>&1 | Out-Null
Write-Host " ✅ Dashboard types OK" -ForegroundColor Green
} catch {
Write-Host " ❌ Dashboard types FAILED" -ForegroundColor Red
npx tsc --noEmit --skipLibCheck 2>&1
$DashboardExit = 1
}
# 检查 extension
Write-Host "[3/4] Checking extension types..." -ForegroundColor Yellow
Set-Location "$ProjectRoot/extension"
try {
npx tsc --noEmit --skipLibCheck 2>&1 | Out-Null
Write-Host " ✅ Extension types OK" -ForegroundColor Green
} catch {
Write-Host " ❌ Extension types FAILED" -ForegroundColor Red
npx tsc --noEmit --skipLibCheck 2>&1
$ExtensionExit = 1
}
# 检查 node-agent
Write-Host "[4/4] Checking node-agent types..." -ForegroundColor Yellow
if (Test-Path "$ProjectRoot/node-agent/tsconfig.json") {
Set-Location "$ProjectRoot/node-agent"
try {
npx tsc --noEmit --skipLibCheck 2>&1 | Out-Null
Write-Host " ✅ Node-agent types OK" -ForegroundColor Green
} catch {
Write-Host " ❌ Node-agent types FAILED" -ForegroundColor Red
npx tsc --noEmit --skipLibCheck 2>&1
$NodeAgentExit = 1
}
} else {
Write-Host " ⏭️ Node-agent skipped (no tsconfig.json)" -ForegroundColor Gray
}
Write-Host ""
Write-Host "========================================" -ForegroundColor Cyan
$TotalErrors = $ServerExit + $DashboardExit + $ExtensionExit + $NodeAgentExit
if ($TotalErrors -eq 0) {
Write-Host " ✅ All type checks passed!" -ForegroundColor Green
Write-Host "========================================" -ForegroundColor Cyan
Set-Location $ProjectRoot
exit 0
} else {
Write-Host " ❌ Type check failed with $TotalErrors error(s)" -ForegroundColor Red
Write-Host "========================================" -ForegroundColor Cyan
Set-Location $ProjectRoot
exit 1
}

129
scripts/migrate-types.ps1 Normal file
View File

@@ -0,0 +1,129 @@
#!/usr/bin/env pwsh
# migrate-types.ps1 - 类型导入迁移脚本
# 用法: ./scripts/migrate-types.ps1 [-DryRun]
param(
[switch]$DryRun
)
$ErrorActionPreference = "Stop"
$ProjectRoot = Split-Path -Parent $PSScriptRoot
Write-Host "========================================" -ForegroundColor Cyan
Write-Host " Type Import Migration Script" -ForegroundColor Cyan
Write-Host "========================================" -ForegroundColor Cyan
Write-Host ""
if ($DryRun) {
Write-Host "🔍 DRY RUN MODE - No files will be modified" -ForegroundColor Yellow
Write-Host ""
}
# 定义迁移规则
$MigrationRules = @(
@{
Pattern = "from ['`"]\.\.\/models\/User['`"]"
Replacement = "from '@shared/types'"
Description = "User model import"
},
@{
Pattern = "from ['`"]\.\.\/models\/Product['`"]"
Replacement = "from '@shared/types'"
Description = "Product model import"
},
@{
Pattern = "from ['`"]\.\.\/models\/Order['`"]"
Replacement = "from '@shared/types'"
Description = "Order model import"
},
@{
Pattern = "from ['`"]\.\.\/types\/user['`"]"
Replacement = "from '@shared/types'"
Description = "User type import (frontend)"
},
@{
Pattern = "from ['`"]\.\.\/types\/product['`"]"
Replacement = "from '@shared/types'"
Description = "Product type import (frontend)"
},
@{
Pattern = "from ['`"]\.\.\/types\/order['`"]"
Replacement = "from '@shared/types'"
Description = "Order type import (frontend)"
}
)
$TotalFiles = 0
$TotalChanges = 0
# 迁移后端文件
Write-Host "[1/2] Migrating server files..." -ForegroundColor Yellow
$ServerFiles = Get-ChildItem -Path "$ProjectRoot/server/src" -Include "*.ts" -Recurse
foreach ($File in $ServerFiles) {
$Content = Get-Content $File.FullName -Raw
$OriginalContent = $Content
$FileChanged = $false
foreach ($Rule in $MigrationRules) {
if ($Content -match $Rule.Pattern) {
$Content = $Content -replace $Rule.Pattern, $Rule.Replacement
Write-Host " 📝 $($File.Name): $($Rule.Description)" -ForegroundColor Gray
$FileChanged = $true
}
}
if ($FileChanged) {
$TotalFiles++
$TotalChanges++
if (-not $DryRun) {
Set-Content -Path $File.FullName -Value $Content -NoNewline
}
}
}
# 迁移前端文件
Write-Host "[2/2] Migrating dashboard files..." -ForegroundColor Yellow
$DashboardFiles = Get-ChildItem -Path "$ProjectRoot/dashboard/src" -Include "*.ts", "*.tsx" -Recurse
foreach ($File in $DashboardFiles) {
$Content = Get-Content $File.FullName -Raw
$OriginalContent = $Content
$FileChanged = $false
foreach ($Rule in $MigrationRules) {
if ($Content -match $Rule.Pattern) {
$Content = $Content -replace $Rule.Pattern, $Rule.Replacement
Write-Host " 📝 $($File.Name): $($Rule.Description)" -ForegroundColor Gray
$FileChanged = $true
}
}
if ($FileChanged) {
$TotalFiles++
$TotalChanges++
if (-not $DryRun) {
Set-Content -Path $File.FullName -Value $Content -NoNewline
}
}
}
Write-Host ""
Write-Host "========================================" -ForegroundColor Cyan
Write-Host " Migration Summary" -ForegroundColor Cyan
Write-Host "========================================" -ForegroundColor Cyan
Write-Host " Files processed: $TotalFiles" -ForegroundColor White
Write-Host " Total changes: $TotalChanges" -ForegroundColor White
if ($DryRun) {
Write-Host ""
Write-Host " This was a DRY RUN. Run without -DryRun to apply changes." -ForegroundColor Yellow
} else {
Write-Host ""
Write-Host " ✅ Migration complete!" -ForegroundColor Green
Write-Host " Please run: ./scripts/check-types.ps1" -ForegroundColor Yellow
}
Write-Host "========================================" -ForegroundColor Cyan