All files / src/lib/formatters output.ts

88.5% Statements 77/87
79.41% Branches 54/68
100% Functions 13/13
88.5% Lines 77/87

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494                                                                                                                                                      970x 66x     904x                                                                                                                                                                           702x                                                                                                                           92x 65x   27x     27x 13x 16x     16x 16x 16x   16x     14x 14x 20x   14x                     238x 1x 1x   237x                                                         716x 238x 146x 146x     146x 146x     92x   92x     238x 238x     478x 478x 458x 458x                                                       97x                                               729x 729x 729x 1458x 1458x     1458x 1376x     729x                     15141x   15141x         15141x 4587x 4587x   10554x 2320x 2320x     2320x   8234x     8234x 729x 729x 729x       7505x                           458x           458x       458x 458x       458x 6776x 6776x 6776x                                       143x     143x 143x 2101x 2101x 2101x   143x         190x               176x    
/**
 * Shared output utilities
 *
 * Handles the common pattern of JSON vs human-readable output
 * that appears in most CLI commands.
 *
 * Declare formatting in {@link OutputConfig} on `buildCommand`, then
 * yield data from the generator:
 * ```ts
 * buildCommand({
 *   output: { human: formatUser },
 *   async *func() { yield new CommandOutput(data); },
 * })
 * ```
 * The wrapper reads `json`/`fields` from flags and applies formatting
 * automatically. Generators return `{ hint }` for footer text.
 *
 * The same data object is serialized to JSON and passed to the human
 * formatter — there is no divergent-data path.
 */
 
import type { ZodType } from "zod";
import type { Writer } from "../../types/index.js";
import { plainSafeMuted } from "./human.js";
import { filterFields, formatJson } from "./json.js";
 
// ---------------------------------------------------------------------------
// Zero-copy object capture (library mode)
// ---------------------------------------------------------------------------
 
// ---------------------------------------------------------------------------
// Output config (declared on buildCommand)
// ---------------------------------------------------------------------------
 
/**
 * Stateful human renderer created once per command invocation.
 *
 * The wrapper calls `render()` once per yielded value and `finalize()`
 * once after the generator completes. This enables streaming commands
 * to maintain per-invocation rendering state (e.g., a table that needs
 * a header on first call and a footer on last).
 *
 * For stateless commands, `finalize` can be omitted — the wrapper falls
 * back to `writeFooter(hint)`.
 *
 * @typeParam T - The data type yielded by the command
 */
export type HumanRenderer<T> = {
  /** Render a single yielded data chunk as human-readable text. */
  render: (data: T) => string;
  /**
   * Called once after the generator completes. Returns the final output
   * string (e.g., a streaming table's bottom border + formatted hint).
   *
   * When defined, replaces the default `writeFooter(hint)` behavior —
   * the wrapper writes the returned string directly.
   *
   * When absent, the wrapper falls back to `writeFooter(hint)`.
   */
  finalize?: (hint?: string) => string;
};
 
/**
 * Resolve the `human` field of an {@link OutputConfig} into a
 * {@link HumanRenderer}. Supports two forms:
 *
 * 1. **Plain function** — `(data: T) => string` — auto-wrapped into a
 *    stateless renderer (no `finalize`).
 * 2. **Factory** — `() => HumanRenderer<T>` — called once per invocation
 *    to produce a renderer with optional `finalize()`.
 *
 * Disambiguation: a function with `.length === 0` is treated as a factory.
 */
export function resolveRenderer<T>(human: HumanOutput<T>): HumanRenderer<T> {
  // Factory: zero-arg function that returns a renderer
  if (human.length === 0) {
    return (human as () => HumanRenderer<T>)();
  }
  // Plain formatter: wrap in a stateless renderer
  return { render: human as (data: T) => string };
}
 
/**
 * Human rendering for an {@link OutputConfig}.
 *
 * Two forms:
 * - **Plain function** `(data: T) => string` — stateless, auto-wrapped.
 * - **Factory** `() => HumanRenderer<T>` — called per invocation for
 *   stateful renderers (e.g., streaming tables with `finalize()`).
 */
export type HumanOutput<T> = ((data: T) => string) | (() => HumanRenderer<T>);
 
