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,47 @@
1
+ import { ProgressCallback } from './types';
2
+
3
+ export class ProgressBar {
4
+ private static readonly BAR_LENGTH = 40;
5
+ private lastUpdateTime = 0;
6
+ private readonly updateInterval = 100; // Update every 100ms
7
+
8
+ show(current: number, total: number, currentPath?: string): void {
9
+ const now = Date.now();
10
+ if (now - this.lastUpdateTime < this.updateInterval && current !== total) {
11
+ return;
12
+ }
13
+ this.lastUpdateTime = now;
14
+
15
+ const percentage = Math.min(100, Math.round((current / total) * 100));
16
+ const filled = Math.round((current / total) * ProgressBar.BAR_LENGTH);
17
+ const empty = ProgressBar.BAR_LENGTH - filled;
18
+
19
+ const bar = '█'.repeat(filled) + '░'.repeat(empty);
20
+ const progressText = `[${bar}] ${percentage}% (${current}/${total})`;
21
+
22
+ let output = `\r${progressText}`;
23
+
24
+ if (currentPath) {
25
+ const maxPathLength = process.stdout.columns ? process.stdout.columns - progressText.length - 5 : 50;
26
+ const displayPath = currentPath.length > maxPathLength
27
+ ? '...' + currentPath.slice(-(maxPathLength - 3))
28
+ : currentPath;
29
+ output += ` ${displayPath}`;
30
+ }
31
+
32
+ process.stdout.write(output);
33
+
34
+ if (current === total) {
35
+ process.stdout.write('\n');
36
+ }
37
+ }
38
+
39
+ static createCallback(enabled: boolean = true): ProgressCallback | undefined {
40
+ if (!enabled) return undefined;
41
+
42
+ const progressBar = new ProgressBar();
43
+ return (current: number, total: number, currentPath?: string) => {
44
+ progressBar.show(current, total, currentPath);
45
+ };
46
+ }
47
+ }
@@ -0,0 +1,129 @@
1
+ import * as path from 'path';
2
+ import { formatSize } from './utils';
3
+
4
+ export interface TreeNode {
5
+ name: string;
6
+ path: string;
7
+ size?: number;
8
+ isDirectory: boolean;
9
+ children?: TreeNode[];
10
+ }
11
+
12
+ export class TreeViewGenerator {
13
+ static generateTreeView(files: Array<{ path: string; size: number }>, rootPath: string): string {
14
+ const tree = this.buildTree(files, rootPath);
15
+ return this.renderTree(tree);
16
+ }
17
+
18
+ private static buildTree(files: Array<{ path: string; size: number }>, rootPath: string): TreeNode {
19
+ const root: TreeNode = {
20
+ name: path.basename(rootPath) || rootPath,
21
+ path: rootPath,
22
+ isDirectory: true,
23
+ children: []
24
+ };
25
+
26
+ const nodeMap = new Map<string, TreeNode>();
27
+ nodeMap.set(rootPath, root);
28
+
29
+ // Sort files by path to ensure consistent ordering
30
+ const sortedFiles = files.sort((a, b) => a.path.localeCompare(b.path));
31
+
32
+ for (const file of sortedFiles) {
33
+ const relativePath = path.relative(rootPath, file.path);
34
+ if (!relativePath || relativePath === '.') continue;
35
+
36
+ const parts = relativePath.split(path.sep);
37
+ let currentPath = rootPath;
38
+ let currentNode = root;
39
+
40
+ // Create directory nodes
41
+ for (let i = 0; i < parts.length - 1; i++) {
42
+ const part = parts[i];
43
+ currentPath = path.join(currentPath, part);
44
+
45
+ if (!nodeMap.has(currentPath)) {
46
+ const dirNode: TreeNode = {
47
+ name: part,
48
+ path: currentPath,
49
+ isDirectory: true,
50
+ children: []
51
+ };
52
+ nodeMap.set(currentPath, dirNode);
53
+ currentNode.children!.push(dirNode);
54
+ }
55
+ currentNode = nodeMap.get(currentPath)!;
56
+ }
57
+
58
+ // Create file node
59
+ const fileName = parts[parts.length - 1];
60
+ const fileNode: TreeNode = {
61
+ name: fileName,
62
+ path: file.path,
63
+ size: file.size,
64
+ isDirectory: false
65
+ };
66
+ currentNode.children!.push(fileNode);
67
+ }
68
+
69
+ // Sort children: directories first, then files, both alphabetically
70
+ this.sortTreeNodes(root);
71
+ return root;
72
+ }
73
+
74
+ private static sortTreeNodes(node: TreeNode): void {
75
+ if (!node.children) return;
76
+
77
+ node.children.sort((a, b) => {
78
+ // Directories first
79
+ if (a.isDirectory && !b.isDirectory) return -1;
80
+ if (!a.isDirectory && b.isDirectory) return 1;
81
+
82
+ // Then alphabetically
83
+ return a.name.localeCompare(b.name);
84
+ });
85
+
86
+ // Recursively sort children
87
+ for (const child of node.children) {
88
+ this.sortTreeNodes(child);
89
+ }
90
+ }
91
+
92
+ private static renderTree(node: TreeNode, prefix: string = '', isLast: boolean = true, level: number = 0): string {
93
+ const MAX_DEPTH = 10; // Prevent infinite recursion
94
+ if (level > MAX_DEPTH) return '';
95
+
96
+ let result = '';
97
+ const connector = isLast ? '└── ' : '├── ';
98
+ const sizeInfo = node.isDirectory ? '' : ` (${formatSize(node.size || 0)})`;
99
+ const icon = node.isDirectory ? '📁' : '📄';
100
+
101
+ result += `${prefix}${connector}${icon} ${node.name}${sizeInfo}\n`;
102
+
103
+ if (node.children && node.children.length > 0) {
104
+ const newPrefix = prefix + (isLast ? ' ' : '│ ');
105
+
106
+ for (let i = 0; i < node.children.length; i++) {
107
+ const child = node.children[i];
108
+ const childIsLast = i === node.children.length - 1;
109
+ result += this.renderTree(child, newPrefix, childIsLast, level + 1);
110
+ }
111
+ }
112
+
113
+ return result;
114
+ }
115
+
116
+ static generateCompactTreeView(files: Array<{ path: string; size: number }>, rootPath: string, maxFiles: number = 50): string {
117
+ // Limit the number of files to display for performance
118
+ const limitedFiles = files.slice(0, maxFiles);
119
+ const tree = this.buildTree(limitedFiles, rootPath);
120
+
121
+ let result = this.renderTree(tree);
122
+
123
+ if (files.length > maxFiles) {
124
+ result += `\n... and ${files.length - maxFiles} more files\n`;
125
+ }
126
+
127
+ return result;
128
+ }
129
+ }
package/src/types.ts ADDED
@@ -0,0 +1,47 @@
1
+ export interface LargeFile {
2
+ path: string;
3
+ size: number;
4
+ sizeFormatted: string;
5
+ modifiedDate?: Date;
6
+ createdDate?: Date;
7
+ }
8
+
9
+ export interface EmptyFile {
10
+ path: string;
11
+ modifiedDate: Date;
12
+ }
13
+
14
+ export interface FileInfo {
15
+ path: string;
16
+ size: number;
17
+ modifiedDate: Date;
18
+ createdDate: Date;
19
+ }
20
+
21
+ export interface DuplicateFileGroup {
22
+ hash: string;
23
+ size: number;
24
+ sizeFormatted: string;
25
+ files: string[];
26
+ totalWastedSpace: number;
27
+ totalWastedSpaceFormatted: string;
28
+ }
29
+
30
+ export interface ProgressCallback {
31
+ (current: number, total: number, currentPath?: string): void;
32
+ }
33
+
34
+ export interface ConfigOptions {
35
+ excludePatterns?: string[];
36
+ largeSizeThreshold?: number;
37
+ enableDuplicateDetection?: boolean;
38
+ enableProgressBar?: boolean;
39
+ outputFormat?: 'table' | 'json' | 'csv' | 'tree';
40
+ maxDepth?: number;
41
+ minSize?: number;
42
+ maxSize?: number;
43
+ dateFrom?: Date;
44
+ dateTo?: Date;
45
+ topN?: number;
46
+ showEmptyFiles?: boolean;
47
+ }
package/src/utils.ts ADDED
@@ -0,0 +1,54 @@
1
+ import prettyBytes from 'pretty-bytes';
2
+
3
+ export function formatSize(bytes: number): string {
4
+ return prettyBytes(bytes);
5
+ }
6
+
7
+ export function formatSizeInMB(bytes: number): number {
8
+ return Math.round((bytes / (1024 * 1024)) * 10) / 10;
9
+ }
10
+
11
+ export function shouldExcludeDirectory(dirName: string, excludePatterns: string[] = []): boolean {
12
+ const defaultExcludes = ['node_modules', '.git', '.svn', '.hg', 'dist', 'build', '.cache'];
13
+ const allExcludes = [...defaultExcludes, ...excludePatterns];
14
+
15
+ return allExcludes.some(pattern => {
16
+ if (pattern.includes('*')) {
17
+ // Simple glob pattern matching
18
+ const regex = new RegExp(pattern.replace(/\*/g, '.*'));
19
+ return regex.test(dirName);
20
+ }
21
+ return dirName === pattern;
22
+ });
23
+ }
24
+
25
+ export function shouldExcludeFile(fileName: string, excludePatterns: string[] = []): boolean {
26
+ return excludePatterns.some(pattern => {
27
+ if (pattern.includes('*')) {
28
+ // Simple glob pattern matching
29
+ const regex = new RegExp(pattern.replace(/\*/g, '.*'));
30
+ return regex.test(fileName);
31
+ }
32
+ return fileName.endsWith(pattern) || fileName === pattern;
33
+ });
34
+ }
35
+
36
+ export const EMOJIS = {
37
+ folder: '📂',
38
+ package: '📦',
39
+ folderIcon: '📁',
40
+ fileIcon: '📄',
41
+ types: '🗂',
42
+ images: '📷',
43
+ videos: '🎬',
44
+ documents: '📄',
45
+ audio: '🎵',
46
+ code: '🧑‍💻',
47
+ archives: '🗃️',
48
+ other: '❓',
49
+ largeFile: '🚨',
50
+ duplicates: '🔄',
51
+ progress: '🔍',
52
+ success: '✅',
53
+ csv: '📄'
54
+ } as const;
package/tsconfig.json ADDED
@@ -0,0 +1,31 @@
1
+ {
2
+ "compilerOptions": {
3
+ "target": "ES2022",
4
+ "module": "CommonJS",
5
+ "lib": [
6
+ "ES2022"
7
+ ],
8
+ "outDir": "./bin",
9
+ "rootDir": "./src",
10
+ "strict": true,
11
+ "esModuleInterop": true,
12
+ "skipLibCheck": true,
13
+ "forceConsistentCasingInFileNames": true,
14
+ "declaration": true,
15
+ "declarationMap": true,
16
+ "sourceMap": true,
17
+ "moduleResolution": "node",
18
+ "resolveJsonModule": true,
19
+ "allowSyntheticDefaultImports": true,
20
+ "types": [
21
+ "node"
22
+ ]
23
+ },
24
+ "include": [
25
+ "src/**/*"
26
+ ],
27
+ "exclude": [
28
+ "node_modules",
29
+ "bin"
30
+ ]
31
+ }