All files / app/codeCharta/util aggregationGenerator.ts

100% Statements 49/49
100% Branches 9/9
100% Functions 7/7
100% Lines 48/48

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 11783x 83x 83x 83x 83x   83x   83x 83x 83x 83x 83x     14x 7x 2x     5x   5x 12x 12x 12x 12x   5x       5x                                                   5x 12x   5x   5x       5x 12x 40x 18x   40x           12x           12x 12x     12x 60x 24x     12x 12x       20x 24x 24x     24x 8x           5x 5x 5x 5x      
import { CodeMapNode, CCFile, NodeType } from "../codeCharta.model"
import { FileNameHelper } from "./fileNameHelper"
import { getUpdatedPath } from "./nodePathHelper"
import packageJson from "../../../package.json"
import { fileRoot } from "../services/loadFile/fileRoot"
import { FileState } from "../model/files/files"
import { clone } from "./clone"
 
export class AggregationGenerator {
    private static projectNameArray: string[] = []
    private static fileNameArray: string[] = []
    private static fileChecksumArray: string[] = []
    private static fileSizesSum = 0
 
    static calculateAggregationFile(fileStates: Pick<FileState, "file">[]) {
        const inputFiles = clone(fileStates.map(x => x.file))
        if (inputFiles.length === 1) {
            return inputFiles[0]
        }
 
        this.resetVariables()
 
        for (const inputFile of inputFiles) {
            this.projectNameArray.push(inputFile.fileMeta.projectName.replace(" ", "_"))
            this.fileNameArray.push(FileNameHelper.withoutCCExtension(inputFile.fileMeta.fileName).replace(" ", "_"))
            this.fileChecksumArray.push(inputFile.fileMeta.fileChecksum)
            this.fileSizesSum += inputFile.fileMeta.exportedFileSize
        }
        return this.getNewAggregatedMap(inputFiles)
    }
 
    private static getNewAggregatedMap(inputFiles: CCFile[]) {
        const outputFile: CCFile = {
            fileMeta: {
                projectName: `project_aggregation_of_${this.projectNameArray.join("_and_")}`,
                fileName: `file_aggregation_of_${this.fileNameArray.join("_and_")}`,
                fileChecksum: this.fileChecksumArray.join(";"),
                apiVersion: packageJson.codecharta.apiVersion,
                exportedFileSize: this.fileSizesSum
            },
            map: {
                name: fileRoot.rootName,
                type: NodeType.FOLDER,
                children: [],
                attributes: {},
                path: fileRoot.rootPath
            },
            settings: {
                fileSettings: {
                    edges: [],
                    blacklist: [],
                    attributeTypes: { nodes: {}, edges: {} },
                    attributeDescriptors: {},
                    markedPackages: []
                }
            }
        }
 
        for (const inputMap of inputFiles) {
            outputFile.map.children.push(this.extractNodeFromMap(inputMap))
        }
        this.aggregateRootAttributes(outputFile)
 
        return outputFile
    }
 
    private static aggregateRootAttributes(outputFile: CCFile) {
        for (const { attributes } of outputFile.map.children) {
            for (const key of Object.keys(attributes)) {
                if (outputFile.map.attributes[key] === undefined) {
                    outputFile.map.attributes[key] = 0
                }
                outputFile.map.attributes[key] += attributes[key]
            }
        }
    }
 
    private static extractNodeFromMap(inputMap: CCFile) {
        const outputNode: CodeMapNode = {
            name: inputMap.fileMeta.fileName,
            children: inputMap.map.children,
            type: inputMap.map.type
        }
 
        if (inputMap.map.path) {
            outputNode.path = getUpdatedPath(inputMap.fileMeta.fileName, inputMap.map.path)
        }
 
        for (const key of Object.keys(inputMap.map)) {
            if (key !== "name" && key !== "path" && key !== "children") {
                outputNode[key] = inputMap.map[key]
            }
        }
        this.updatePathOfAllChildren(inputMap.fileMeta.fileName, outputNode.children)
        return outputNode
    }
 
    private static updatePathOfAllChildren(fileName: string, children: CodeMapNode[]) {
        for (const child of children) {
            if (child.path) {
                child.path = getUpdatedPath(fileName, child.path)
            }
 
            if (child.children) {
                this.updatePathOfAllChildren(fileName, child.children)
            }
        }
    }
 
    private static resetVariables() {
        this.projectNameArray = []
        this.fileNameArray = []
        this.fileChecksumArray = []
        this.fileSizesSum = 0
    }
}