/**
 * Output configuration declared on `buildCommand` for automatic rendering.
 *
 * When present, `--json` and `--fields` flags are injected and the wrapper
 * auto-renders yielded {@link CommandOutput} values.
 *
 * @typeParam T - Type of data the command yields (used by `human` formatter
 *   and serialized as-is to JSON)
 */
export type OutputConfig<T> = {
  /**
   * Human-readable renderer.
   *
   * Pass a plain `(data: T) => string` for stateless formatting, or a
   * zero-arg factory `() => HumanRenderer<T>` for stateful rendering
   * with `finalize()` support.
   */
  human: HumanOutput<T>;
  /**
   * Top-level keys to strip from JSON output.
   *
   * Use this for fields that exist only for the human formatter
   * (e.g. pre-formatted terminal strings) and should not appear
   * in the JSON contract.
   *
   * Ignored when {@link jsonTransform} is set — the transform is
   * responsible for shaping the final JSON output.
   */
  jsonExclude?: ReadonlyArray<keyof T & string>;
  /**
   * Custom JSON serialization transform.
   *
   * When set, replaces the default JSON output path entirely.
   * The function receives the raw command data and the parsed `--fields`
   * list, and returns the final object to serialize.
   *
   * This is useful for list commands that wrap items in a
   * `{ data, hasMore, nextCursor }` envelope and need per-element
   * field filtering rather than top-level filtering.
   *
   * When `jsonTransform` is set, `jsonExclude` is ignored.
   */
  jsonTransform?: (data: T, fields?: string[]) => unknown;
  /**
   * Zod schema describing the shape of JSON output (after transform/exclude).
   *
   * For list commands with {@link jsonTransform}, this should describe the
   * **item** type (what `--fields` operates on), not the envelope.
   *
   * Used for:
   * - `--help` output: appends available JSON fields to the command description
   * - `sentry help <cmd>`: structured field documentation
   * - `generate-skill.ts`: SKILL.md field tables for AI agents
   */
  schema?: ZodType;
};
 
/**
 * Yield type for commands with {@link OutputConfig}.
 *
 * Commands wrap each yielded value in this class so the `buildCommand`
 * wrapper can unambiguously detect data vs void/raw yields via `instanceof`.
 *
 * Hints are NOT carried on yielded values — they belong on the generator's
 * return value ({@link CommandReturn}) so the framework renders them once
 * after the generator completes.
 *
 * @typeParam T - The data type (matches the `OutputConfig<T>` type parameter)
 */
export class CommandOutput<T> {
  /** The data to render (serialized as-is to JSON, passed to `human` formatter) */
  readonly data: T;
  constructor(data: T) {
    this.data = data;
  }
}
 
/**
 * Yield token that tells the `buildCommand` wrapper to clear the terminal.
 *
 * On interactive terminals (TTY, non-plain), writes ANSI escape codes
 * to move the cursor home and clear the screen. On piped/plain output
 * or in JSON mode, the token is silently ignored.
 *
 * Use before re-yielding `CommandOutput` in refresh/polling loops
 * so the new output replaces the old in-place rather than appending.
 */
export class ClearScreen {
  /** Brand field for instanceof checks — never read, just exists. */
  readonly _brand = "ClearScreen" as const;
}
 
/**
 * Return type for command generators.
 *
 * Carries metadata that applies to the entire command invocation — not to
 * individual yielded chunks. The `buildCommand` wrapper captures this from
 * the generator's return value (the `done: true` result of `.next()`).
 *
 * `hint` is shown in human mode and suppressed in JSON mode.
 */
export type CommandReturn = {
  /**
   * Hint line appended after all output (suppressed in JSON mode).
   *
   * When the renderer has a `finalize()` method, the hint is passed
   * to it — the renderer decides how to render it alongside any
   * cleanup output (e.g., table footer). Otherwise the wrapper writes
   * it via `writeFooter()`.
   */
  hint?: string;
};
 
/**
 * Rendering context passed to {@link renderCommandOutput}.
 * Contains the wrapper-injected flag values needed for output mode selection.
 */
type RenderContext = {
  /** Whether `--json` was passed */
  json: boolean;
  /** Pre-parsed `--fields` value */
  fields?: string[];
  /** ANSI prefix to prepend to the output (e.g., clear-screen escape) */
  clearPrefix?: string;
};
 
/**
 * Apply `jsonExclude` keys to data, stripping excluded fields from
 * objects or from each element of an array. Returns the data unchanged
 * when no exclusions are configured.
 */
