Skip to content

Commit 134028c

Browse files
Create collect-type-errors.ps1
1 parent 960e243 commit 134028c

1 file changed

Lines changed: 110 additions & 0 deletions

File tree

scripts/collect-type-errors.ps1

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
# PowerShell script to run type-check and save errors to JSON
2+
# Usage: .\scripts\collect-type-errors.ps1
3+
4+
$ErrorActionPreference = "Continue"
5+
$outputFile = "typescript-errors.json"
6+
7+
Write-Host "Running TypeScript type check..." -ForegroundColor Cyan
8+
9+
# Capture both stdout and stderr
10+
$output = & npm run type-check 2>&1
11+
12+
# Parse the output
13+
$errors = @()
14+
$summary = @{
15+
timestamp = Get-Date -Format "yyyy-MM-ddTHH:mm:ssZ"
16+
command = "npm run type-check"
17+
exitCode = $LASTEXITCODE
18+
totalLines = $output.Count
19+
}
20+
21+
# Process output lines
22+
$currentError = $null
23+
foreach ($line in $output) {
24+
$lineText = $line.ToString()
25+
26+
# Detect error lines (file:line:col - error TS####:)
27+
if ($lineText -match '^(.+?)\((\d+),(\d+)\):\s+(error|warning)\s+(TS\d+):\s*(.*)$') {
28+
if ($currentError) {
29+
$errors += $currentError
30+
}
31+
32+
$currentError = @{
33+
file = $Matches[1]
34+
line = [int]$Matches[2]
35+
column = [int]$Matches[3]
36+
severity = $Matches[4]
37+
code = $Matches[5]
38+
message = $Matches[6].Trim()
39+
fullText = $lineText
40+
}
41+
}
42+
# Alternative format: src/file.ts:line:col - error TS####:
43+
elseif ($lineText -match '^(.+?):(\d+):(\d+)\s+-\s+(error|warning)\s+(TS\d+):\s*(.*)$') {
44+
if ($currentError) {
45+
$errors += $currentError
46+
}
47+
48+
$currentError = @{
49+
file = $Matches[1]
50+
line = [int]$Matches[2]
51+
column = [int]$Matches[3]
52+
severity = $Matches[4]
53+
code = $Matches[5]
54+
message = $Matches[6].Trim()
55+
fullText = $lineText
56+
}
57+
}
58+
# Continuation of previous error message
59+
elseif ($currentError -and $lineText -match '^\s+(.+)$') {
60+
$currentError.message += " " + $Matches[1].Trim()
61+
}
62+
}
63+
64+
# Add last error if exists
65+
if ($currentError) {
66+
$errors += $currentError
67+
}
68+
69+
# Extract summary information
70+
$foundErrors = 0
71+
$foundWarnings = 0
72+
foreach ($line in $output) {
73+
$lineText = $line.ToString()
74+
if ($lineText -match 'Found (\d+) error[s]?') {
75+
$foundErrors = [int]$Matches[1]
76+
}
77+
if ($lineText -match 'Found (\d+) warning[s]?') {
78+
$foundWarnings = [int]$Matches[1]
79+
}
80+
}
81+
82+
# Build final JSON structure
83+
$result = @{
84+
summary = @{
85+
timestamp = $summary.timestamp
86+
command = $summary.command
87+
exitCode = $summary.exitCode
88+
totalErrors = if ($foundErrors -gt 0) { $foundErrors } else { $errors.Count }
89+
totalWarnings = $foundWarnings
90+
totalLines = $summary.totalLines
91+
}
92+
errors = $errors
93+
rawOutput = $output | ForEach-Object { $_.ToString() }
94+
}
95+
96+
# Convert to JSON and save
97+
$jsonOutput = $result | ConvertTo-Json -Depth 10
98+
99+
try {
100+
$jsonOutput | Out-File -FilePath $outputFile -Encoding UTF8
101+
Write-Host "`nType check results saved to $outputFile" -ForegroundColor Green
102+
Write-Host "Total errors found: $($result.summary.totalErrors)" -ForegroundColor $(if ($result.summary.totalErrors -gt 0) { "Red" } else { "Green" })
103+
Write-Host "Total warnings found: $($result.summary.totalWarnings)" -ForegroundColor Yellow
104+
} catch {
105+
Write-Host "Error saving to file: $_" -ForegroundColor Red
106+
exit 1
107+
}
108+
109+
# Exit with same code as type-check
110+
exit $summary.exitCode

0 commit comments

Comments
 (0)