All files / lib/commands docs.js

92.15% Statements 47/51
69.81% Branches 37/53
100% Functions 1/1
92.15% Lines 47/51

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                1x 1x                               9x   9x   9x 9x     9x 3x 3x           3x       6x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x       5x     4x     4x 4x 4x 4x     4x     4x                           4x 4x     4x 1x 1x 1x 1x 1x       4x 1x     4x             1x                 1x 1x 1x   1x      
import chalk from 'chalk';
import fs from 'fs-extra';
import path from 'path';
import { fileURLToPath } from 'url';
import { gatherData } from '../visualizers/index.js';
import { readTemplate, openBrowser, escapeJsonForScript } from '../visualizers/utils.js';
import { renderHtml } from '../visualizers/index.js';
 
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
 
/**
 * Command: docs
 * Generate HTML documentation for installed skills with interactive visualization.
 *
 * Usage: rosetta docs [options]
 */
export async function docs(options = {}) {
  const {
    output,
    ide,
    open: openBrowserFlag,
    quiet = false,
    dryRun = false,
    json = false
  } = options;
 
  try {
    // Resolve default output path if not provided
    const defaultOutputPath = path.join(process.cwd(), '.rosetta', 'docs', 'skills.html');
    const outputPath = output || defaultOutputPath;
 
    // If dry-run with JSON, just gather and output data
    if (dryRun && json) {
      const data = await gatherData();
      console.log(JSON.stringify({
        success: true,
        skills: data.skills,
        stats: data.stats,
        currentIde: data.currentIde
      }, null, 2));
      return;
    }
 
    // If dry-run without JSON, show preview (per existing behavior)
    if (dryRun) {
      const data = await gatherData();
      console.log(chalk.cyan('\n[DRY RUN] Visualization generated successfully'));
      console.log(chalk.gray(`Would write to: ${outputPath}`));
      console.log(chalk.gray(`Total skills: ${data.skills.length}`));
      const byStatus = data.stats.byStatus || {};
      const installedCount = (byStatus.installed || 0) + (byStatus['user-created'] || 0);
      const catalogCount = byStatus.catalog || 0;
      console.log(chalk.gray(`Installed: ${installedCount}`));
      console.log(chalk.gray(`Catalog: ${catalogCount}`));
      const ideFilter = ide || data.currentIde;
      console.log(chalk.gray(`IDE filter: ${ideFilter === 'all' ? 'none (showing all)' : ideFilter}`));
      return;
    }
 
    // Gather data
    const data = await gatherData();
 
    // Determine effective IDE filter
    const effectiveIdeFilter = ide || data.currentIde || 'all';
 
    // Load template and styles
    const templatePath = path.join(__dirname, '..', 'visualizers', 'template.html');
    const stylesPath = path.join(__dirname, '..', 'visualizers', 'styles.css');
    const template = await readTemplate(templatePath);
    const styles = await readTemplate(stylesPath);
 
    // Render HTML
    const html = renderHtml(data, template, styles, effectiveIdeFilter);
 
    // If JSON output requested (without dry-run), output data instead of HTML
    Iif (json) {
      console.log(JSON.stringify({
        success: true,
        outputPath,
        currentIde: effectiveIdeFilter,
        skillsCount: data.skills.length,
        installedCount: (data.stats.byStatus?.installed || 0) + (data.stats.byStatus?.['user-created'] || 0),
        catalogCount: data.stats.byStatus.catalog || 0,
        skills: data.skills
      }, null, 2));
      return;
    }
 
    // Ensure output directory exists and write HTML file
    await fs.ensureDir(path.dirname(outputPath));
    await fs.writeFile(outputPath, html, 'utf-8');
 
    // Print success message if not quiet
    if (!quiet) {
      console.log(chalk.green(`✓ Documentation generated: ${outputPath}`));
      const byStatus = data.stats.byStatus || {};
      const installedCount = (byStatus.installed || 0) + (byStatus['user-created'] || 0);
      const catalogCount = byStatus.catalog || 0;
      console.log(chalk.gray(`  Total skills: ${data.skills.length} (${installedCount} installed, ${catalogCount} available)`));
    }
 
    // Open browser if requested
    if (openBrowserFlag) {
      openBrowser(outputPath);
    }
 
    return {
      success: true,
      outputPath,
      skillsCount: data.skills.length
    };
 
  } catch (err) {
    Iif (json || dryRun) {
      console.error(JSON.stringify({
        success: false,
        error: err.message,
        stack: err.stack
      }, null, 2));
      process.exit(1);
    }
 
    console.error(chalk.red(`Error: ${err.message}`));
    Eif (err.stack) {
      console.error(chalk.gray(err.stack));
    }
    process.exit(1);
  }
}