fix-token-config.ps1 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. # fix-token-config.ps1 (ASCII-only comments to avoid PS5 encoding issues)
  2. # Strategy: find "tokenConfig" key, cut back to last } before it, re-inject from UTF-8 file
  3. param(
  4. [string]$SkillsRoot = (Split-Path $PSScriptRoot -Parent),
  5. [string]$InjectionFile = (Join-Path $PSScriptRoot "token-config-injection.txt"),
  6. [switch]$DryRun
  7. )
  8. $utf8NoBom = New-Object System.Text.UTF8Encoding($false)
  9. if (-not (Test-Path $InjectionFile)) {
  10. Write-Error "Injection file not found: $InjectionFile"
  11. exit 1
  12. }
  13. $injection = [System.IO.File]::ReadAllText($InjectionFile, [System.Text.Encoding]::UTF8)
  14. $injection = $injection.TrimEnd()
  15. $categories = @("voc","social-media","competitor-analysis","review-analysis","synthesis","social-voc","douyin")
  16. $fixed = 0
  17. $skipped = 0
  18. foreach ($cat in $categories) {
  19. $catPath = Join-Path $SkillsRoot $cat
  20. if (-not (Test-Path $catPath)) { continue }
  21. Get-ChildItem -Recurse -Filter "api-config.json" -Path $catPath | ForEach-Object {
  22. $filePath = $_.FullName
  23. $relPath = $filePath.Replace($SkillsRoot + '\', '')
  24. $content = Get-Content -Path $filePath -Raw -Encoding UTF8
  25. if ([string]::IsNullOrEmpty($content)) {
  26. Write-Warning " [WARN] empty: $relPath"
  27. $skipped++
  28. return
  29. }
  30. $searchKey = [string]'"tokenConfig"'
  31. $tcIdx = $content.IndexOf($searchKey)
  32. if ($tcIdx -lt 0) {
  33. Write-Host " [SKIP] no tokenConfig: $relPath" -ForegroundColor DarkGray
  34. $skipped++
  35. return
  36. }
  37. $cutPos = $content.LastIndexOf([string]'}', $tcIdx - 1)
  38. if ($cutPos -lt 0) {
  39. Write-Warning " [WARN] no cut point: $relPath"
  40. $skipped++
  41. return
  42. }
  43. $base = $content.Substring(0, $cutPos + 1).TrimEnd()
  44. $newContent = $base + ",`r`n" + $injection + "`r`n}"
  45. if ($DryRun) {
  46. Write-Host " [DRY] $relPath" -ForegroundColor Yellow
  47. } else {
  48. [System.IO.File]::WriteAllText($filePath, $newContent, $utf8NoBom)
  49. Write-Host " [OK] $relPath" -ForegroundColor Green
  50. }
  51. $fixed++
  52. }
  53. }
  54. Write-Host ""
  55. Write-Host "Done: $fixed fixed, $skipped skipped"
  56. if ($DryRun) { Write-Host "Remove -DryRun to apply." -ForegroundColor Yellow }