From 9b66da43395fc7a5c98bc38f6f8b03dd2682f9e8 Mon Sep 17 00:00:00 2001 From: Shivam Sharma Date: Fri, 19 Dec 2025 19:35:57 +0530 Subject: [PATCH] refactor: replace chalk with styleText --- package-lock.json | 13 ------------- package.json | 1 - src/cli.js | 4 ++-- src/scanner.js | 30 +++++++++++++++--------------- 4 files changed, 17 insertions(+), 31 deletions(-) diff --git a/package-lock.json b/package-lock.json index 9e3633c..644abfc 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9,7 +9,6 @@ "version": "2.4.2", "license": "MIT", "dependencies": { - "chalk": "^5.6.2", "commander": "^14.0.2", "fast-glob": "^3.3.3", "ignore": "^7.0.5" @@ -68,18 +67,6 @@ "node": ">=8" } }, - "node_modules/chalk": { - "version": "5.6.2", - "resolved": "https://registry.npmjs.org/chalk/-/chalk-5.6.2.tgz", - "integrity": "sha512-7NzBL0rN6fMUW+f7A6Io4h40qQlG+xGmtMxfbnH/K7TAtt8JQWVQK+6g0UXKMeVJoyV5EkkNsErQ8pVD3bLHbA==", - "license": "MIT", - "engines": { - "node": "^12.17.0 || ^14.13 || >=16.0.0" - }, - "funding": { - "url": "https://github.com/chalk/chalk?sponsor=1" - } - }, "node_modules/commander": { "version": "14.0.2", "resolved": "https://registry.npmjs.org/commander/-/commander-14.0.2.tgz", diff --git a/package.json b/package.json index 275c24f..50e11a0 100644 --- a/package.json +++ b/package.json @@ -38,7 +38,6 @@ "access": "public" }, "dependencies": { - "chalk": "^5.6.2", "commander": "^14.0.2", "fast-glob": "^3.3.3", "ignore": "^7.0.5" diff --git a/src/cli.js b/src/cli.js index 28c7d30..0967c14 100644 --- a/src/cli.js +++ b/src/cli.js @@ -3,7 +3,7 @@ import { Command } from 'commander'; import path from 'node:path'; import scanner from './scanner.js'; -import chalk from 'chalk'; +import { styleText } from 'node:util'; const program = new Command(); program @@ -22,7 +22,7 @@ const logo = ` `; -console.log(chalk.magenta(logo)); +console.log(styleText('magenta', logo)); (async () => { const opts = program.opts(); diff --git a/src/scanner.js b/src/scanner.js index f64258d..39c6cb5 100644 --- a/src/scanner.js +++ b/src/scanner.js @@ -3,7 +3,7 @@ import path from 'node:path'; import fg from 'fast-glob'; import ignore from 'ignore'; import { execSync } from 'node:child_process'; -import chalk from 'chalk'; +import { styleText } from 'node:util'; import { findUnusedModules } from './unusedModuleDetector.js'; const DEFAULT_CONFIG_FILES = ['.codeguardianrc.json', 'codeguardian.config.json']; @@ -186,24 +186,24 @@ async function run({ configPath = null, staged = false, verbose = false } = {}) } } if (unusedImports.length > 0) { - console.log(chalk.yellowBright(`\nWarning: Unused imports in ${file}:`)); + console.log(styleText('yellow', `\nWarning: Unused imports in ${file}:`)); for (const id of unusedImports) { - console.log(chalk.yellow(` ${id}`)); + console.log(styleText('yellow', ` ${id}`)); } - console.log(chalk.gray('These imports are present but never used in this file.')); + console.log(styleText('gray', 'These imports are present but never used in this file.')); } } } // Print nice output if (findings.length === 0) { - console.log(chalk.green('Scan successful but no secrets found in.', process.cwd())); + console.log(styleText('green', 'Scan successful but no secrets found in.', process.cwd())); } else { - console.log(chalk.red(`Found ${findings.length} file(s) with potential secrets:`)); + console.log(styleText('red', `Found ${findings.length} file(s) with potential secrets:`)); for (const f of findings) { - console.log(chalk.yellow(`\nFile: ${f.file}`)); + console.log(styleText('yellow', `\nFile: ${f.file}`)); for (const m of f.matches) { - console.log(` ${chalk.magenta('Rule:')} ${m.rule} ${chalk.gray(`(line ${m.lineNumber})`)}\n ${chalk.red(m.line)}`); + console.log(` ${styleText('magenta','Rule:')} ${m.rule} ${styleText('gray', `(line ${m.lineNumber})`)}\n ${styleText('red', m.line)}`); } } } @@ -211,21 +211,21 @@ async function run({ configPath = null, staged = false, verbose = false } = {}) // Unused JS/TS module detection (warn only) const unused = findUnusedModules(jsTsFiles, importMap); if (unused.length > 0) { - console.log(chalk.yellowBright(`\nWarning: Unused modules detected (not imported by any other file):`)); + console.log(styleText('yellowBright', `\nWarning: Unused modules detected (not imported by any other file):`)); for (const f of unused) { - console.log(chalk.yellow(` ${f}`)); + console.log(styleText('yellow', ` ${f}`)); } - console.log(chalk.gray('These files are not blocking CI, but consider cleaning up unused modules.')); + console.log(styleText('gray', 'These files are not blocking CI, but consider cleaning up unused modules.')); } const endTime = process.hrtime.bigint(); const endMem = process.memoryUsage().heapUsed; const durationMs = Number(endTime - startTime) / 1e6; const memMB = (endMem - startMem) / 1024 / 1024; - console.log(chalk.cyanBright(`\nScan stats:`)); - console.log(chalk.cyan(` Files scanned: ${filesScanned}`)); - console.log(chalk.cyan(` Time taken: ${durationMs.toFixed(1)} ms`)); - console.log(chalk.cyan(` Memory used: ${memMB.toFixed(2)} MB`)); + console.log(styleText('cyanBright', `\nScan stats:`)); + console.log(styleText('cyan', ` Files scanned: ${filesScanned}`)); + console.log(styleText('cyan', ` Time taken: ${durationMs.toFixed(1)} ms`)); + console.log(styleText('cyan', ` Memory used: ${memMB.toFixed(2)} MB`)); return { findings }; }