Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 36 additions & 2 deletions src/commands/doctor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,17 @@ import type {
DiagnosticReport,
DiagnosticResult,
DiagnosticSummary,
DiagnosticReportJSON,
} from '../diagnostics/types.js';
import { DIAGNOSTIC_SCHEMA_VERSION } from '../diagnostics/types.js';
import { EXIT_CODES } from '../errors/errors.js';
import { TerminalOutput } from '../output/terminal.js';

export interface DoctorCommandOptions {
readonly checks?: readonly DiagnosticCheck[];
readonly output?: TerminalOutput;
readonly setExitCode?: (code: number) => void;
readonly json?: boolean;
}

// Exported bounds for diagnostic text rendering.
Expand Down Expand Up @@ -66,6 +69,27 @@ function formatResult(result: DiagnosticResult): string {
return `[${result.status.toUpperCase()}] ${label}: ${message}`;
}

function renderJSONReport(report: DiagnosticReport): DiagnosticReportJSON {
return {
schemaVersion: DIAGNOSTIC_SCHEMA_VERSION,
checks: report.results.map((result) => ({
id: result.id,
label: result.label,
status: result.status,
message: sanitizeText(result.message, MAX_MESSAGE_LENGTH),
...(result.remediation === undefined
? {}
: { remediation: sanitizeText(result.remediation, MAX_REMEDIATION_LENGTH) }),
})),
summary: {
pass: report.summary.pass,
warn: report.summary.warn,
fail: report.summary.fail,
total: report.summary.total,
},
};
}

function formatOverallHealth(summary: DiagnosticSummary): string {
if (summary.total === 0) {
return 'Overall health: no diagnostics registered.';
Expand Down Expand Up @@ -130,12 +154,22 @@ export function createDoctorCommand(
const output = options.output ?? new TerminalOutput();
const setExitCode =
options.setExitCode ?? ((code) => (process.exitCode = code));
const isJsonMode = options.json ?? false;

return new Command('doctor')
.description('Check the local StellarForge development environment.')
.action(() => {
.option('--json', 'Output as JSON')
.action((cmdOptions) => {
const report = runDiagnostics(checks);
renderDiagnosticReport(report, output);
const jsonMode = isJsonMode || cmdOptions.json;

if (jsonMode) {
const jsonReport = renderJSONReport(report);
console.log(JSON.stringify(jsonReport, null, 2));
} else {
renderDiagnosticReport(report, output);
}

setExitCode(
report.summary.fail > 0 ? EXIT_CODES.diagnostic : EXIT_CODES.success,
);
Expand Down
22 changes: 22 additions & 0 deletions src/diagnostics/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,25 @@ export interface DiagnosticReport {
readonly results: readonly DiagnosticResult[];
readonly summary: DiagnosticSummary;
}

// JSON output schema (versioned and stable)
export const DIAGNOSTIC_SCHEMA_VERSION = '1.0.0';

export interface DiagnosticCheckJSON {
readonly id: string;
readonly label: string;
readonly status: DiagnosticStatus;
readonly message: string;
readonly remediation?: string;
}

export interface DiagnosticReportJSON {
readonly schemaVersion: string;
readonly checks: readonly DiagnosticCheckJSON[];
readonly summary: {
readonly pass: number;
readonly warn: number;
readonly fail: number;
readonly total: number;
};
}
1 change: 1 addition & 0 deletions src/output/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ export { redactSecretLikeText, REDACTED_PLACEHOLDER } from './redaction.js';
export {
getStringDisplayWidth,
sanitizeTerminalText,
shouldUseColor,
TerminalOutput,
wrapTerminalText,
type TerminalOutputOptions,
Expand Down
44 changes: 44 additions & 0 deletions src/output/terminal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ export interface TerminalOutputOptions {
* the output stream explicitly provides a positive `columns` property.
*/
readonly columns?: number;
/**
* Whether to use ANSI color codes in output.
* When omitted, determined by: NO_COLOR env var (disables), TTY detection,
* and FORCE_COLOR env var (enables only if NO_COLOR is not set).
*/
readonly useColor?: boolean;
}

export interface WrapOptions {
Expand All @@ -45,6 +51,33 @@ const BEL = 0x07;
const BACKSLASH = 0x5c;
const MAX_CONTROL_SEQUENCE_LENGTH = 4096;

/**
* Determines whether ANSI color codes should be used in output based on:
* - NO_COLOR environment variable (disables colors)
* - TTY detection (enables colors if connected to terminal)
* - FORCE_COLOR environment variable (enables colors if NO_COLOR not set)
*
* NO_COLOR takes precedence over TTY and FORCE_COLOR, following the convention.
* @see https://no-color.org/
*/
export function shouldUseColor(
stream?: TextOutputStream,
environment: NodeJS.ProcessEnv = process.env,
): boolean {
// NO_COLOR disables colors regardless of other factors
if (environment.NO_COLOR !== undefined && environment.NO_COLOR !== '') {
return false;
}

// FORCE_COLOR enables colors (unless NO_COLOR is set, which we checked above)
if (environment.FORCE_COLOR !== undefined && environment.FORCE_COLOR !== '') {
return true;
}

// TTY detection: check if connected to interactive terminal
return stream?.isTTY === true;
}

function isTerminalControlCharacter(code: number): boolean {
return (
code <= 0x08 ||
Expand Down Expand Up @@ -443,6 +476,7 @@ export function wrapTerminalText(
export class TerminalOutput {
readonly #streams: TerminalOutputStreams;
readonly #columns: number | undefined;
readonly #useColor: boolean;

constructor(
streams: TerminalOutputStreams = {
Expand All @@ -453,6 +487,12 @@ export class TerminalOutput {
) {
this.#streams = streams;
this.#columns = options.columns;
this.#useColor =
options.useColor ??
shouldUseColor(
this.#streams.stdout,
typeof process !== 'undefined' ? process.env : {},
);
}

// Returns whether the destination stream can accept more data immediately
Expand All @@ -475,6 +515,10 @@ export class TerminalOutput {
return this.#write(this.#streams.stderr, message);
}

get useColor(): boolean {
return this.#useColor;
}

onceStdoutDrained(listener: () => void): void {
this.#onceDrained(this.#streams.stdout, listener);
}
Expand Down
Loading