All files / bin/commands explain.js

62.5% Statements 35/56
80% Branches 8/10
100% Functions 1/1
62.5% Lines 35/56

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 57418x 418x 418x 418x 418x 418x 418x 418x 418x 418x 9x               9x 9x 9x 9x                             9x 9x 1x 1x 1x 8x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x  
import { getDiagnostic, suggestCodes } from '../../lib/core/diagnostics/catalogue.js';
import { bold, cyan, red, yellow } from '../colors.js';
 
/**
 * Executes the `avenx explain <CODE>` command.
 * @param {object} cli
 * @param {string} rawCode
 * @param {boolean} [asJson]
 */
export function explainDiagnostic(cli, rawCode, asJson = false) {
  if (!rawCode) {
    if (asJson) {
      console.log(JSON.stringify({ error: 'Diagnostic code required' }, null, 2));
    } else {
      console.error('❌ Please provide a diagnostic code (e.g., avenx explain AVX_W29 or avenx explain W29)');
    }
    process.exit(1);
  }
 
  const diagnostic = getDiagnostic(rawCode);
 
  if (!diagnostic) {
    const suggestions = suggestCodes(rawCode);
    if (asJson) {
      console.log(JSON.stringify({
        error: `Unknown diagnostic code: '${rawCode}'`,
        suggestions
      }, null, 2));
    } else {
      console.error(`❌ Unknown diagnostic code: '${rawCode}'`);
      if (suggestions.length > 0) {
        console.log(`\nDid you mean: ${suggestions.join(', ')}?`);
      }
    }
    process.exit(1);
  }
 
  if (asJson) {
    console.log(JSON.stringify(diagnostic, null, 2));
    return;
  }
 
  const severityColor = diagnostic.severity === 'error' ? red : yellow;
 
  console.log(`\n${bold(diagnostic.code)}: ${diagnostic.name} [${severityColor(diagnostic.severity.toUpperCase())}]`);
  console.log(`Category: ${cyan(diagnostic.category)}\n`);
  console.log(`${bold('Summary:')}\n  ${diagnostic.summary}\n`);
 
  console.log(bold('Common Causes:'));
  diagnostic.causes.forEach((c) => console.log(`  • ${c}`));
 
  console.log(`\n${bold('How to Fix:')}`);
  diagnostic.remedies.forEach((r) => console.log(`  • ${r}`));
 
  console.log(`\n${bold('Documentation:')}\n  ${cyan(diagnostic.docsUrl)}\n`);
}