function applyJsonExclude(
  data: unknown,
  excludeKeys: readonly string[] | undefined
): unknown {
  if (!excludeKeys || excludeKeys.length === 0) {
    return data;
  }
  Iif (typeof data !== "object" || data === null) {
    return data;
  }
  if (Array.isArray(data)) {
    return data.map((item: unknown) => {
      Iif (typeof item !== "object" || item === null) {
        return item;
      }
      const copy = { ...item } as Record<string, unknown>;
      for (const key of excludeKeys) {
        delete copy[key];
      }
      return copy;
    });
  }
  const copy = { ...data } as Record<string, unknown>;
  for (const key of excludeKeys) {
    delete copy[key];
  }
  return copy;
}
 
/**
 * Write a final JSON object to stdout.
 *
 * When the writer supports zero-copy capture (library mode), the object
 * is handed off directly without serialization. Otherwise it is
 * JSON-stringified and written as a single line.
 */
function emitJsonObject(stdout: Writer, obj: unknown): void {
  if (stdout.captureObject) {
    stdout.captureObject(obj);
    return;
  }
  stdout.write(`${formatJson(obj)}\n`);
}
 
/**
 * Render a single yielded `CommandOutput<T>` chunk.
 *
 * Called by the `buildCommand` wrapper per yielded value. In JSON mode
 * the data is serialized (with optional field filtering / transform);
 * in human mode the resolved renderer's `render()` is called.
 *
 * Hints are NOT rendered here — the wrapper calls `finalize()` or
 * `writeFooter()` once after the generator completes.
 *
 * @param stdout - Writer to output to
 * @param data - The data yielded by the command
 * @param config - The output config declared on buildCommand
 * @param renderer - Per-invocation renderer (from `config.human()`)
 * @param ctx - Rendering context with flag values
 */
// biome-ignore lint/nursery/useMaxParams: Framework function — config/renderer/ctx are all required for JSON vs human split.
export function renderCommandOutput(
  stdout: Writer,
  data: unknown,
  // biome-ignore lint/suspicious/noExplicitAny: Variance erasure — config/renderer are paired at build time, but the framework iterates over unknown yields.
  config: OutputConfig<any>,
  // biome-ignore lint/suspicious/noExplicitAny: Renderer type mirrors erased OutputConfig<T>
  renderer: HumanRenderer<any>,
  ctx: RenderContext
): void {
  if (ctx.json) {
    if (config.jsonTransform) {
      const transformed = config.jsonTransform(data, ctx.fields);
      Iif (transformed === undefined) {
        return;
      }
      emitJsonObject(stdout, transformed);
      return;
    }
 
    const excluded = applyJsonExclude(data, config.jsonExclude);
    const final =
      ctx.fields && ctx.fields.length > 0
        ? filterFields(excluded, ctx.fields)
        : excluded;
    emitJsonObject(stdout, final);
    return;
  }
 
  const text = renderer.render(data);
  if (text) {
    const prefix = ctx.clearPrefix ?? "";
    stdout.write(`${prefix}${text}\n`);
  }
}
 
// ---------------------------------------------------------------------------
// Schema introspection
// ---------------------------------------------------------------------------
 
/**
 * Field metadata extracted from a Zod schema for documentation.
 *
 * Populated by {@link extractSchemaFields} and consumed by:
 * - `introspect.ts` → `CommandInfo.jsonFields`
 * - `help.ts` → human help output
 * - `generate-skill.ts` → SKILL.md field tables
 */
export type SchemaFieldInfo = {
  /** Field name (top-level key in the JSON object) */
  name: string;
  /** Human-readable type string (e.g. "string", "number", "object", "string | null") */
  type: string;
  /** Description from `z.describe()` */
  description?: string;
  /** Whether the field is optional in the schema */
  optional: boolean;
};
 
/** Leaf-type name mapping for {@link zodTypeToString} */
const ZOD_TYPE_MAP: Record<string, string> = {
  ZodString: "string",
  ZodNumber: "number",
  ZodBoolean: "boolean",
  ZodObject: "object",
  ZodArray: "array",
  ZodRecord: "object",
  ZodNull: "null",
  ZodUnknown: "unknown",
  ZodAny: "any",
  ZodEnum: "string",
  ZodLiteral: "string",
};
 
