|
| 1 | +/** |
| 2 | + * Shared lint utilities for CLI commands |
| 3 | + */ |
| 4 | + |
| 5 | +import { |
| 6 | + REMOTE_BOARDS_URL, |
| 7 | + type BoardBundle, |
| 8 | + type LintIssue, |
| 9 | + type LintResult, |
| 10 | +} from '@wokwi/diagram-lint'; |
| 11 | +import chalkTemplate from 'chalk-template'; |
| 12 | + |
| 13 | +export interface LintDisplayOptions { |
| 14 | + /** Only show errors, hide warnings and info */ |
| 15 | + quiet?: boolean; |
| 16 | +} |
| 17 | + |
| 18 | +/** |
| 19 | + * Format a single lint issue for display |
| 20 | + */ |
| 21 | +export function formatLintIssue(issue: LintIssue): string { |
| 22 | + const icon = |
| 23 | + issue.severity === 'error' ? '\u2717' : issue.severity === 'warning' ? '\u26A0' : '\u2139'; |
| 24 | + const color = |
| 25 | + issue.severity === 'error' ? 'red' : issue.severity === 'warning' ? 'yellow' : 'gray'; |
| 26 | + const location = issue.partId ? ` (${issue.partId})` : ''; |
| 27 | + return chalkTemplate`{${color} ${icon} [${issue.rule}]} ${issue.message}${location}`; |
| 28 | +} |
| 29 | + |
| 30 | +/** |
| 31 | + * Display lint results to console |
| 32 | + */ |
| 33 | +export function displayLintResults(result: LintResult, options: LintDisplayOptions = {}): void { |
| 34 | + const { quiet = false } = options; |
| 35 | + |
| 36 | + // If quiet and no errors, don't show anything |
| 37 | + if (quiet && result.stats.errors === 0) { |
| 38 | + return; |
| 39 | + } |
| 40 | + |
| 41 | + for (const issue of result.issues) { |
| 42 | + // In quiet mode, only show errors |
| 43 | + if (quiet && issue.severity !== 'error') { |
| 44 | + continue; |
| 45 | + } |
| 46 | + console.error(formatLintIssue(issue)); |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +/** |
| 51 | + * Check if lint result has blocking errors |
| 52 | + * |
| 53 | + * @param result - The lint result |
| 54 | + * @param warningsAsErrors - If true, treat warnings as errors |
| 55 | + * @returns true if there are blocking errors (or warnings if warningsAsErrors is true) |
| 56 | + */ |
| 57 | +export function hasLintErrors(result: LintResult, warningsAsErrors?: boolean): boolean { |
| 58 | + if (result.stats.errors > 0) { |
| 59 | + return true; |
| 60 | + } |
| 61 | + if (warningsAsErrors && result.stats.warnings > 0) { |
| 62 | + return true; |
| 63 | + } |
| 64 | + return false; |
| 65 | +} |
| 66 | + |
| 67 | +/** |
| 68 | + * Format lint summary |
| 69 | + */ |
| 70 | +export function formatLintSummary(result: LintResult): string { |
| 71 | + const parts: string[] = []; |
| 72 | + |
| 73 | + if (result.stats.errors > 0) { |
| 74 | + parts.push(chalkTemplate`{red ${result.stats.errors} error(s)}`); |
| 75 | + } |
| 76 | + if (result.stats.warnings > 0) { |
| 77 | + parts.push(chalkTemplate`{yellow ${result.stats.warnings} warning(s)}`); |
| 78 | + } |
| 79 | + if (result.stats.infos > 0) { |
| 80 | + parts.push(chalkTemplate`{gray ${result.stats.infos} info}`); |
| 81 | + } |
| 82 | + |
| 83 | + if (parts.length === 0) { |
| 84 | + return chalkTemplate`{green \u2713} No issues found`; |
| 85 | + } |
| 86 | + |
| 87 | + return `Found ${parts.join(', ')}`; |
| 88 | +} |
| 89 | + |
| 90 | +export interface FetchBoardsOptions { |
| 91 | + /** Timeout in milliseconds (default: 5000) */ |
| 92 | + timeout?: number; |
| 93 | +} |
| 94 | + |
| 95 | +/** |
| 96 | + * Fetch board definitions from the remote registry |
| 97 | + * |
| 98 | + * @returns Board bundle, or null if fetch fails |
| 99 | + */ |
| 100 | +export async function fetchRemoteBoards(options?: FetchBoardsOptions): Promise<BoardBundle | null> { |
| 101 | + const timeout = options?.timeout ?? 5000; |
| 102 | + |
| 103 | + try { |
| 104 | + const response = await fetch(REMOTE_BOARDS_URL, { |
| 105 | + signal: AbortSignal.timeout(timeout), |
| 106 | + }); |
| 107 | + |
| 108 | + if (!response.ok) { |
| 109 | + return null; |
| 110 | + } |
| 111 | + |
| 112 | + return (await response.json()) as BoardBundle; |
| 113 | + } catch { |
| 114 | + // Network error, timeout, or JSON parse error |
| 115 | + return null; |
| 116 | + } |
| 117 | +} |
0 commit comments