From 8aecb2529f160fb5125d5bec628c2e9f7395ee7a Mon Sep 17 00:00:00 2001 From: Rakshith Acharya Date: Sun, 24 Nov 2024 00:47:44 +0530 Subject: [PATCH] =?UTF-8?q?=F0=9F=8C=86=20Added=20checkCommand.js?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- commands/checkCommand.js | 134 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 134 insertions(+) create mode 100644 commands/checkCommand.js diff --git a/commands/checkCommand.js b/commands/checkCommand.js new file mode 100644 index 0000000..ba5e7d4 --- /dev/null +++ b/commands/checkCommand.js @@ -0,0 +1,134 @@ +const { program } = require("commander"); +const path = require("path"); +const clc = require("cli-color"); +const fs = require("fs/promises"); +const { + getAllFiles, + getLargeFiles, + getCodeStats, + findDuplicates, + findEmptyFiles, +} = require("../utils/fileUtils"); +const { + generateHtmlReport, + generateJsonReport, +} = require("../utils/reportUtils"); +const config = require("../config"); + +function checkCommand() { + program + .command("check") + .description(clc.cyanBright("Check the health of the codebase")) + .option( + "-p, --pattern ", + "Glob pattern to specify which files to include in the scan.", + "**/*" + ) + .option( + "-s, --size ", + "Size limit (in bytes) to classify files as large", + config.defaultSizeLimit + ) + .option( + "-o, --output ", + "Output format (html/json)", + config.defaultOutput + ) + .option( + "-d, --directory ", + "Directory to save the generated report", + config.defaultDirectory + ) + .action(async (options) => { + const { pattern, size, output, directory } = options; + + try { + console.log(clc.cyanBright.bold("\nšŸŽÆ Codebase Health Check šŸŽÆ")); + console.log( + clc.yellow( + "Scanning your codebase for large files, duplicates, and empty files... šŸ•µļøā€ā™‚ļø\n" + ) + ); + + const files = await getAllFiles(pattern); + const largeFiles = await getLargeFiles(files, parseInt(size)); + const { totalLines, totalFiles } = await getCodeStats(files); + const duplicates = await findDuplicates(files); + const emptyFiles = await findEmptyFiles(files); + + console.log(clc.cyanBright.bold("\nšŸ“Š Codebase Statistics šŸ“Š")); + console.log(clc.magenta(`- Total files scanned: ${totalFiles}`)); + console.log(clc.magenta(`- Total lines of code: ${totalLines}`)); + console.log( + clc.magenta(`- Total duplicate files: ${duplicates.length}`) + ); + console.log(clc.magenta(`- Total empty files: ${emptyFiles.length}`)); + + console.log(clc.yellow.bold("\nLarge Files Found šŸ—‚ļø")); + if (largeFiles.length === 0) { + console.log( + clc.greenBright( + "āœ… No large files found! Your code is neat and optimized!" + ) + ); + } else { + largeFiles.forEach(({ file, size }) => { + console.log(clc.redBright(` šŸ”“ ${file} - ${size} bytes`)); + }); + } + + console.log(clc.yellow.bold("\nDuplicate Files Found šŸ“„")); + if (duplicates.length === 0) { + console.log(clc.greenBright("āœ… No duplicate files found!")); + } else { + duplicates.forEach(({ file1, file2 }) => { + console.log( + clc.redBright(` šŸ”“ Duplicate files: ${file1} and ${file2}`) + ); + }); + } + + console.log(clc.yellow.bold("\nEmpty Files Found šŸ“‚")); + if (emptyFiles.length === 0) { + console.log( + clc.greenBright( + "āœ… No empty files found! Your code is in great shape!" + ) + ); + } else { + emptyFiles.forEach((file) => { + console.log(clc.redBright(` šŸ”“ Empty file: ${file}`)); + }); + } + + console.log(clc.greenBright.bold("\nšŸ”§ Health Check Complete! šŸ”§")); + + const reportDirectory = path.resolve(directory); + await fs.mkdir(reportDirectory, { recursive: true }); + + const reportPath = path.join(reportDirectory, "report.html"); + if (output === "html") { + generateHtmlReport( + { totalLines, totalFiles }, + largeFiles, + duplicates, + emptyFiles, + reportPath + ); + } else if (output === "json") { + const jsonReportPath = path.join(reportDirectory, "report.json"); + generateJsonReport( + { totalLines, totalFiles }, + largeFiles, + duplicates, + emptyFiles, + jsonReportPath + ); + } + } catch (err) { + console.error(clc.redBright("āŒ An error occurred:"), err.message); + } + }); +} + +module.exports = checkCommand;