CRAP Complexity Coverage Location
6.00 2 0/18 (0.00%) crap/html-report/html-report.service.ts (L32 - L75)
    public async createReport(crapReport: CrapReport): Promise<void> {
        const htmlReportDir = this.configService.getHtmlReportDir();
        if (!htmlReportDir) {
            throw new Error("No HTML report directory specified.");
        }

        await this.initHandlebars();
        const prismStyles = await this.fileSystemService.loadSourceFile(
            new URL("./prism/prism.min.css", import.meta.url),
        );
        const prismScript = await this.fileSystemService.loadSourceFile(new URL("./prism/prism.js", import.meta.url));

        const functions: FunctionReport[] = [];
        Object.entries(crapReport).forEach(([filePath, fileReport]) => {
            Object.values(fileReport).forEach((functionReport, fileIndex) => {
                functions.push({ ...functionReport, filePath, fileIndex });
            });
        });
        functions.sort((a, b) => b.statements.crap - a.statements.crap);
        const pageTemplate = await this.fileSystemService.loadHandlebarsTemplate(
            new URL("./template/page.hbs", import.meta.url),
        );
        await Promise.all(
            functions.map(async (functionReport) => {
                const html = pageTemplate({
                    ...functionReport,
                    content: "function",
                    title: `CRAP: ${functionReport.functionDescriptor} - ${functionReport.filePath}`,
                    sourceCode: functionReport.sourceCode,
                    prismStyles,
                    prismScript,
                });

                this.fileSystemService.writeHtmlReport(
                    join(htmlReportDir, functionReport.filePath, `${functionReport.fileIndex}.html`),
                    html,
                );
            }),
        );

        const result = pageTemplate({ functions, content: "overview", title: "CRAP", prismStyles, prismScript });

        await this.fileSystemService.writeHtmlReport(join(htmlReportDir, "index.html"), result, { logLevel: "log" });
    }