@aiready/pattern-detect 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/cli.js ADDED
@@ -0,0 +1,447 @@
1
+ #!/usr/bin/env node
2
+ "use strict";
3
+ var __create = Object.create;
4
+ var __defProp = Object.defineProperty;
5
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
6
+ var __getOwnPropNames = Object.getOwnPropertyNames;
7
+ var __getProtoOf = Object.getPrototypeOf;
8
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
9
+ var __copyProps = (to, from, except, desc) => {
10
+ if (from && typeof from === "object" || typeof from === "function") {
11
+ for (let key of __getOwnPropNames(from))
12
+ if (!__hasOwnProp.call(to, key) && key !== except)
13
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
14
+ }
15
+ return to;
16
+ };
17
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
18
+ // If the importer is in node compatibility mode or this is not an ESM
19
+ // file that has been converted to a CommonJS file using a Babel-
20
+ // compatible transform (i.e. "__esModule" has not been set), then set
21
+ // "default" to the CommonJS "module.exports" for node compatibility.
22
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
23
+ mod
24
+ ));
25
+
26
+ // src/cli.ts
27
+ var import_commander = require("commander");
28
+
29
+ // src/index.ts
30
+ var import_core2 = require("@aiready/core");
31
+
32
+ // src/detector.ts
33
+ var import_core = require("@aiready/core");
34
+ function categorizePattern(code) {
35
+ const lower = code.toLowerCase();
36
+ if (lower.includes("request") && lower.includes("response") || lower.includes("router.") || lower.includes("app.get") || lower.includes("app.post") || lower.includes("express") || lower.includes("ctx.body")) {
37
+ return "api-handler";
38
+ }
39
+ if (lower.includes("validate") || lower.includes("schema") || lower.includes("zod") || lower.includes("yup") || lower.includes("if") && lower.includes("throw")) {
40
+ return "validator";
41
+ }
42
+ if (lower.includes("return (") || lower.includes("jsx") || lower.includes("component") || lower.includes("props")) {
43
+ return "component";
44
+ }
45
+ if (lower.includes("class ") || lower.includes("this.")) {
46
+ return "class-method";
47
+ }
48
+ if (lower.includes("return ") && !lower.includes("this") && !lower.includes("new ")) {
49
+ return "utility";
50
+ }
51
+ if (lower.includes("function") || lower.includes("=>")) {
52
+ return "function";
53
+ }
54
+ return "unknown";
55
+ }
56
+ function extractCodeBlocks(content, minLines) {
57
+ const lines = content.split("\n");
58
+ const blocks = [];
59
+ let currentBlock = [];
60
+ let blockStart = 0;
61
+ let braceDepth = 0;
62
+ let inFunction = false;
63
+ for (let i = 0; i < lines.length; i++) {
64
+ const line = lines[i];
65
+ const trimmed = line.trim();
66
+ if (!inFunction && (trimmed.includes("function ") || trimmed.includes("=>") || trimmed.includes("async ") || /^(export\s+)?(async\s+)?function\s+/.test(trimmed) || /^(export\s+)?const\s+\w+\s*=\s*(async\s*)?\(/.test(trimmed))) {
67
+ inFunction = true;
68
+ blockStart = i;
69
+ }
70
+ for (const char of line) {
71
+ if (char === "{") braceDepth++;
72
+ if (char === "}") braceDepth--;
73
+ }
74
+ if (inFunction) {
75
+ currentBlock.push(line);
76
+ }
77
+ if (inFunction && braceDepth === 0 && currentBlock.length >= minLines) {
78
+ const blockContent = currentBlock.join("\n");
79
+ const linesOfCode = currentBlock.filter(
80
+ (l) => l.trim() && !l.trim().startsWith("//")
81
+ ).length;
82
+ blocks.push({
83
+ content: blockContent,
84
+ startLine: blockStart + 1,
85
+ patternType: categorizePattern(blockContent),
86
+ linesOfCode
87
+ });
88
+ currentBlock = [];
89
+ inFunction = false;
90
+ } else if (inFunction && braceDepth === 0) {
91
+ currentBlock = [];
92
+ inFunction = false;
93
+ }
94
+ }
95
+ return blocks;
96
+ }
97
+ function normalizeCode(code) {
98
+ return code.replace(/\/\/.*$/gm, "").replace(/\/\*[\s\S]*?\*\//g, "").replace(/"[^"]*"/g, '"STR"').replace(/'[^']*'/g, "'STR'").replace(/`[^`]*`/g, "`STR`").replace(/\b\d+\b/g, "NUM").replace(/\s+/g, " ").trim();
99
+ }
100
+ function calculateSimilarity(block1, block2) {
101
+ const norm1 = normalizeCode(block1);
102
+ const norm2 = normalizeCode(block2);
103
+ const baseSimilarity = (0, import_core.similarityScore)(norm1, norm2);
104
+ const tokens1 = norm1.split(/[\s(){}[\];,]+/).filter(Boolean);
105
+ const tokens2 = norm2.split(/[\s(){}[\];,]+/).filter(Boolean);
106
+ const tokenSimilarity = (0, import_core.similarityScore)(tokens1.join(" "), tokens2.join(" "));
107
+ return baseSimilarity * 0.4 + tokenSimilarity * 0.6;
108
+ }
109
+ function detectDuplicatePatterns(files, options) {
110
+ const { minSimilarity, minLines } = options;
111
+ const duplicates = [];
112
+ const allBlocks = files.flatMap(
113
+ (file) => extractCodeBlocks(file.content, minLines).map((block) => ({
114
+ ...block,
115
+ file: file.file,
116
+ normalized: normalizeCode(block.content),
117
+ tokenCost: (0, import_core.estimateTokens)(block.content)
118
+ }))
119
+ );
120
+ console.log(`Extracted ${allBlocks.length} code blocks for analysis`);
121
+ for (let i = 0; i < allBlocks.length; i++) {
122
+ for (let j = i + 1; j < allBlocks.length; j++) {
123
+ const block1 = allBlocks[i];
124
+ const block2 = allBlocks[j];
125
+ if (block1.file === block2.file) continue;
126
+ const similarity = calculateSimilarity(block1.content, block2.content);
127
+ if (similarity >= minSimilarity) {
128
+ duplicates.push({
129
+ file1: block1.file,
130
+ file2: block2.file,
131
+ line1: block1.startLine,
132
+ line2: block2.startLine,
133
+ similarity,
134
+ snippet: block1.content.split("\n").slice(0, 5).join("\n") + "\n...",
135
+ patternType: block1.patternType,
136
+ tokenCost: block1.tokenCost + block2.tokenCost,
137
+ linesOfCode: block1.linesOfCode
138
+ });
139
+ }
140
+ }
141
+ }
142
+ return duplicates.sort(
143
+ (a, b) => b.similarity - a.similarity || b.tokenCost - a.tokenCost
144
+ );
145
+ }
146
+
147
+ // src/index.ts
148
+ function getRefactoringSuggestion(patternType, similarity) {
149
+ const baseMessages = {
150
+ "api-handler": "Extract common middleware or create a base handler class",
151
+ validator: "Consolidate validation logic into shared schema validators (Zod/Yup)",
152
+ utility: "Move to a shared utilities file and reuse across modules",
153
+ "class-method": "Consider inheritance or composition to share behavior",
154
+ component: "Extract shared logic into a custom hook or HOC",
155
+ function: "Extract into a shared helper function",
156
+ unknown: "Extract common logic into a reusable module"
157
+ };
158
+ const urgency = similarity > 0.95 ? " (CRITICAL: Nearly identical code)" : similarity > 0.9 ? " (HIGH: Very similar, refactor soon)" : "";
159
+ return baseMessages[patternType] + urgency;
160
+ }
161
+ async function analyzePatterns(options) {
162
+ const { minSimilarity = 0.85, minLines = 5, ...scanOptions } = options;
163
+ const files = await (0, import_core2.scanFiles)(scanOptions);
164
+ const results = [];
165
+ const fileContents = await Promise.all(
166
+ files.map(async (file) => ({
167
+ file,
168
+ content: await (0, import_core2.readFileContent)(file)
169
+ }))
170
+ );
171
+ const duplicates = detectDuplicatePatterns(fileContents, {
172
+ minSimilarity,
173
+ minLines
174
+ });
175
+ for (const file of files) {
176
+ const fileDuplicates = duplicates.filter(
177
+ (dup) => dup.file1 === file || dup.file2 === file
178
+ );
179
+ const issues = fileDuplicates.map((dup) => {
180
+ const otherFile = dup.file1 === file ? dup.file2 : dup.file1;
181
+ const severity = dup.similarity > 0.95 ? "critical" : dup.similarity > 0.9 ? "major" : "minor";
182
+ return {
183
+ type: "duplicate-pattern",
184
+ severity,
185
+ message: `${dup.patternType} pattern ${Math.round(dup.similarity * 100)}% similar to ${otherFile} (${dup.tokenCost} tokens wasted)`,
186
+ location: {
187
+ file,
188
+ line: dup.file1 === file ? dup.line1 : dup.line2
189
+ },
190
+ suggestion: getRefactoringSuggestion(dup.patternType, dup.similarity)
191
+ };
192
+ });
193
+ const totalTokenCost = fileDuplicates.reduce(
194
+ (sum, dup) => sum + dup.tokenCost,
195
+ 0
196
+ );
197
+ results.push({
198
+ fileName: file,
199
+ issues,
200
+ metrics: {
201
+ tokenCost: totalTokenCost,
202
+ consistencyScore: Math.max(0, 1 - fileDuplicates.length * 0.1)
203
+ }
204
+ });
205
+ }
206
+ return results;
207
+ }
208
+ function generateSummary(results) {
209
+ const allIssues = results.flatMap((r) => r.issues);
210
+ const totalTokenCost = results.reduce(
211
+ (sum, r) => sum + (r.metrics.tokenCost || 0),
212
+ 0
213
+ );
214
+ const patternsByType = {
215
+ "api-handler": 0,
216
+ validator: 0,
217
+ utility: 0,
218
+ "class-method": 0,
219
+ component: 0,
220
+ function: 0,
221
+ unknown: 0
222
+ };
223
+ allIssues.forEach((issue) => {
224
+ const match = issue.message.match(/^(\S+(?:-\S+)*) pattern/);
225
+ if (match) {
226
+ const type = match[1];
227
+ patternsByType[type] = (patternsByType[type] || 0) + 1;
228
+ }
229
+ });
230
+ const topDuplicates = allIssues.slice(0, 10).map((issue) => {
231
+ const similarityMatch = issue.message.match(/(\d+)% similar/);
232
+ const tokenMatch = issue.message.match(/\((\d+) tokens/);
233
+ const typeMatch = issue.message.match(/^(\S+(?:-\S+)*) pattern/);
234
+ const fileMatch = issue.message.match(/similar to (.+?) \(/);
235
+ return {
236
+ file1: issue.location.file,
237
+ file2: fileMatch?.[1] || "unknown",
238
+ similarity: similarityMatch ? parseInt(similarityMatch[1]) / 100 : 0,
239
+ patternType: typeMatch?.[1] || "unknown",
240
+ tokenCost: tokenMatch ? parseInt(tokenMatch[1]) : 0
241
+ };
242
+ });
243
+ return {
244
+ totalPatterns: allIssues.length,
245
+ totalTokenCost,
246
+ patternsByType,
247
+ topDuplicates
248
+ };
249
+ }
250
+
251
+ // src/cli.ts
252
+ var import_chalk = __toESM(require("chalk"));
253
+ var import_fs = require("fs");
254
+ var import_path = require("path");
255
+ var program = new import_commander.Command();
256
+ program.name("aiready-patterns").description("Detect duplicate patterns in your codebase").version("0.1.0").argument("<directory>", "Directory to analyze").option("-s, --similarity <number>", "Minimum similarity score (0-1)", "0.85").option("-l, --min-lines <number>", "Minimum lines to consider", "5").option("--include <patterns>", "File patterns to include (comma-separated)").option("--exclude <patterns>", "File patterns to exclude (comma-separated)").option(
257
+ "-o, --output <format>",
258
+ "Output format: console, json, html",
259
+ "console"
260
+ ).option("--output-file <path>", "Output file path (for json/html)").action(async (directory, options) => {
261
+ console.log(import_chalk.default.blue("\u{1F50D} Analyzing patterns...\n"));
262
+ const results = await analyzePatterns({
263
+ rootDir: directory,
264
+ minSimilarity: parseFloat(options.similarity),
265
+ minLines: parseInt(options.minLines),
266
+ include: options.include?.split(","),
267
+ exclude: options.exclude?.split(",")
268
+ });
269
+ const summary = generateSummary(results);
270
+ const totalIssues = results.reduce((sum, r) => sum + r.issues.length, 0);
271
+ if (options.output === "json") {
272
+ const jsonOutput = {
273
+ summary,
274
+ results,
275
+ timestamp: (/* @__PURE__ */ new Date()).toISOString()
276
+ };
277
+ if (options.outputFile) {
278
+ (0, import_fs.writeFileSync)(options.outputFile, JSON.stringify(jsonOutput, null, 2));
279
+ console.log(import_chalk.default.green(`
280
+ \u2713 JSON report saved to ${options.outputFile}`));
281
+ } else {
282
+ console.log(JSON.stringify(jsonOutput, null, 2));
283
+ }
284
+ return;
285
+ }
286
+ if (options.output === "html") {
287
+ const html = generateHTMLReport(summary, results);
288
+ const outputPath = options.outputFile || (0, import_path.join)(process.cwd(), "pattern-report.html");
289
+ (0, import_fs.writeFileSync)(outputPath, html);
290
+ console.log(import_chalk.default.green(`
291
+ \u2713 HTML report saved to ${outputPath}`));
292
+ return;
293
+ }
294
+ console.log(import_chalk.default.cyan("\u2501".repeat(60)));
295
+ console.log(import_chalk.default.bold.white(" PATTERN ANALYSIS SUMMARY"));
296
+ console.log(import_chalk.default.cyan("\u2501".repeat(60)) + "\n");
297
+ console.log(
298
+ import_chalk.default.white(`\u{1F4C1} Files analyzed: ${import_chalk.default.bold(results.length)}`)
299
+ );
300
+ console.log(
301
+ import_chalk.default.yellow(`\u26A0 Duplicate patterns found: ${import_chalk.default.bold(totalIssues)}`)
302
+ );
303
+ console.log(
304
+ import_chalk.default.red(
305
+ `\u{1F4B0} Token cost (wasted): ${import_chalk.default.bold(summary.totalTokenCost.toLocaleString())}`
306
+ )
307
+ );
308
+ console.log(import_chalk.default.cyan("\n\u2501".repeat(60)));
309
+ console.log(import_chalk.default.bold.white(" PATTERNS BY TYPE"));
310
+ console.log(import_chalk.default.cyan("\u2501".repeat(60)) + "\n");
311
+ const sortedTypes = Object.entries(summary.patternsByType).filter(([, count]) => count > 0).sort(([, a], [, b]) => b - a);
312
+ sortedTypes.forEach(([type, count]) => {
313
+ const icon = getPatternIcon(type);
314
+ console.log(`${icon} ${import_chalk.default.white(type.padEnd(15))} ${import_chalk.default.bold(count)}`);
315
+ });
316
+ if (summary.topDuplicates.length > 0) {
317
+ console.log(import_chalk.default.cyan("\n\u2501".repeat(60)));
318
+ console.log(import_chalk.default.bold.white(" TOP DUPLICATE PATTERNS"));
319
+ console.log(import_chalk.default.cyan("\u2501".repeat(60)) + "\n");
320
+ summary.topDuplicates.slice(0, 10).forEach((dup, idx) => {
321
+ const severityColor = dup.similarity > 0.95 ? import_chalk.default.red : dup.similarity > 0.9 ? import_chalk.default.yellow : import_chalk.default.blue;
322
+ console.log(
323
+ `${import_chalk.default.dim(`${idx + 1}.`)} ${severityColor(
324
+ `${Math.round(dup.similarity * 100)}%`
325
+ )} ${getPatternIcon(dup.patternType)} ${import_chalk.default.white(dup.patternType)}`
326
+ );
327
+ console.log(
328
+ ` ${import_chalk.default.dim(dup.file1)}`
329
+ );
330
+ console.log(
331
+ ` ${import_chalk.default.dim("\u2194")} ${import_chalk.default.dim(dup.file2)}`
332
+ );
333
+ console.log(
334
+ ` ${import_chalk.default.red(`${dup.tokenCost.toLocaleString()} tokens wasted`)}
335
+ `
336
+ );
337
+ });
338
+ }
339
+ const allIssues = results.flatMap(
340
+ (r) => r.issues.map((issue) => ({ ...issue, file: r.fileName }))
341
+ );
342
+ const criticalIssues = allIssues.filter(
343
+ (issue) => issue.severity === "critical"
344
+ );
345
+ if (criticalIssues.length > 0) {
346
+ console.log(import_chalk.default.cyan("\u2501".repeat(60)));
347
+ console.log(import_chalk.default.bold.white(" CRITICAL ISSUES (>95% similar)"));
348
+ console.log(import_chalk.default.cyan("\u2501".repeat(60)) + "\n");
349
+ criticalIssues.slice(0, 5).forEach((issue) => {
350
+ console.log(import_chalk.default.red("\u25CF ") + import_chalk.default.white(`${issue.file}:${issue.location.line}`));
351
+ console.log(` ${import_chalk.default.dim(issue.message)}`);
352
+ console.log(` ${import_chalk.default.green("\u2192")} ${import_chalk.default.italic(issue.suggestion)}
353
+ `);
354
+ });
355
+ }
356
+ console.log(import_chalk.default.cyan("\u2501".repeat(60)));
357
+ console.log(
358
+ import_chalk.default.white(
359
+ `
360
+ \u{1F4A1} Run with ${import_chalk.default.bold("--output json")} or ${import_chalk.default.bold("--output html")} for detailed reports
361
+ `
362
+ )
363
+ );
364
+ });
365
+ function getPatternIcon(type) {
366
+ const icons = {
367
+ "api-handler": "\u{1F310}",
368
+ validator: "\u2713",
369
+ utility: "\u{1F527}",
370
+ "class-method": "\u{1F4E6}",
371
+ component: "\u269B\uFE0F",
372
+ function: "\u0192",
373
+ unknown: "\u2753"
374
+ };
375
+ return icons[type];
376
+ }
377
+ function generateHTMLReport(summary, results) {
378
+ return `<!DOCTYPE html>
379
+ <html>
380
+ <head>
381
+ <title>Pattern Detection Report</title>
382
+ <style>
383
+ body { font-family: system-ui, -apple-system, sans-serif; margin: 40px; background: #f5f5f5; }
384
+ .container { max-width: 1200px; margin: 0 auto; background: white; padding: 30px; border-radius: 8px; box-shadow: 0 2px 8px rgba(0,0,0,0.1); }
385
+ h1 { color: #333; border-bottom: 3px solid #007acc; padding-bottom: 10px; }
386
+ .summary { display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); gap: 20px; margin: 30px 0; }
387
+ .stat-card { background: #f9f9f9; padding: 20px; border-radius: 6px; border-left: 4px solid #007acc; }
388
+ .stat-value { font-size: 32px; font-weight: bold; color: #007acc; }
389
+ .stat-label { color: #666; font-size: 14px; text-transform: uppercase; }
390
+ table { width: 100%; border-collapse: collapse; margin: 20px 0; }
391
+ th, td { text-align: left; padding: 12px; border-bottom: 1px solid #ddd; }
392
+ th { background: #f5f5f5; font-weight: 600; }
393
+ .critical { color: #d32f2f; }
394
+ .major { color: #f57c00; }
395
+ .minor { color: #1976d2; }
396
+ </style>
397
+ </head>
398
+ <body>
399
+ <div class="container">
400
+ <h1>\u{1F50D} Pattern Detection Report</h1>
401
+ <p>Generated: ${(/* @__PURE__ */ new Date()).toLocaleString()}</p>
402
+
403
+ <div class="summary">
404
+ <div class="stat-card">
405
+ <div class="stat-value">${results.length}</div>
406
+ <div class="stat-label">Files Analyzed</div>
407
+ </div>
408
+ <div class="stat-card">
409
+ <div class="stat-value">${summary.totalPatterns}</div>
410
+ <div class="stat-label">Duplicate Patterns</div>
411
+ </div>
412
+ <div class="stat-card">
413
+ <div class="stat-value">${summary.totalTokenCost.toLocaleString()}</div>
414
+ <div class="stat-label">Tokens Wasted</div>
415
+ </div>
416
+ </div>
417
+
418
+ <h2>Top Duplicate Patterns</h2>
419
+ <table>
420
+ <thead>
421
+ <tr>
422
+ <th>Similarity</th>
423
+ <th>Type</th>
424
+ <th>File 1</th>
425
+ <th>File 2</th>
426
+ <th>Token Cost</th>
427
+ </tr>
428
+ </thead>
429
+ <tbody>
430
+ ${summary.topDuplicates.slice(0, 20).map(
431
+ (dup) => `
432
+ <tr>
433
+ <td class="${dup.similarity > 0.95 ? "critical" : dup.similarity > 0.9 ? "major" : "minor"}">${Math.round(dup.similarity * 100)}%</td>
434
+ <td>${dup.patternType}</td>
435
+ <td><code>${dup.file1}</code></td>
436
+ <td><code>${dup.file2}</code></td>
437
+ <td>${dup.tokenCost.toLocaleString()}</td>
438
+ </tr>
439
+ `
440
+ ).join("")}
441
+ </tbody>
442
+ </table>
443
+ </div>
444
+ </body>
445
+ </html>`;
446
+ }
447
+ program.parse();
package/dist/cli.mjs ADDED
@@ -0,0 +1,204 @@
1
+ #!/usr/bin/env node
2
+ import {
3
+ analyzePatterns,
4
+ generateSummary
5
+ } from "./chunk-RLWJXASG.mjs";
6
+
7
+ // src/cli.ts
8
+ import { Command } from "commander";
9
+ import chalk from "chalk";
10
+ import { writeFileSync } from "fs";
11
+ import { join } from "path";
12
+ var program = new Command();
13
+ program.name("aiready-patterns").description("Detect duplicate patterns in your codebase").version("0.1.0").argument("<directory>", "Directory to analyze").option("-s, --similarity <number>", "Minimum similarity score (0-1)", "0.85").option("-l, --min-lines <number>", "Minimum lines to consider", "5").option("--include <patterns>", "File patterns to include (comma-separated)").option("--exclude <patterns>", "File patterns to exclude (comma-separated)").option(
14
+ "-o, --output <format>",
15
+ "Output format: console, json, html",
16
+ "console"
17
+ ).option("--output-file <path>", "Output file path (for json/html)").action(async (directory, options) => {
18
+ console.log(chalk.blue("\u{1F50D} Analyzing patterns...\n"));
19
+ const results = await analyzePatterns({
20
+ rootDir: directory,
21
+ minSimilarity: parseFloat(options.similarity),
22
+ minLines: parseInt(options.minLines),
23
+ include: options.include?.split(","),
24
+ exclude: options.exclude?.split(",")
25
+ });
26
+ const summary = generateSummary(results);
27
+ const totalIssues = results.reduce((sum, r) => sum + r.issues.length, 0);
28
+ if (options.output === "json") {
29
+ const jsonOutput = {
30
+ summary,
31
+ results,
32
+ timestamp: (/* @__PURE__ */ new Date()).toISOString()
33
+ };
34
+ if (options.outputFile) {
35
+ writeFileSync(options.outputFile, JSON.stringify(jsonOutput, null, 2));
36
+ console.log(chalk.green(`
37
+ \u2713 JSON report saved to ${options.outputFile}`));
38
+ } else {
39
+ console.log(JSON.stringify(jsonOutput, null, 2));
40
+ }
41
+ return;
42
+ }
43
+ if (options.output === "html") {
44
+ const html = generateHTMLReport(summary, results);
45
+ const outputPath = options.outputFile || join(process.cwd(), "pattern-report.html");
46
+ writeFileSync(outputPath, html);
47
+ console.log(chalk.green(`
48
+ \u2713 HTML report saved to ${outputPath}`));
49
+ return;
50
+ }
51
+ console.log(chalk.cyan("\u2501".repeat(60)));
52
+ console.log(chalk.bold.white(" PATTERN ANALYSIS SUMMARY"));
53
+ console.log(chalk.cyan("\u2501".repeat(60)) + "\n");
54
+ console.log(
55
+ chalk.white(`\u{1F4C1} Files analyzed: ${chalk.bold(results.length)}`)
56
+ );
57
+ console.log(
58
+ chalk.yellow(`\u26A0 Duplicate patterns found: ${chalk.bold(totalIssues)}`)
59
+ );
60
+ console.log(
61
+ chalk.red(
62
+ `\u{1F4B0} Token cost (wasted): ${chalk.bold(summary.totalTokenCost.toLocaleString())}`
63
+ )
64
+ );
65
+ console.log(chalk.cyan("\n\u2501".repeat(60)));
66
+ console.log(chalk.bold.white(" PATTERNS BY TYPE"));
67
+ console.log(chalk.cyan("\u2501".repeat(60)) + "\n");
68
+ const sortedTypes = Object.entries(summary.patternsByType).filter(([, count]) => count > 0).sort(([, a], [, b]) => b - a);
69
+ sortedTypes.forEach(([type, count]) => {
70
+ const icon = getPatternIcon(type);
71
+ console.log(`${icon} ${chalk.white(type.padEnd(15))} ${chalk.bold(count)}`);
72
+ });
73
+ if (summary.topDuplicates.length > 0) {
74
+ console.log(chalk.cyan("\n\u2501".repeat(60)));
75
+ console.log(chalk.bold.white(" TOP DUPLICATE PATTERNS"));
76
+ console.log(chalk.cyan("\u2501".repeat(60)) + "\n");
77
+ summary.topDuplicates.slice(0, 10).forEach((dup, idx) => {
78
+ const severityColor = dup.similarity > 0.95 ? chalk.red : dup.similarity > 0.9 ? chalk.yellow : chalk.blue;
79
+ console.log(
80
+ `${chalk.dim(`${idx + 1}.`)} ${severityColor(
81
+ `${Math.round(dup.similarity * 100)}%`
82
+ )} ${getPatternIcon(dup.patternType)} ${chalk.white(dup.patternType)}`
83
+ );
84
+ console.log(
85
+ ` ${chalk.dim(dup.file1)}`
86
+ );
87
+ console.log(
88
+ ` ${chalk.dim("\u2194")} ${chalk.dim(dup.file2)}`
89
+ );
90
+ console.log(
91
+ ` ${chalk.red(`${dup.tokenCost.toLocaleString()} tokens wasted`)}
92
+ `
93
+ );
94
+ });
95
+ }
96
+ const allIssues = results.flatMap(
97
+ (r) => r.issues.map((issue) => ({ ...issue, file: r.fileName }))
98
+ );
99
+ const criticalIssues = allIssues.filter(
100
+ (issue) => issue.severity === "critical"
101
+ );
102
+ if (criticalIssues.length > 0) {
103
+ console.log(chalk.cyan("\u2501".repeat(60)));
104
+ console.log(chalk.bold.white(" CRITICAL ISSUES (>95% similar)"));
105
+ console.log(chalk.cyan("\u2501".repeat(60)) + "\n");
106
+ criticalIssues.slice(0, 5).forEach((issue) => {
107
+ console.log(chalk.red("\u25CF ") + chalk.white(`${issue.file}:${issue.location.line}`));
108
+ console.log(` ${chalk.dim(issue.message)}`);
109
+ console.log(` ${chalk.green("\u2192")} ${chalk.italic(issue.suggestion)}
110
+ `);
111
+ });
112
+ }
113
+ console.log(chalk.cyan("\u2501".repeat(60)));
114
+ console.log(
115
+ chalk.white(
116
+ `
117
+ \u{1F4A1} Run with ${chalk.bold("--output json")} or ${chalk.bold("--output html")} for detailed reports
118
+ `
119
+ )
120
+ );
121
+ });
122
+ function getPatternIcon(type) {
123
+ const icons = {
124
+ "api-handler": "\u{1F310}",
125
+ validator: "\u2713",
126
+ utility: "\u{1F527}",
127
+ "class-method": "\u{1F4E6}",
128
+ component: "\u269B\uFE0F",
129
+ function: "\u0192",
130
+ unknown: "\u2753"
131
+ };
132
+ return icons[type];
133
+ }
134
+ function generateHTMLReport(summary, results) {
135
+ return `<!DOCTYPE html>
136
+ <html>
137
+ <head>
138
+ <title>Pattern Detection Report</title>
139
+ <style>
140
+ body { font-family: system-ui, -apple-system, sans-serif; margin: 40px; background: #f5f5f5; }
141
+ .container { max-width: 1200px; margin: 0 auto; background: white; padding: 30px; border-radius: 8px; box-shadow: 0 2px 8px rgba(0,0,0,0.1); }
142
+ h1 { color: #333; border-bottom: 3px solid #007acc; padding-bottom: 10px; }
143
+ .summary { display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); gap: 20px; margin: 30px 0; }
144
+ .stat-card { background: #f9f9f9; padding: 20px; border-radius: 6px; border-left: 4px solid #007acc; }
145
+ .stat-value { font-size: 32px; font-weight: bold; color: #007acc; }
146
+ .stat-label { color: #666; font-size: 14px; text-transform: uppercase; }
147
+ table { width: 100%; border-collapse: collapse; margin: 20px 0; }
148
+ th, td { text-align: left; padding: 12px; border-bottom: 1px solid #ddd; }
149
+ th { background: #f5f5f5; font-weight: 600; }
150
+ .critical { color: #d32f2f; }
151
+ .major { color: #f57c00; }
152
+ .minor { color: #1976d2; }
153
+ </style>
154
+ </head>
155
+ <body>
156
+ <div class="container">
157
+ <h1>\u{1F50D} Pattern Detection Report</h1>
158
+ <p>Generated: ${(/* @__PURE__ */ new Date()).toLocaleString()}</p>
159
+
160
+ <div class="summary">
161
+ <div class="stat-card">
162
+ <div class="stat-value">${results.length}</div>
163
+ <div class="stat-label">Files Analyzed</div>
164
+ </div>
165
+ <div class="stat-card">
166
+ <div class="stat-value">${summary.totalPatterns}</div>
167
+ <div class="stat-label">Duplicate Patterns</div>
168
+ </div>
169
+ <div class="stat-card">
170
+ <div class="stat-value">${summary.totalTokenCost.toLocaleString()}</div>
171
+ <div class="stat-label">Tokens Wasted</div>
172
+ </div>
173
+ </div>
174
+
175
+ <h2>Top Duplicate Patterns</h2>
176
+ <table>
177
+ <thead>
178
+ <tr>
179
+ <th>Similarity</th>
180
+ <th>Type</th>
181
+ <th>File 1</th>
182
+ <th>File 2</th>
183
+ <th>Token Cost</th>
184
+ </tr>
185
+ </thead>
186
+ <tbody>
187
+ ${summary.topDuplicates.slice(0, 20).map(
188
+ (dup) => `
189
+ <tr>
190
+ <td class="${dup.similarity > 0.95 ? "critical" : dup.similarity > 0.9 ? "major" : "minor"}">${Math.round(dup.similarity * 100)}%</td>
191
+ <td>${dup.patternType}</td>
192
+ <td><code>${dup.file1}</code></td>
193
+ <td><code>${dup.file2}</code></td>
194
+ <td>${dup.tokenCost.toLocaleString()}</td>
195
+ </tr>
196
+ `
197
+ ).join("")}
198
+ </tbody>
199
+ </table>
200
+ </div>
201
+ </body>
202
+ </html>`;
203
+ }
204
+ program.parse();