Skip to content

Commit

Permalink
[EVOL]: code review & refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
helabenkhalfallah committed May 30, 2024
1 parent da275e0 commit ccb6db6
Show file tree
Hide file tree
Showing 12 changed files with 1,213 additions and 487 deletions.
7 changes: 7 additions & 0 deletions eslint.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@ export default [
rules: {
quotes: ["error", "single"],
semi: ["error", "always"],
indent: [
2,
2,
{
SwitchCase: 1,
},
],
}
}
];
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,16 @@
"@babel/core": "=7.18.6",
"@babel/eslint-parser": "=7.24.6",
"@babel/preset-env": "=7.23.6",
"eslint": "=7.5.0 || =8.0.0 || =9.0.0"
"eslint": "=7.5.0 || =8.0.0 || =9.0.0",
"madge": "^7.0.0"
},
"dependencies": {
"typhonjs-escomplex": "=0.1.0",
"@typhonjs/babel-parser": "=0.2.0",
"fs-extra": "=8.1.0",
"globals": "=15.3.0",
"globby": "=10.0.1",
"lodash": "=4.17.21",
"typhonjs-escomplex": "=0.1.0",
"unixify": "=1.0.0",
"winston": "=3.13.0",
"winston-daily-rotate-file": "=5.0.0"
Expand Down
716 changes: 716 additions & 0 deletions pnpm-lock.yaml

Large diffs are not rendered by default.

44 changes: 25 additions & 19 deletions src/commons/AuditUtils.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,13 @@
import fs from 'fs-extra';
import AppLogger from './AppLogger.js';
import CodeComplexityConfig from '../config/CodeComplexityConfig.js';

const {
buildHtmlComplexityReports,
} = CodeComplexityConfig;

/**
* Checks if a file is of an accepted type.
* @param {string} fileName - The name of the file.
* @returns {boolean} - Returns true if the file is of an accepted type, false otherwise.
*/
const isAcceptedFileType = (fileName) => (
fileName?.endsWith('.js') ||
fileName?.endsWith('.js') ||
fileName?.endsWith('.jsx') ||
fileName?.endsWith('.ts') ||
fileName?.endsWith('.tsx') ||
Expand All @@ -25,7 +20,7 @@ const isAcceptedFileType = (fileName) => (
* @returns {boolean} - Returns true if the file is excluded, false otherwise.
*/
const isExcludedFile = (filePath) => (
filePath?.toLowerCase()?.includes('snap') ||
filePath?.toLowerCase()?.includes('snap') ||
filePath?.toLowerCase()?.includes('mock') ||
filePath?.toLowerCase()?.includes('jest') ||
filePath?.toLowerCase()?.includes('webpack') ||
Expand Down Expand Up @@ -78,24 +73,29 @@ const groupReportsByFile = (reports) => reports?.reduce((acc, report) => ({
* @param {object} summary - The audit summary
* @param {Array} auditReports - The audit reports to format.
* @param {string} fileFormat - The format of the file.
* @param {function} htmlFormatter - The html report formatter
* @returns {string} - Returns a string with the formatted reports.
*/
const formatReports = (summary, auditReports, fileFormat) => {
const formatReports = ({
summary,
auditReports,
fileFormat,
htmlReportFormatter,
}) => {
const reportsByFile = groupReportsByFile(auditReports);

if(fileFormat === 'json'){
return JSON.stringify({
summary,
reports: reportsByFile,
}, null, 2);
return JSON.stringify({
summary,
reports: reportsByFile,
}, null, 2);
}

if(fileFormat === 'html'){
const descriptions = [
...new Set(auditReports.map((report) => report.description) || [])
].map((description, index) => `${index + 1}) ${description}`) || [];

return buildHtmlComplexityReports(summary, descriptions, reportsByFile);
if(fileFormat === 'html' && htmlReportFormatter){
const descriptions = [
...new Set(auditReports.map((report) => report.description) || [])
].map((description, index) => `${index + 1}) ${description}`) || [];
return htmlReportFormatter(summary, descriptions, reportsByFile);
}

return '';
Expand All @@ -117,6 +117,7 @@ const writeAuditToFile = (summary, auditReports, options) => {
const {
fileName,
fileFormat,
htmlReportFormatter,
} = options || {};

const outPutFileName = `${fileName || 'CodeQualityReport'}.${fileFormat || 'json'}`;
Expand All @@ -125,7 +126,12 @@ const writeAuditToFile = (summary, auditReports, options) => {
fs.rmSync(outPutFileName);
}

fs.writeFileSync(outPutFileName, formatReports(summary, auditReports, fileFormat));
fs.writeFileSync(outPutFileName, formatReports({
summary,
auditReports,
fileFormat,
htmlReportFormatter,
}));

return true;
} catch (error) {
Expand Down
50 changes: 28 additions & 22 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,34 @@

import path from 'path';
import { parseArgs } from 'node:util';
import CodeComplexityAuditor from './kernel/CodeComplexityAuditor.js';
import CodeComplexityAuditor from './kernel/complexity/CodeComplexityAuditor.js';
import CodeComplexityConfig from './kernel/complexity/CodeComplexityConfig.js';
import AppLogger from './commons/AppLogger.js';
import AuditUtils from './commons/AuditUtils.js';

const{
writeAuditToFile
} = AuditUtils;

const {
formatHtmlComplexityReports
} = CodeComplexityConfig;

const args = parseArgs({
options: {
srcDir: {
type: 'string',
},
outputDir: {
type: 'string',
},
outputFile: {
type: 'string',
},
format: {
type: 'string',
},
options: {
srcDir: {
type: 'string',
},
outputDir: {
type: 'string',
},
outputFile: {
type: 'string',
},
format: {
type: 'string',
},
},
});

const {
Expand All @@ -42,20 +47,21 @@ if(!srcDir){
const directory = srcDir;

const inputOptions = {
exclude: null,
noempty: true,
quiet: true,
title: directory,
exclude: null,
noempty: true,
quiet: true,
title: directory,
};

const outPutOptions = {
fileName: path.join(outputDir, outputFile),
fileFormat: format // html or json
fileName: path.join(outputDir, outputFile),
fileFormat: format, // html or json
htmlReportFormatter: formatHtmlComplexityReports,
};

const {
summary,
auditReports,
summary,
auditReports,
} = await CodeComplexityAuditor.startAudit(directory, inputOptions);

AppLogger.info(`[AuditorWorker] summary: ${Object.keys(summary || {}).length}`);
Expand Down
44 changes: 0 additions & 44 deletions src/kernel/CodeAnalysisHelper.js

This file was deleted.

Loading

0 comments on commit ccb6db6

Please sign in to comment.