import { relative, resolve } from 'node:path' import process from 'node:process' import { styleText } from 'node:util' import { read } from '@internals/utils' import type { Adapter, Config, Diagnostic, DiagnosticFormatOptions, ProblemDiagnostic } from '@kubb/core' import { Diagnostics, getInputKind, logLevel as logLevelMap } from '@kubb/core' import { pluralize } from '../../ui/text.ts' import { locateInSpec } from '../specFrame.ts' import { getInputPath, type LoggerContext } from './context.ts' import type { Phases } from './phases.ts' import type { Progress } from './progress.ts' import type { Spinner } from './spinner.ts' export type PrinterOptions = { ctx: LoggerContext; spinner: Spinner; progress: Progress; phases: Phases } export type PrinterState = { /** Problems held back so the human view can print them grouped by plugin once the build ends. */ problems: Array /** The finished config's diagnostics, for the closing verdict. */ diagnostics: Array /** A local-file input, which a located diagnostic points into; `load` parses it once, on the first such diagnostic. */ spec: { path: string; adapter: Adapter; source: Promise } | undefined } /** Warnings, errors and diagnostics: printed as they come, or held back and grouped by plugin once a human's build ends. */ export class Printer { readonly state: PrinterState = { problems: [], diagnostics: [], spec: undefined } readonly #ctx: LoggerContext readonly #formatOptions: DiagnosticFormatOptions constructor({ ctx, spinner, progress, phases }: PrinterOptions) { this.#ctx = ctx const { writer, silent, human, logLevel, text, line, hooks } = ctx const { audience } = writer const { state } = this const formatOptions: DiagnosticFormatOptions = { audience, verbose: logLevel >= logLevelMap.verbose, // The code lines up at the right edge, inside the group indent and the human view's per-problem indent. width: Math.min(process.stdout.columns || 80, 100) - (audience === 'human' ? 4 : 2), } this.#formatOptions = formatOptions hooks.hook('kubb:warn', ({ message, info }) => { if (logLevel < logLevelMap.warn) { return } writer.warn(line(styleText('yellow', '⚠'), message, logLevel >= logLevelMap.info ? info : undefined)) }) // Unguarded: a failure stays visible even at silent. hooks.hook('kubb:error', ({ error }) => { const phase = phases.phase if (phase) { phase.failed = true } // Rendered as a `KUBB_UNKNOWN` diagnostic, so the output alone says what broke and where. The // name tells a `TypeError` from a plain `Error`. const message = error.name === 'Error' || Diagnostics.isError(error) ? error.message : `${error.name}: ${error.message}` const help = Diagnostics.isError(error) ? error.diagnostic.help : undefined const [headline = '', ...details] = Diagnostics.format({ code: Diagnostics.code.unknown, severity: 'error', message, help, cause: error }, formatOptions) if (!human) { spinner.stop() progress.stop() writer.diagnostic([headline, ...details]) return } // The writer draws the error symbol, so the message goes in bare. const drawn = spinner.active if (drawn) spinner.stop(message, 'failed') if (!drawn) writer.error(text(message)) if (details.length) { writer.block(details.map(text)) } }) hooks.hook('kubb:diagnostic', async ({ diagnostic }) => { // Silent still surfaces errors so failures stay visible. It drops warnings and info. if (silent && diagnostic.severity !== 'error') { return } // A Studio job's problems show in Studio; its job line counts the errors. if (ctx.session && !Diagnostics.isUpdate(diagnostic)) { return } spinner.stop() progress.stop() if (Diagnostics.isUpdate(diagnostic)) { writer.update( [`\`v${diagnostic.currentVersion}\` → \`v${diagnostic.latestVersion}\``, 'Run `npm install -g @kubb/cli` to update'], 'Update available for `Kubb`', ) return } // Inside a build, the human view waits for the end to group by plugin. if (human && ctx.groupOpen) { state.problems.push(diagnostic) return } await this.#writeProblems([diagnostic]) }) } // Format options for one problem, with its line and column in a local spec when its pointer resolves there. async #optionsFor(problem: ProblemDiagnostic): Promise { const { location } = problem const { spec } = this.state if (!location || !spec) { return this.#formatOptions } const source = await spec.source const position = source && locateInSpec({ adapter: spec.adapter, source, location }) if (!position) { return this.#formatOptions } return { ...this.#formatOptions, position: { file: relative(process.cwd(), spec.path), ...position } } } // Prints problems grouped under their plugin's name, each with the code aligned at the right. async #writeProblems(problems: ReadonlyArray): Promise { const { writer, human } = this.#ctx if (!problems.length) { return } const formatted = await Promise.all(problems.map(async (problem) => Diagnostics.format(problem, await this.#optionsFor(problem)))) if (!human) { for (const lines of formatted) writer.diagnostic(lines) return } const byPlugin = Map.groupBy( problems.map((problem, index) => ({ plugin: problem.plugin ?? '', lines: formatted[index]! })), ({ plugin }) => plugin, ) const lines = [...byPlugin].flatMap(([plugin, group]) => [ ...(plugin ? [` ${styleText('bold', plugin)}`, ''] : []), ...group.flatMap((entry) => [...entry.lines.map((entryLine) => ` ${entryLine}`), '']), ]) writer.diagnostic(lines.slice(0, -1)) } /** Points located diagnostics into the config's input when it is a local file. */ setSpec(config: Config): void { const input = getInputPath(config) if (input && config.adapter?.locate && getInputKind(input) === 'file') { const path = resolve(config.root, input) // Read lazily, once, and only when a diagnostic needs a frame. let source: Promise | undefined this.state.spec = { path, adapter: config.adapter, get source() { return (source ??= read(path).catch(() => undefined)) }, } } } /** Prints the problems held back so far. */ flush(): Promise { return this.#writeProblems(this.state.problems.splice(0)) } /** `✖ Failed 1 error · 1 warning in plugin-ts` once the build ended, or `✗ Generation failed` for a run that stopped early. */ failedTitle(): string { const { errors, warnings } = Diagnostics.count(this.state.diagnostics) const counts = [errors && pluralize(errors, 'error'), warnings && pluralize(warnings, 'warning')].filter(Boolean).join(' · ') if (!this.#ctx.human || !counts) { return '✗ Generation failed' } const plugins = Diagnostics.failedPlugins(this.state.diagnostics) return `✖ Failed ${counts}${plugins.length ? ` in ${plugins.join(', ')}` : ''}` } reset(): void { Object.assign(this.state, { problems: [], diagnostics: [], spec: undefined }) } } export function createPrinter(options: PrinterOptions): Printer { return new Printer(options) }