-
Notifications
You must be signed in to change notification settings - Fork 130
Expand file tree
/
Copy pathoutput.ts
More file actions
150 lines (139 loc) · 5.39 KB
/
output.ts
File metadata and controls
150 lines (139 loc) · 5.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import { AppError, normalizeError, type NormalizedError } from './errors.ts';
import { buildSnapshotDisplayLines, formatSnapshotLine } from './snapshot-lines.ts';
import type { SnapshotNode } from './snapshot.ts';
import { styleText } from 'node:util';
type JsonResult =
| { success: true; data?: Record<string, unknown> }
| {
success: false;
error: {
code: string;
message: string;
hint?: string;
diagnosticId?: string;
logPath?: string;
details?: Record<string, unknown>;
};
};
export function printJson(result: JsonResult): void {
process.stdout.write(`${JSON.stringify(result, null, 2)}\n`);
}
export function printHumanError(
err: AppError | NormalizedError,
options: { showDetails?: boolean } = {},
): void {
const normalized = err instanceof AppError ? normalizeError(err) : err;
process.stderr.write(`Error (${normalized.code}): ${normalized.message}\n`);
if (normalized.hint) {
process.stderr.write(`Hint: ${normalized.hint}\n`);
}
if (normalized.diagnosticId) {
process.stderr.write(`Diagnostic ID: ${normalized.diagnosticId}\n`);
}
if (normalized.logPath) {
process.stderr.write(`Diagnostics Log: ${normalized.logPath}\n`);
}
if (options.showDetails && normalized.details) {
process.stderr.write(`${JSON.stringify(normalized.details, null, 2)}\n`);
}
}
type SnapshotDiffLine = {
kind?: 'added' | 'removed' | 'unchanged';
text?: string;
};
export function formatSnapshotText(
data: Record<string, unknown>,
options: { raw?: boolean; flatten?: boolean } = {},
): string {
const rawNodes = data.nodes;
const nodes = Array.isArray(rawNodes) ? (rawNodes as SnapshotNode[]) : [];
const truncated = Boolean(data.truncated);
const appName = typeof data.appName === 'string' ? data.appName : undefined;
const appBundleId = typeof data.appBundleId === 'string' ? data.appBundleId : undefined;
const meta: string[] = [];
if (appName) meta.push(`Page: ${appName}`);
if (appBundleId) meta.push(`App: ${appBundleId}`);
const header = `Snapshot: ${nodes.length} nodes${truncated ? ' (truncated)' : ''}`;
const prefix = meta.length > 0 ? `${meta.join('\n')}\n` : '';
if (nodes.length === 0) {
return `${prefix}${header}\n`;
}
if (options.raw) {
const rawLines = nodes.map((node) => JSON.stringify(node));
return `${prefix}${header}\n${rawLines.join('\n')}\n`;
}
if (options.flatten) {
const flatLines = nodes.map((node) => formatSnapshotLine(node, 0, false));
return `${prefix}${header}\n${flatLines.join('\n')}\n`;
}
const lines = buildSnapshotDisplayLines(nodes).map((line) => line.text);
return `${prefix}${header}\n${lines.join('\n')}\n`;
}
export function formatSnapshotDiffText(data: Record<string, unknown>): string {
const baselineInitialized = data.baselineInitialized === true;
const summaryRaw = (data.summary ?? {}) as Record<string, unknown>;
const additions = toNumber(summaryRaw.additions);
const removals = toNumber(summaryRaw.removals);
const unchanged = toNumber(summaryRaw.unchanged);
const useColor = supportsColor();
if (baselineInitialized) {
return `Baseline initialized (${unchanged} lines).\n`;
}
const rawLines = Array.isArray(data.lines) ? (data.lines as SnapshotDiffLine[]) : [];
const contextLines = applyContextWindow(rawLines, 1);
const lines = contextLines.map((line) => {
const text = typeof line.text === 'string' ? line.text : '';
if (line.kind === 'added') {
const prefix = text.startsWith(' ') ? `+${text}` : `+ ${text}`;
return useColor ? colorize(prefix, 'green') : prefix;
}
if (line.kind === 'removed') {
const prefix = text.startsWith(' ') ? `-${text}` : `- ${text}`;
return useColor ? colorize(prefix, 'red') : prefix;
}
return useColor ? colorize(text, 'dim') : text;
});
const body = lines.length > 0 ? `${lines.join('\n')}\n` : '';
if (!useColor) {
return `${body}${additions} additions, ${removals} removals, ${unchanged} unchanged\n`;
}
const summary = [
`${colorize(String(additions), 'green')} additions`,
`${colorize(String(removals), 'red')} removals`,
`${colorize(String(unchanged), 'dim')} unchanged`,
].join(', ');
return `${body}${summary}\n`;
}
function toNumber(value: unknown): number {
return typeof value === 'number' && Number.isFinite(value) ? value : 0;
}
function applyContextWindow(lines: SnapshotDiffLine[], contextWindow: number): SnapshotDiffLine[] {
if (lines.length === 0) return lines;
const changedIndices = lines
.map((line, index) => ({ index, kind: line.kind }))
.filter((entry) => entry.kind === 'added' || entry.kind === 'removed')
.map((entry) => entry.index);
if (changedIndices.length === 0) return lines;
const keep = new Array<boolean>(lines.length).fill(false);
for (const index of changedIndices) {
const start = Math.max(0, index - contextWindow);
const end = Math.min(lines.length - 1, index + contextWindow);
for (let i = start; i <= end; i += 1) {
keep[i] = true;
}
}
return lines.filter((_, index) => keep[index]);
}
function supportsColor(): boolean {
const forceColor = process.env.FORCE_COLOR;
if (typeof forceColor === 'string') {
return forceColor !== '0';
}
if (typeof process.env.NO_COLOR === 'string') {
return false;
}
return Boolean(process.stdout.isTTY);
}
function colorize(text: string, format: Parameters<typeof styleText>[0]): string {
return styleText(format, text);
}