Files
makemd/scripts/migrate-types.ps1

130 lines
4.0 KiB
PowerShell
Raw Permalink Normal View History

#!/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