-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcolor-analysis.js
More file actions
110 lines (90 loc) · 3.26 KB
/
color-analysis.js
File metadata and controls
110 lines (90 loc) · 3.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#!/usr/bin/env node
const fs = require("fs");
const path = require("path");
// Color pattern definitions
const patterns = {
tailwindColors:
/\b(bg|text|border|ring|shadow|from|to|via)-([a-z]+)-(\d+)\b/g,
tailwindNamedColors:
/\b(bg|text|border|ring|shadow|from|to|via)-(red|blue|green|yellow|orange|purple|pink|gray|grey|black|white|slate|zinc|neutral|stone|amber|lime|emerald|teal|cyan|sky|indigo|violet|fuchsia|rose)\b/g,
hexColors: /#[0-9a-fA-F]{3,8}/g,
rgbaColors: /rgba?\([0-9,\s\.]+\)/g,
cssCustomProps: /--[a-zA-Z-]*color[a-zA-Z-]*/g,
namedColors:
/\b(red|blue|green|yellow|orange|purple|pink|gray|grey|black|white|brown|cyan|magenta|lime|indigo|violet|crimson|navy|teal|olive|maroon|silver|gold|transparent|inherit|currentColor)\b/g,
};
const colorCounts = {};
function initializeColorCounts() {
Object.keys(patterns).forEach((patternName) => {
colorCounts[patternName] = {};
});
}
function processFile(filePath) {
try {
const content = fs.readFileSync(filePath, "utf8");
Object.keys(patterns).forEach((patternName) => {
const pattern = patterns[patternName];
let match;
while ((match = pattern.exec(content)) !== null) {
const colorValue = match[0];
if (!colorCounts[patternName][colorValue]) {
colorCounts[patternName][colorValue] = 0;
}
colorCounts[patternName][colorValue]++;
}
});
} catch (error) {
console.error(`Error processing file ${filePath}:`, error.message);
}
}
function walkDirectory(dir, fileExtensions) {
const items = fs.readdirSync(dir);
for (const item of items) {
const fullPath = path.join(dir, item);
const stat = fs.statSync(fullPath);
if (stat.isDirectory()) {
// Skip node_modules, .next, .git
if (!["node_modules", ".next", ".git"].includes(item)) {
walkDirectory(fullPath, fileExtensions);
}
} else if (stat.isFile()) {
const ext = path.extname(item);
if (fileExtensions.includes(ext)) {
processFile(fullPath);
}
}
}
}
function generateReport() {
console.log("=".repeat(80));
console.log("COLOR USAGE ANALYSIS REPORT");
console.log("=".repeat(80));
let totalColors = 0;
Object.keys(colorCounts).forEach((patternName) => {
const colors = colorCounts[patternName];
const sortedColors = Object.entries(colors).sort(([, a], [, b]) => b - a);
if (sortedColors.length > 0) {
console.log(`\n${patternName.toUpperCase()}:`);
console.log("-".repeat(40));
sortedColors.forEach(([color, count]) => {
console.log(` ${color.padEnd(30)} ${count} times`);
totalColors += count;
});
console.log(` Total unique colors: ${sortedColors.length}`);
console.log(
` Total usages: ${sortedColors.reduce((sum, [, count]) => sum + count, 0)}`,
);
}
});
console.log("\n" + "=".repeat(80));
console.log(`SUMMARY: ${totalColors} total color usages found`);
console.log("=".repeat(80));
}
// Main execution
initializeColorCounts();
const fileExtensions = [".tsx", ".ts", ".jsx", ".js", ".css", ".json"];
const startDir = process.cwd();
console.log(`Analyzing color usage in: ${startDir}`);
console.log(`File extensions: ${fileExtensions.join(", ")}`);
walkDirectory(startDir, fileExtensions);
generateReport();