-
Notifications
You must be signed in to change notification settings - Fork 637
Add support for extendedDiagnostics json
output
#835
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
695bd55
7b2189a
3c263ea
5a36edd
d8be0e1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
package main | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
"time" | ||
) | ||
|
||
// max returns the larger of x or y | ||
func max(x, y int) int { | ||
Check failure on line 13 in cmd/tsgo/diagnostics.go
|
||
if x > y { | ||
return x | ||
} | ||
return y | ||
} | ||
|
||
// tableRow represents a single row in the diagnostic table | ||
type tableRow struct { | ||
name string | ||
value string | ||
} | ||
|
||
// table collects and formats diagnostic data | ||
type table struct { | ||
rows []tableRow | ||
} | ||
|
||
// add adds a statistic to the table, automatically formatting durations | ||
func (t *table) add(name string, value any) { | ||
if d, ok := value.(time.Duration); ok { | ||
value = formatDuration(d) | ||
} | ||
t.rows = append(t.rows, tableRow{name, fmt.Sprint(value)}) | ||
} | ||
|
||
// toJSON converts the table to a map suitable for JSON serialization | ||
func (t *table) toJSON() map[string]string { | ||
result := make(map[string]string) | ||
for _, row := range t.rows { | ||
result[row.name] = row.value | ||
} | ||
return result | ||
} | ||
|
||
// print outputs the table in a formatted way to stdout | ||
func (t *table) print() { | ||
nameWidth := 0 | ||
valueWidth := 0 | ||
for _, r := range t.rows { | ||
nameWidth = max(nameWidth, len(r.name)) | ||
valueWidth = max(valueWidth, len(r.value)) | ||
} | ||
|
||
for _, r := range t.rows { | ||
fmt.Printf("%-*s %*s\n", nameWidth+1, r.name+":", valueWidth, r.value) | ||
} | ||
} | ||
|
||
// formatDuration formats a duration in seconds with 3 decimal places | ||
func formatDuration(d time.Duration) string { | ||
return fmt.Sprintf("%.3fs", d.Seconds()) | ||
} | ||
|
||
// OutputStats handles the output of diagnostic statistics based on the extendedDiagnostics option | ||
// Returns any error encountered during the output process | ||
func OutputStats(stats *table, extendedDiagnostics string) error { | ||
|
||
if extendedDiagnostics == "" { | ||
return nil | ||
} | ||
|
||
if extendedDiagnostics == "inline" { | ||
stats.print() | ||
return nil | ||
} | ||
|
||
jsonData := stats.toJSON() | ||
|
||
if strings.HasSuffix(extendedDiagnostics, ".json") { | ||
// Ensure directory exists | ||
dir := filepath.Dir(extendedDiagnostics) | ||
if dir != "." { | ||
if err := os.MkdirAll(dir, 0755); err != nil { | ||
return fmt.Errorf("error creating directory for stats JSON file: %w", err) | ||
} | ||
} | ||
|
||
// Write to file | ||
file, err := os.Create(extendedDiagnostics) | ||
if err != nil { | ||
return fmt.Errorf("error creating stats JSON file: %w", err) | ||
} | ||
defer file.Close() | ||
|
||
enc := json.NewEncoder(file) | ||
enc.SetIndent("", " ") | ||
if err := enc.Encode(jsonData); err != nil { | ||
return fmt.Errorf("error writing stats to JSON file: %w", err) | ||
} | ||
return nil | ||
} | ||
|
||
return fmt.Errorf("invalid value for --extendedDiagnostics. Use 'inline' or a path ending with '.json'") | ||
Check failure on line 106 in cmd/tsgo/diagnostics.go
|
||
} |
Uh oh!
There was an error while loading. Please reload this page.