dir-analysis-tool 1.0.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.
@@ -0,0 +1,704 @@
1
+ import { ExtendedAnalysisResult } from './export';
2
+ import { formatSize } from './utils';
3
+ import { promises as fs } from 'fs';
4
+
5
+ export class HTMLReportGenerator {
6
+ static async generateReport(result: ExtendedAnalysisResult, filename?: string): Promise<string> {
7
+ let outputFile = filename || `directory-analysis-${new Date().toISOString().slice(0, 10)}.html`;
8
+
9
+ // Check if the provided filename is actually a directory
10
+ if (filename) {
11
+ try {
12
+ const stats = await fs.stat(filename);
13
+ if (stats.isDirectory()) {
14
+ // If it's a directory, generate a filename inside that directory
15
+ const path = require('path');
16
+ outputFile = path.join(filename, `directory-analysis-${new Date().toISOString().slice(0, 10)}.html`);
17
+ }
18
+ } catch (error) {
19
+ // If stat fails, assume it's a valid filename (file doesn't exist yet)
20
+ outputFile = filename;
21
+ }
22
+ }
23
+
24
+ const html = this.generateHTML(result);
25
+ await fs.writeFile(outputFile, html);
26
+
27
+ return outputFile;
28
+ }
29
+
30
+ private static generateHTML(result: ExtendedAnalysisResult): string {
31
+ return `<!DOCTYPE html>
32
+ <html lang="en">
33
+ <head>
34
+ <meta charset="UTF-8">
35
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
36
+ <title>Directory Analysis Report - ${result.path}</title>
37
+ <script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
38
+ <style>
39
+ ${this.getCSS()}
40
+ </style>
41
+ </head>
42
+ <body>
43
+ <div class="container">
44
+ <header class="header">
45
+ <h1>📂 Directory Analysis Report</h1>
46
+ <p class="path">📍 ${result.path}</p>
47
+ <p class="timestamp">📅 Generated on ${new Date().toLocaleString()}</p>
48
+ </header>
49
+
50
+ <div class="summary-grid">
51
+ <div class="summary-card">
52
+ <div class="summary-icon">📦</div>
53
+ <div class="summary-content">
54
+ <h3>Total Size</h3>
55
+ <p class="summary-value">${formatSize(result.totalSizeBytes)}</p>
56
+ </div>
57
+ </div>
58
+ <div class="summary-card">
59
+ <div class="summary-icon">📄</div>
60
+ <div class="summary-content">
61
+ <h3>Files</h3>
62
+ <p class="summary-value">${result.files.toLocaleString()}</p>
63
+ </div>
64
+ </div>
65
+ <div class="summary-card">
66
+ <div class="summary-icon">📁</div>
67
+ <div class="summary-content">
68
+ <h3>Folders</h3>
69
+ <p class="summary-value">${result.folders.toLocaleString()}</p>
70
+ </div>
71
+ </div>
72
+ <div class="summary-card">
73
+ <div class="summary-icon">🗂️</div>
74
+ <div class="summary-content">
75
+ <h3>File Types</h3>
76
+ <p class="summary-value">${this.countNonZeroTypes(result.types)}</p>
77
+ </div>
78
+ </div>
79
+ </div>
80
+
81
+ <div class="charts-container">
82
+ <div class="chart-section">
83
+ <h2>📊 File Type Distribution</h2>
84
+ <div class="chart-wrapper">
85
+ <canvas id="fileTypeChart"></canvas>
86
+ </div>
87
+ </div>
88
+
89
+ ${result.topLargestFiles && result.topLargestFiles.length > 0 ? `
90
+ <div class="chart-section">
91
+ <h2>📈 Largest Files</h2>
92
+ <div class="chart-wrapper">
93
+ <canvas id="largestFilesChart"></canvas>
94
+ </div>
95
+ </div>
96
+ ` : ''}
97
+ </div>
98
+
99
+ ${result.largeFiles && result.largeFiles.length > 0 ? `
100
+ <div class="section">
101
+ <h2>🚨 Large Files</h2>
102
+ <div class="table-wrapper">
103
+ <table class="data-table">
104
+ <thead>
105
+ <tr>
106
+ <th>File Path</th>
107
+ <th>Size</th>
108
+ </tr>
109
+ </thead>
110
+ <tbody>
111
+ ${result.largeFiles.slice(0, 20).map(file => `
112
+ <tr>
113
+ <td class="file-path">${this.escapeHtml(file.path)}</td>
114
+ <td class="file-size">${file.sizeFormatted}</td>
115
+ </tr>
116
+ `).join('')}
117
+ </tbody>
118
+ </table>
119
+ </div>
120
+ </div>
121
+ ` : ''}
122
+
123
+ ${result.duplicateGroups && result.duplicateGroups.length > 0 ? `
124
+ <div class="section">
125
+ <h2>🔄 Duplicate Files</h2>
126
+ <div class="duplicate-stats">
127
+ <p>📊 Total Groups: <strong>${result.duplicateGroups.length}</strong></p>
128
+ ${result.duplicateStats ? `
129
+ <p>💾 Wasted Space: <strong>${result.duplicateStats.totalWastedSpaceFormatted}</strong></p>
130
+ ` : ''}
131
+ </div>
132
+ <div class="duplicate-groups">
133
+ ${result.duplicateGroups.slice(0, 10).map((group, index) => `
134
+ <div class="duplicate-group">
135
+ <h4>Group ${index + 1} - ${group.sizeFormatted} each × ${group.files.length} files</h4>
136
+ <ul class="duplicate-files">
137
+ ${group.files.slice(0, 5).map(file => `
138
+ <li class="duplicate-file">${this.escapeHtml(file)}</li>
139
+ `).join('')}
140
+ ${group.files.length > 5 ? `
141
+ <li class="more-files">... and ${group.files.length - 5} more files</li>
142
+ ` : ''}
143
+ </ul>
144
+ </div>
145
+ `).join('')}
146
+ </div>
147
+ </div>
148
+ ` : ''}
149
+
150
+ ${result.emptyFiles && result.emptyFiles.length > 0 ? `
151
+ <div class="section">
152
+ <h2>📭 Empty Files</h2>
153
+ <div class="table-wrapper">
154
+ <table class="data-table">
155
+ <thead>
156
+ <tr>
157
+ <th>File Path</th>
158
+ <th>Modified Date</th>
159
+ </tr>
160
+ </thead>
161
+ <tbody>
162
+ ${result.emptyFiles.slice(0, 20).map(file => `
163
+ <tr>
164
+ <td class="file-path">${this.escapeHtml(file.path)}</td>
165
+ <td class="file-date">${file.modifiedDate.toLocaleDateString()}</td>
166
+ </tr>
167
+ `).join('')}
168
+ </tbody>
169
+ </table>
170
+ </div>
171
+ </div>
172
+ ` : ''}
173
+
174
+ <footer class="footer">
175
+ <p>Generated by Dir Analyzer v1.0.0</p>
176
+ </footer>
177
+ </div>
178
+
179
+ <script>
180
+ ${this.getJavaScript(result)}
181
+ </script>
182
+ </body>
183
+ </html>`;
184
+ } private static getCSS(): string {
185
+ return `
186
+ * {
187
+ margin: 0;
188
+ padding: 0;
189
+ box-sizing: border-box;
190
+ }
191
+
192
+ body {
193
+ font-family: 'Fira Code', 'Consolas', 'Monaco', 'Courier New', monospace;
194
+ line-height: 1.6;
195
+ color: #00ff00;
196
+ background: #0a0a0a;
197
+ min-height: 100vh;
198
+ background-image:
199
+ linear-gradient(90deg, rgba(0,255,0,0.03) 1px, transparent 1px),
200
+ linear-gradient(rgba(0,255,0,0.03) 1px, transparent 1px);
201
+ background-size: 20px 20px;
202
+ }
203
+
204
+ .container {
205
+ max-width: 1200px;
206
+ margin: 0 auto;
207
+ padding: 20px;
208
+ }
209
+
210
+ .header {
211
+ background: #1a1a1a;
212
+ padding: 30px;
213
+ border: 2px solid #00ff00;
214
+ border-radius: 8px;
215
+ box-shadow: 0 0 20px rgba(0,255,0,0.3);
216
+ margin-bottom: 30px;
217
+ text-align: center;
218
+ position: relative;
219
+ }
220
+
221
+ .header::before {
222
+ content: '> ';
223
+ position: absolute;
224
+ left: 15px;
225
+ top: 15px;
226
+ color: #00ff00;
227
+ font-weight: bold;
228
+ }
229
+
230
+ .header h1 {
231
+ font-size: 2.5em;
232
+ margin-bottom: 10px;
233
+ color: #00ff00;
234
+ text-shadow: 0 0 10px rgba(0,255,0,0.5);
235
+ font-weight: bold;
236
+ }
237
+
238
+ .path {
239
+ font-size: 1.2em;
240
+ color: #00cccc;
241
+ margin-bottom: 5px;
242
+ font-family: 'Fira Code', monospace;
243
+ }
244
+
245
+ .timestamp {
246
+ color: #666;
247
+ font-family: 'Fira Code', monospace;
248
+ }
249
+
250
+ .summary-grid {
251
+ display: grid;
252
+ grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
253
+ gap: 20px;
254
+ margin-bottom: 30px;
255
+ }
256
+
257
+ .summary-card {
258
+ background: #1a1a1a;
259
+ padding: 25px;
260
+ border: 1px solid #333;
261
+ border-radius: 8px;
262
+ box-shadow: 0 0 15px rgba(0,255,0,0.1);
263
+ display: flex;
264
+ align-items: center;
265
+ transition: all 0.3s ease;
266
+ position: relative;
267
+ }
268
+
269
+ .summary-card::before {
270
+ content: '$ ';
271
+ position: absolute;
272
+ left: 10px;
273
+ top: 10px;
274
+ color: #00ff00;
275
+ font-size: 0.9em;
276
+ }
277
+
278
+ .summary-card:hover {
279
+ border-color: #00ff00;
280
+ box-shadow: 0 0 25px rgba(0,255,0,0.3);
281
+ transform: translateY(-2px);
282
+ }
283
+
284
+ .summary-icon {
285
+ font-size: 3em;
286
+ margin-right: 20px;
287
+ filter: grayscale(1) brightness(1.5);
288
+ }
289
+
290
+ .summary-content h3 {
291
+ color: #00cccc;
292
+ margin-bottom: 5px;
293
+ text-transform: uppercase;
294
+ letter-spacing: 1px;
295
+ }
296
+
297
+ .summary-value {
298
+ font-size: 2em;
299
+ font-weight: bold;
300
+ color: #00ff00;
301
+ text-shadow: 0 0 5px rgba(0,255,0,0.5);
302
+ }
303
+
304
+ .charts-container {
305
+ display: grid;
306
+ grid-template-columns: repeat(auto-fit, minmax(400px, 1fr));
307
+ gap: 30px;
308
+ margin-bottom: 30px;
309
+ }
310
+
311
+ .chart-section {
312
+ background: #1a1a1a;
313
+ padding: 25px;
314
+ border: 1px solid #333;
315
+ border-radius: 8px;
316
+ box-shadow: 0 0 15px rgba(0,255,0,0.1);
317
+ position: relative;
318
+ }
319
+
320
+ .chart-section::before {
321
+ content: '>> ';
322
+ position: absolute;
323
+ left: 10px;
324
+ top: 10px;
325
+ color: #00ff00;
326
+ font-size: 0.9em;
327
+ }
328
+
329
+ .chart-section h2 {
330
+ margin-bottom: 20px;
331
+ color: #00ff00;
332
+ text-transform: uppercase;
333
+ letter-spacing: 1px;
334
+ border-bottom: 2px solid #00ff00;
335
+ padding-bottom: 10px;
336
+ }
337
+
338
+ .chart-wrapper {
339
+ position: relative;
340
+ height: 300px;
341
+ }
342
+
343
+ .section {
344
+ background: #1a1a1a;
345
+ padding: 25px;
346
+ border: 1px solid #333;
347
+ border-radius: 8px;
348
+ box-shadow: 0 0 15px rgba(0,255,0,0.1);
349
+ margin-bottom: 30px;
350
+ position: relative;
351
+ }
352
+
353
+ .section::before {
354
+ content: '>>> ';
355
+ position: absolute;
356
+ left: 10px;
357
+ top: 10px;
358
+ color: #00ff00;
359
+ font-size: 0.9em;
360
+ }
361
+
362
+ .section h2 {
363
+ margin-bottom: 20px;
364
+ color: #00ff00;
365
+ border-bottom: 2px solid #00ff00;
366
+ padding-bottom: 10px;
367
+ text-transform: uppercase;
368
+ letter-spacing: 1px;
369
+ }
370
+
371
+ .table-wrapper {
372
+ overflow-x: auto;
373
+ }
374
+
375
+ .data-table {
376
+ width: 100%;
377
+ border-collapse: collapse;
378
+ margin-top: 10px;
379
+ border: 1px solid #333;
380
+ }
381
+
382
+ .data-table th,
383
+ .data-table td {
384
+ padding: 12px;
385
+ text-align: left;
386
+ border-bottom: 1px solid #333;
387
+ border-right: 1px solid #333;
388
+ }
389
+
390
+ .data-table th {
391
+ background: #0d0d0d;
392
+ font-weight: 600;
393
+ color: #00cccc;
394
+ text-transform: uppercase;
395
+ letter-spacing: 1px;
396
+ }
397
+
398
+ .data-table tr:hover {
399
+ background: #2a2a2a;
400
+ }
401
+
402
+ .file-path {
403
+ font-family: 'Fira Code', 'Consolas', monospace;
404
+ font-size: 0.9em;
405
+ color: #00ff00;
406
+ }
407
+
408
+ .file-size {
409
+ font-weight: 600;
410
+ color: #ff6b6b;
411
+ font-family: 'Fira Code', monospace;
412
+ }
413
+
414
+ .file-date {
415
+ color: #666;
416
+ font-family: 'Fira Code', monospace;
417
+ }
418
+
419
+ .duplicate-stats {
420
+ background: #0d0d0d;
421
+ padding: 15px;
422
+ border: 1px solid #333;
423
+ border-radius: 4px;
424
+ margin-bottom: 20px;
425
+ }
426
+
427
+ .duplicate-group {
428
+ border: 1px solid #333;
429
+ border-radius: 4px;
430
+ margin-bottom: 15px;
431
+ padding: 15px;
432
+ background: #0d0d0d;
433
+ }
434
+
435
+ .duplicate-group h4 {
436
+ color: #00cccc;
437
+ margin-bottom: 10px;
438
+ font-family: 'Fira Code', monospace;
439
+ }
440
+
441
+ .duplicate-files {
442
+ list-style: none;
443
+ }
444
+
445
+ .duplicate-file {
446
+ padding: 5px 0;
447
+ font-family: 'Fira Code', monospace;
448
+ font-size: 0.9em;
449
+ color: #00ff00;
450
+ }
451
+
452
+ .duplicate-file::before {
453
+ content: ' - ';
454
+ color: #666;
455
+ }
456
+
457
+ .more-files {
458
+ padding: 5px 0;
459
+ color: #666;
460
+ font-style: italic;
461
+ font-family: 'Fira Code', monospace;
462
+ }
463
+
464
+ .footer {
465
+ text-align: center;
466
+ padding: 20px;
467
+ color: #666;
468
+ font-size: 0.9em;
469
+ font-family: 'Fira Code', monospace;
470
+ border-top: 1px solid #333;
471
+ margin-top: 30px;
472
+ }
473
+
474
+ .footer::before {
475
+ content: '~$ ';
476
+ color: #00ff00;
477
+ }
478
+
479
+ @media (max-width: 768px) {
480
+ .container {
481
+ padding: 10px;
482
+ }
483
+
484
+ .header h1 {
485
+ font-size: 2em;
486
+ }
487
+
488
+ .summary-grid {
489
+ grid-template-columns: 1fr;
490
+ }
491
+
492
+ .charts-container {
493
+ grid-template-columns: 1fr;
494
+ }
495
+
496
+ .summary-card {
497
+ text-align: center;
498
+ flex-direction: column;
499
+ }
500
+
501
+ .summary-icon {
502
+ margin-right: 0;
503
+ margin-bottom: 10px;
504
+ }
505
+ }
506
+ `;
507
+ }
508
+
509
+ private static getJavaScript(result: ExtendedAnalysisResult): string {
510
+ const fileTypeData = this.getFileTypeChartData(result.types);
511
+ const largestFilesData = this.getLargestFilesChartData(result.topLargestFiles);
512
+
513
+ return ` // File Type Chart
514
+ const fileTypeCtx = document.getElementById('fileTypeChart').getContext('2d');
515
+ new Chart(fileTypeCtx, {
516
+ type: 'doughnut',
517
+ data: {
518
+ labels: ${JSON.stringify(fileTypeData.labels)},
519
+ datasets: [{
520
+ data: ${JSON.stringify(fileTypeData.data)},
521
+ backgroundColor: [
522
+ '#00ff00', '#00cccc', '#ffff00', '#ff6b6b',
523
+ '#9966ff', '#ff9900', '#66ff66', '#ff3366'
524
+ ],
525
+ borderColor: '#0a0a0a',
526
+ borderWidth: 2
527
+ }]
528
+ },
529
+ options: {
530
+ responsive: true,
531
+ maintainAspectRatio: false,
532
+ plugins: {
533
+ legend: {
534
+ position: 'bottom',
535
+ labels: {
536
+ padding: 20,
537
+ usePointStyle: true,
538
+ color: '#00ff00',
539
+ font: {
540
+ family: 'Fira Code, Consolas, Monaco, monospace',
541
+ size: 12
542
+ }
543
+ }
544
+ },
545
+ tooltip: {
546
+ backgroundColor: '#1a1a1a',
547
+ titleColor: '#00ff00',
548
+ bodyColor: '#00cccc',
549
+ borderColor: '#333',
550
+ borderWidth: 1,
551
+ titleFont: {
552
+ family: 'Fira Code, Consolas, Monaco, monospace'
553
+ },
554
+ bodyFont: {
555
+ family: 'Fira Code, Consolas, Monaco, monospace'
556
+ },
557
+ callbacks: {
558
+ label: function(context) {
559
+ const label = context.label || '';
560
+ const value = context.parsed;
561
+ const total = context.dataset.data.reduce((a, b) => a + b, 0);
562
+ const percentage = ((value / total) * 100).toFixed(1);
563
+ return label + ': ' + value + ' files (' + percentage + '%)';
564
+ }
565
+ }
566
+ }
567
+ }
568
+ }
569
+ });
570
+
571
+ ${result.topLargestFiles && result.topLargestFiles.length > 0 ? `
572
+ // Largest Files Chart
573
+ const largestFilesCtx = document.getElementById('largestFilesChart').getContext('2d');
574
+ new Chart(largestFilesCtx, {
575
+ type: 'bar',
576
+ data: {
577
+ labels: ${JSON.stringify(largestFilesData.labels)},
578
+ datasets: [{
579
+ label: 'File Size',
580
+ data: ${JSON.stringify(largestFilesData.data)},
581
+ backgroundColor: '#00ff00',
582
+ borderColor: '#00cccc',
583
+ borderWidth: 1
584
+ }]
585
+ },
586
+ options: {
587
+ responsive: true,
588
+ maintainAspectRatio: false,
589
+ scales: {
590
+ y: {
591
+ beginAtZero: true,
592
+ ticks: {
593
+ color: '#00ff00',
594
+ font: {
595
+ family: 'Fira Code, Consolas, Monaco, monospace'
596
+ },
597
+ callback: function(value) {
598
+ return formatBytes(value);
599
+ }
600
+ },
601
+ grid: {
602
+ color: '#333'
603
+ }
604
+ },
605
+ x: {
606
+ ticks: {
607
+ color: '#00ff00',
608
+ font: {
609
+ family: 'Fira Code, Consolas, Monaco, monospace'
610
+ },
611
+ maxRotation: 45,
612
+ minRotation: 45
613
+ },
614
+ grid: {
615
+ color: '#333'
616
+ }
617
+ }
618
+ },
619
+ plugins: {
620
+ legend: {
621
+ display: false
622
+ },
623
+ tooltip: {
624
+ backgroundColor: '#1a1a1a',
625
+ titleColor: '#00ff00',
626
+ bodyColor: '#00cccc',
627
+ borderColor: '#333',
628
+ borderWidth: 1,
629
+ titleFont: {
630
+ family: 'Fira Code, Consolas, Monaco, monospace'
631
+ },
632
+ bodyFont: {
633
+ family: 'Fira Code, Consolas, Monaco, monospace'
634
+ },
635
+ callbacks: {
636
+ label: function(context) {
637
+ return 'Size: ' + formatBytes(context.parsed.y);
638
+ }
639
+ }
640
+ }
641
+ }
642
+ }
643
+ });
644
+ ` : ''}
645
+
646
+ function formatBytes(bytes) {
647
+ if (bytes === 0) return '0 Bytes';
648
+ const k = 1024;
649
+ const sizes = ['Bytes', 'KB', 'MB', 'GB'];
650
+ const i = Math.floor(Math.log(bytes) / Math.log(k));
651
+ return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + ' ' + sizes[i];
652
+ }
653
+ `;
654
+ }
655
+
656
+ private static getFileTypeChartData(types: any): { labels: string[], data: number[] } {
657
+ const typeMapping = {
658
+ code: '🧑‍💻 Code',
659
+ images: '📷 Images',
660
+ documents: '📄 Documents',
661
+ videos: '🎬 Videos',
662
+ audio: '🎵 Audio',
663
+ archives: '🗃️ Archives',
664
+ other: '❓ Other'
665
+ };
666
+
667
+ const labels: string[] = [];
668
+ const data: number[] = [];
669
+
670
+ Object.entries(types).forEach(([key, value]) => {
671
+ if ((value as number) > 0) {
672
+ labels.push(typeMapping[key as keyof typeof typeMapping] || key);
673
+ data.push(value as number);
674
+ }
675
+ });
676
+
677
+ return { labels, data };
678
+ }
679
+
680
+ private static getLargestFilesChartData(topFiles?: any[]): { labels: string[], data: number[] } {
681
+ if (!topFiles || topFiles.length === 0) {
682
+ return { labels: [], data: [] };
683
+ }
684
+
685
+ const labels = topFiles.map(file => {
686
+ const fileName = file.path.split(/[/\\]/).pop() || file.path;
687
+ return fileName.length > 20 ? fileName.substring(0, 17) + '...' : fileName;
688
+ });
689
+
690
+ const data = topFiles.map(file => file.size);
691
+
692
+ return { labels, data };
693
+ }
694
+
695
+ private static countNonZeroTypes(types: any): number {
696
+ return Object.values(types).filter(count => (count as number) > 0).length;
697
+ } private static escapeHtml(text: string): string {
698
+ return text
699
+ .replace(/&/g, '&amp;')
700
+ .replace(/</g, '&lt;').replace(/>/g, '&gt;')
701
+ .replace(/"/g, '&quot;')
702
+ .replace(/'/g, '&#39;');
703
+ }
704
+ }
package/src/index.ts ADDED
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env node
2
+ import './cli';