/**
 * Resolve a `ZodUnion` into a deduplicated `" | "`-joined type string.
 *
 * Used by auto-generated `@sentry/api/zod` schemas that represent nullable
 * fields as `z.union([z.string(), z.null()])` instead of `z.string().nullable()`.
 */
function resolveZodUnion(options: ZodType[]): {
  type: string;
  optional: boolean;
} {
  let optional = false;
  const parts: string[] = [];
  for (const opt of options) {
    const resolved = zodTypeToString(opt);
    Iif (resolved.optional) {
      optional = true;
    }
    if (!parts.includes(resolved.type)) {
      parts.push(resolved.type);
    }
  }
  return { type: parts.join(" | "), optional };
}
 
/**
 * Map a Zod type's internal `typeName` to a human-readable string.
 *
 * Unwraps wrapper types (Optional, Nullable, Default) and builds a
 * composite type string (e.g. "string | null" for ZodNullable<ZodString>).
 * Delegates ZodUnion handling to {@link resolveZodUnion}.
 */
function zodTypeToString(schema: ZodType): { type: string; optional: boolean } {
  const def = (schema as { _def?: { typeName?: string; innerType?: ZodType } })
    ._def;
  Iif (!def?.typeName) {
    return { type: "unknown", optional: false };
  }
 
  // Unwrap wrapper types recursively
  if (def.typeName === "ZodOptional" && def.innerType) {
    const inner = zodTypeToString(def.innerType);
    return { type: inner.type, optional: true };
  }
  if (def.typeName === "ZodNullable" && def.innerType) {
    const inner = zodTypeToString(def.innerType);
    const nullableType = inner.type.includes(" | null")
      ? inner.type
      : `${inner.type} | null`;
    return { type: nullableType, optional: inner.optional };
  }
  Iif (def.typeName === "ZodDefault" && def.innerType) {
    return zodTypeToString(def.innerType);
  }
  if (def.typeName === "ZodUnion") {
    const options = (def as { options?: ZodType[] }).options;
    Eif (options?.length) {
      return resolveZodUnion(options);
    }
  }
 
  return { type: ZOD_TYPE_MAP[def.typeName] ?? "unknown", optional: false };
}
 
/**
 * Extract field metadata from a Zod object schema.
 *
 * Returns an array of {@link SchemaFieldInfo} describing each top-level
 * field's name, type, description, and optionality. Returns an empty
 * array for non-object schemas.
 *
 * @param schema - A Zod schema (typically `z.object({...})`)
 */
export function extractSchemaFields(schema: ZodType): SchemaFieldInfo[] {
  const def = (
    schema as {
      _def?: { typeName?: string };
      shape?: Record<string, ZodType>;
    }
  )._def;
 
  Iif (def?.typeName !== "ZodObject") {
    return [];
  }
 
  const shape = (schema as { shape?: Record<string, ZodType> }).shape;
  Iif (!shape) {
    return [];
  }
 
  return Object.entries(shape).map(([name, fieldSchema]) => {
    const { type, optional } = zodTypeToString(fieldSchema);
    const fieldDef = (fieldSchema as { description?: string }).description;
    return {
      name,
      type,
      description: fieldDef,
      optional,
    };
  });
}
 
/**
 * Format schema fields as a help text block for `--help` output.
 *
 * Produces a compact field list like:
 * ```
 * JSON fields (use --json --fields to select):
 *   id (string) — Numeric issue ID
 *   count (string, optional) — Total event count
 * ```
 */
export function formatSchemaForHelp(fields: SchemaFieldInfo[]): string {
  Iif (fields.length === 0) {
    return "";
  }
  const lines = ["JSON fields (use --json --fields to select):"];
  for (const field of fields) {
    const optStr = field.optional ? ", optional" : "";
    const desc = field.description ? ` — ${field.description}` : "";
    lines.push(`  ${field.name} (${field.type}${optStr})${desc}`);
  }
  return lines.join("\n");
}
 
/** Format footer text (muted in TTY, plain when piped, with surrounding newlines). */
export function formatFooter(text: string): string {
  return `\n${plainSafeMuted(text)}\n`;
}
 
/**
 * Write a formatted footer hint to stdout.
 * Adds empty line separator and applies muted styling.
 */
export function writeFooter(stdout: Writer, text: string): void {
  stdout.write(formatFooter(text));
}