All files / src html-generator.ts

93.93% Statements 62/66
77.41% Branches 24/31
100% Functions 9/9
96.87% Lines 62/64

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 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 1782x   2x         2x                                   8x     8x               8x             8x   8x 2x     8x 2x 2x     8x   8x             8x 8x   8x 4x 2x       2x     4x 6x 6x   6x                   4x   4x 2x       8x             8x   8x 6x 4x 2x   4x 2x 2x 2x 2x 2x 2x   2x                 8x 8x 6x         8x 8x   1x 1x 1x     8x 4x     8x               6x             8x   8x 16x 6x 10x 10x 8x         8x 8x      
import * as path from 'path'
import { TreeNode } from './types'
import { indexTemplate } from './compiled-templates'
 
/**
 * HTML generator class for creating index pages from tree structures
 */
export class HtmlGenerator {
  /**
   * Generates a minimal HTML index page from the tree structure
   * @param tree The tree structure of HTML files
   * @param options Generation options
   * @returns HTML string content
   */
  static generateIndex(tree: TreeNode, options: {
    title?: string
    repositoryName?: string
    commitSha?: string
    timestamp?: string
  } = {}): string {
    const {
      title = 'Test Reports Index',
      repositoryName = '',
      commitSha = '',
      timestamp = new Date().toISOString()
    } = options
 
    // Use precompiled template
    const templateData = {
      title,
      metadata: this.generateMetadata(repositoryName, commitSha, timestamp),
      fileList: this.generateFileList(tree),
      fileCount: this.countFiles(tree).toString(),
      generatedTime: new Date(timestamp).toLocaleString()
    }
 
    return indexTemplate(templateData)
  }
 
  /**
   * Generates metadata section HTML
   */
  private static generateMetadata(repositoryName: string, commitSha: string, timestamp: string): string {
    const items = []
    
    if (repositoryName) {
      items.push(`<span>📁 Repository: <strong>${repositoryName}</strong></span>`)
    }
    
    if (commitSha) {
      const shortSha = commitSha.substring(0, 7)
      items.push(`<span>🔗 Commit: <strong>${shortSha}</strong></span>`)
    }
    
    items.push(`<span>🕒 Generated: <strong>${new Date(timestamp).toLocaleString()}</strong></span>`)
    
    return items.join('')
  }
 
  /**
   * Generates the file list HTML from tree structure
   */
  private static generateFileList(tree: TreeNode): string {
    const sections = this.organizeByDirectory(tree)
    let html = ''
 
    for (const [dirName, files] of sections) {
      if (dirName !== 'root') {
        html += `<div class="directory-section">
          <div class="directory-title">📁 ${dirName}</div>
          <ul class="file-list">`
      } else {
        html += `<ul class="file-list">`
      }
 
      for (const file of files) {
        const relativePath = this.getRelativePath(file.path)
        const fileName = path.basename(file.path)
        
        html += `<li class="file-item" data-path="${relativePath}">
          <a href="${relativePath}" target="_blank">
            <span class="file-icon">📄</span>
            <div class="file-info">
              <div class="file-name">${fileName}</div>
            </div>
          </a>
        </li>`
      }
 
      html += `</ul>`
      
      if (dirName !== 'root') {
        html += `</div>`
      }
    }
 
    return html
  }
 
  /**
   * Organizes files by directory for better presentation, sorted alphabetically
   */
  private static organizeByDirectory(tree: TreeNode): Map<string, TreeNode[]> {
    const sections = new Map<string, TreeNode[]>()
    
    const collectFiles = (node: TreeNode, currentDir: string = 'root', depth: number = 0) => {
      if (node.type === 'file') {
        if (!sections.has(currentDir)) {
          sections.set(currentDir, [])
        }
        sections.get(currentDir)!.push(node)
      } else if (node.children) {
        for (const child of node.children) {
          if (child.type === 'file') {
            const dirKey = currentDir === 'root' ? node.name : `${currentDir}/${node.name}`
            if (!sections.has(dirKey)) {
              sections.set(dirKey, [])
            }
            sections.get(dirKey)!.push(child)
          } else E{
            const newDir = currentDir === 'root' ? node.name : `${currentDir}/${node.name}`
            collectFiles(child, newDir, depth + 1)
          }
        }
      }
    }
 
    if (tree.children) {
      for (const child of tree.children) {
        collectFiles(child)
      }
    }
 
    // Sort sections alphabetically (dictionary order)
    const sortedSections = new Map<string, TreeNode[]>()
    const sortedKeys = Array.from(sections.keys()).sort((a, b) => {
      // Sort alphabetically, with 'root' always first
      Iif (a === 'root') return -1
      Iif (b === 'root') return 1
      return a.localeCompare(b)
    })
    
    for (const key of sortedKeys) {
      sortedSections.set(key, sections.get(key)!)
    }
 
    return sortedSections
  }
 
  /**
   * Gets relative path for HTML links
   */
  private static getRelativePath(filePath: string): string {
    // Remove leading './' if present
    return filePath.startsWith('./') ? filePath.substring(2) : filePath
  }
 
  /**
   * Counts total number of files in the tree
   */
  private static countFiles(tree: TreeNode): number {
    let count = 0
    
    const countRecursive = (node: TreeNode) => {
      if (node.type === 'file') {
        count++
      } else if (node.children) {
        for (const child of node.children) {
          countRecursive(child)
        }
      }
    }
    
    countRecursive(tree)
    return count
  }
}