All files / src/server/core code-stats.ts

0% Statements 0/72
0% Branches 0/1
0% Functions 0/1
0% Lines 0/72

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                                                                                                                                                                       
import * as fs from 'node:fs'
import * as path from 'node:path'
 
export function getFileStats(filePath: string) {
  try {
    if (!fs.existsSync(filePath)) {
      return null
    }
 
    const stats = fs.statSync(filePath)
    const content = fs.readFileSync(filePath, 'utf-8')
    const lines = content.split('\n').length
    const characters = content.length
 
    return { lines, characters, size: stats.size }
  }
  catch {
    return null
  }
}
 
export function getFilesStats(files: string[], workspaceRoot?: string) {
  const stats = new Map<string, any>()
  for (const file of files) {
    let fullPath = file
    if (workspaceRoot && !path.isAbsolute(file)) {
      fullPath = path.join(workspaceRoot, file)
    }
    const fileStat = getFileStats(fullPath)
    if (fileStat) stats.set(file, fileStat)
  }
  return stats
}
 
export function calculateCodeGeneration(beforeStats: Map<string, any>, afterStats: Map<string, any>) {
  let totalLinesAdded = 0
  let totalLinesRemoved = 0
  let totalCharactersAdded = 0
  let totalCharactersRemoved = 0
  let filesCreated = 0
  let filesModified = 0
  let filesDeleted = 0
 
  for (const [file, afterStat] of afterStats.entries()) {
    const beforeStat = beforeStats.get(file)
    if (!beforeStat) {
      filesCreated++
      totalLinesAdded += afterStat.lines
      totalCharactersAdded += afterStat.characters
    }
    else {
      const linesDiff = afterStat.lines - beforeStat.lines
      const charsDiff = afterStat.characters - beforeStat.characters
      if (linesDiff !== 0 || charsDiff !== 0) {
        filesModified++
        if (linesDiff > 0) totalLinesAdded += linesDiff
        else totalLinesRemoved += Math.abs(linesDiff)
        if (charsDiff > 0) totalCharactersAdded += charsDiff
        else totalCharactersRemoved += Math.abs(charsDiff)
      }
    }
  }
 
  for (const [, beforeStat] of beforeStats.entries()) {
    if (!afterStats.has(beforeStat.path)) {
      filesDeleted++
      totalLinesRemoved += beforeStat.lines
      totalCharactersRemoved += beforeStat.characters
    }
  }
 
  return {
    totalLinesAdded,
    totalLinesRemoved,
    totalCharactersAdded,
    totalCharactersRemoved,
    filesCreated,
    filesModified,
    filesDeleted,
  }
}