codesummary 1.1.0 → 1.2.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/src/utils.js ADDED
@@ -0,0 +1,139 @@
1
+ /**
2
+ * Shared utilities for CodeSummary
3
+ * Single source of truth for common helpers used across modules
4
+ */
5
+
6
+ import fs from 'fs';
7
+ import path from 'path';
8
+
9
+ /**
10
+ * Format a byte count as a human-readable string
11
+ * @param {number} bytes
12
+ * @returns {string} e.g. "1.4 MB"
13
+ */
14
+ export function formatFileSize(bytes) {
15
+ const units = ['B', 'KB', 'MB', 'GB'];
16
+ let size = bytes;
17
+ let unitIndex = 0;
18
+
19
+ while (size >= 1024 && unitIndex < units.length - 1) {
20
+ size /= 1024;
21
+ unitIndex++;
22
+ }
23
+
24
+ return `${size.toFixed(1)} ${units[unitIndex]}`;
25
+ }
26
+
27
+ /**
28
+ * Return a human-readable language name for a file extension
29
+ * @param {string} extension - e.g. ".js"
30
+ * @returns {string}
31
+ */
32
+ export function getExtensionDescription(extension) {
33
+ const descriptions = {
34
+ '.js': 'JavaScript',
35
+ '.ts': 'TypeScript',
36
+ '.jsx': 'React JSX',
37
+ '.tsx': 'TypeScript JSX',
38
+ '.json': 'JSON',
39
+ '.xml': 'XML',
40
+ '.html': 'HTML',
41
+ '.css': 'CSS',
42
+ '.scss': 'SCSS',
43
+ '.sass': 'Sass',
44
+ '.md': 'Markdown',
45
+ '.txt': 'Text',
46
+ '.py': 'Python',
47
+ '.java': 'Java',
48
+ '.cs': 'C#',
49
+ '.cpp': 'C++',
50
+ '.c': 'C',
51
+ '.h': 'Header',
52
+ '.yaml': 'YAML',
53
+ '.yml': 'YAML',
54
+ '.sh': 'Shell Script',
55
+ '.bat': 'Batch File',
56
+ '.ps1': 'PowerShell',
57
+ '.php': 'PHP',
58
+ '.rb': 'Ruby',
59
+ '.go': 'Go',
60
+ '.rs': 'Rust',
61
+ '.swift': 'Swift',
62
+ '.kt': 'Kotlin',
63
+ '.scala': 'Scala',
64
+ '.vue': 'Vue.js',
65
+ '.svelte': 'Svelte',
66
+ '.dockerfile': 'Dockerfile',
67
+ '.sql': 'SQL',
68
+ '.graphql': 'GraphQL',
69
+ '.cfg': 'Config File',
70
+ '.conf': 'Config File',
71
+ '.env': 'Environment',
72
+ '.local': 'Local Override',
73
+ '.service': 'Systemd Service',
74
+ '.timer': 'Systemd Timer',
75
+ '.ino': 'Arduino',
76
+ '.j2': 'Jinja2 Template',
77
+ '.csv': 'CSV',
78
+ '.tsv': 'TSV',
79
+ '.crt': 'Certificate',
80
+ '.toml': 'TOML',
81
+ '.ini': 'INI Config',
82
+ '.properties': 'Java Properties',
83
+ '.tf': 'Terraform',
84
+ '.tfvars': 'Terraform Vars',
85
+ '.proto': 'Protocol Buffers',
86
+ '.prisma': 'Prisma Schema',
87
+ '.dart': 'Dart',
88
+ '.lua': 'Lua',
89
+ '.r': 'R',
90
+ '.ex': 'Elixir',
91
+ '.exs': 'Elixir Script',
92
+ '.pl': 'Perl',
93
+ '.mk': 'Makefile',
94
+ '.cmake': 'CMake',
95
+ '.mdx': 'MDX',
96
+ '.astro': 'Astro',
97
+ '.graphql': 'GraphQL',
98
+ '.gql': 'GraphQL',
99
+ };
100
+
101
+ return descriptions[extension] || 'Unknown';
102
+ }
103
+
104
+ /**
105
+ * Test a filename against a glob pattern (supports * wildcards)
106
+ * @param {string} fileName
107
+ * @param {string} pattern - e.g. "*.min.js"
108
+ * @returns {boolean}
109
+ */
110
+ export function matchesGlobPattern(fileName, pattern) {
111
+ if (pattern === fileName) return true;
112
+
113
+ const regexPattern = pattern
114
+ .replace(/\./g, '\\.')
115
+ .replace(/\*/g, '.*');
116
+
117
+ return new RegExp(`^${regexPattern}$`, 'i').test(fileName);
118
+ }
119
+
120
+ /**
121
+ * Resolve a versioned output path: if basePath exists, returns basePath-v1,
122
+ * basePath-v2, etc. Strips any existing -vN suffix before versioning.
123
+ * @param {string} basePath - e.g. "/out/myproject_llm.md"
124
+ * @returns {string} A path that does not yet exist on disk
125
+ */
126
+ export function resolveVersionedPath(basePath) {
127
+ if (!fs.existsSync(basePath)) return basePath;
128
+
129
+ const dir = path.dirname(basePath);
130
+ const ext = path.extname(basePath);
131
+ const base = path.basename(basePath, ext).replace(/-v\d+$/, '');
132
+
133
+ let version = 1;
134
+ while (true) {
135
+ const candidate = path.join(dir, `${base}-v${version}${ext}`);
136
+ if (!fs.existsSync(candidate)) return candidate;
137
+ version++;
138
+ }
139
+ }