-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcodeSummary.mjs
More file actions
730 lines (630 loc) · 20.4 KB
/
codeSummary.mjs
File metadata and controls
730 lines (630 loc) · 20.4 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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
#!/usr/bin/env node
/**
* codeSummary.mjs
*
* This script generates a directory tree, collects selected code files,
* sends the directory structure and code files to an LLM for analysis,
* and compiles everything into a single Markdown file.
* The final report includes a table of contents, directory structure,
* LLM analysis, and code files. It also allows selective analysis based on
* selected files or directories and includes a timestamp of when the report was generated.
* Additionally, it manages the output directory and ensures it's excluded from git tracking.
*/
import fs from 'fs/promises';
import path from 'path';
import axios from 'axios';
import dotenv from 'dotenv';
import { fileURLToPath } from 'url';
import { checkbox } from '@inquirer/prompts';
import { minimatch } from 'minimatch';
// Load environment variables
dotenv.config();
// Configuration
const CONFIG = {
outputDir: 'codeSummaryLogs',
excludedDirs: [
'.git',
'node_modules',
'dist',
'build',
'coverage',
'logs',
'tmp',
'.vscode',
'.svelte-kit',
'outlines',
'codeSummaryLogs' // Ensure the output directory is excluded
],
excludedFiles: [
'.DS_Store',
'Thumbs.db',
'package-lock.json',
'yarn.lock',
'pnpm-lock.yaml',
'.env',
'.gitignore',
'*.config.js',
'*codesummary*'
],
api: {
url: 'https://openrouter.ai/api/v1/chat/completions',
key: process.env.OPENROUTER_API_KEY,
siteUrl: process.env.YOUR_SITE_URL || '',
siteName: process.env.YOUR_SITE_NAME || ''
}
};
// Validate API Key
if (!CONFIG.api.key) {
console.error('Error: OPENROUTER_API_KEY is not set in the .env file.');
process.exit(1);
}
// Utility functions
/**
* Get current directory for ES modules
*/
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
/**
* Format current date and time
*/
const getFormattedDate = () => {
const now = new Date();
return now.toISOString().replace(/[:.]/g, '-').slice(0, 19);
};
/**
* Ensure the output directory exists
*/
const ensureOutputDirectory = async () => {
const dirPath = path.resolve(process.cwd(), CONFIG.outputDir);
try {
await fs.mkdir(dirPath, { recursive: true });
console.log(`Output directory ready: ${CONFIG.outputDir}`);
} catch (err) {
console.error('Failed to create output directory:', err);
process.exit(1);
}
};
/**
* Display usage instructions
*/
const displayHelp = () => {
const helpText = `
Usage: node codeSummary.mjs [options]
Options:
--target, -t <path> Specify files or directories to analyze. You can provide multiple targets by repeating the flag or separating them with commas.
--interactive, -i Launch interactive mode to select files/directories.
--help, -h Display this help message.
Examples:
Analyze the entire project (default):
node codeSummary.mjs
Analyze specific directories:
node codeSummary.mjs --target src/components --target src/utils
Analyze specific files and directories:
node codeSummary.mjs -t src/components,src/utils/helpers.ts,README.md
Launch interactive selection:
node codeSummary.mjs --interactive
`;
console.log(helpText);
};
/**
* Traverse the directory and collect files with depth information
*
* @param {string} dir - Directory path
* @param {number} depth - Current depth level
* @returns {Array} - List of file and directory objects with name, path, isDirectory, and depth
*/
const traverseDirectory = async (dir, depth = 0) => {
let results = [];
let items;
try {
items = await fs.readdir(dir, { withFileTypes: true });
} catch (err) {
console.error(`Error reading directory ${dir}:`, err.message);
return results;
}
for (const item of items) {
if (isExcluded(item.name, item.isDirectory())) {
continue;
}
const fullPath = path.join(dir, item.name);
const relativePath = path.relative(process.cwd(), fullPath).split(path.sep).join('/');
results.push({
name: item.name,
path: relativePath,
isDirectory: item.isDirectory(),
depth: depth
});
if (item.isDirectory()) {
const nested = await traverseDirectory(fullPath, depth + 1);
results = results.concat(nested);
}
}
return results;
};
/**
* Check if a file or directory should be excluded
*
* @param {string} name - The file or directory name
* @param {boolean} isDir - Is the item a directory
* @returns {boolean} - True if excluded, false otherwise
*/
const isExcluded = (name, isDir) => {
if (isDir) {
return CONFIG.excludedDirs.includes(name) || name.toLowerCase().includes('codesummary');
} else {
for (const pattern of CONFIG.excludedFiles) {
if (minimatch(name, pattern, { nocase: true })) {
return true;
}
}
return false;
}
};
/**
* Format choices with indentation based on depth
*
* @param {Array} items - List of file and directory objects
* @returns {Array} - List of formatted choices for inquirer
*/
const formatChoices = (items) => {
return items.map(item => ({
name: `${' '.repeat(item.depth)}${item.isDirectory ? '📁' : '📄'} ${item.name}`,
value: item.path,
checked: false
}));
};
/**
* Launch interactive selection using inquirer
*
* @param {Array} allItems - Complete list of all file and directory objects
* @returns {Array} - List of selected file/directory paths
*/
const launchInteractiveSelection = async (allItems) => {
console.log('Launching interactive selection mode...');
const choices = formatChoices(allItems);
try {
const selected = await checkbox({
message: 'Select files and directories to include in the analysis:',
choices,
validate: (answer) => answer.length > 0 || 'You must select at least one item.'
});
return selected;
} catch (err) {
console.error('Selection error:', err.message);
process.exit(1);
}
};
/**
* Process selected targets and include all nested files if a directory is selected
*
* @param {Array} selected - List of selected file and directory paths
* @param {Array} allItems - Complete list of all file and directory objects
* @returns {Array} - Final list of file paths to process
*/
const processSelections = (selected, allItems) => {
let filesToProcess = [];
// Create a map for quick lookup
const itemsMap = new Map();
allItems.forEach(item => {
itemsMap.set(item.path, item);
});
selected.forEach(selectedPath => {
const item = itemsMap.get(selectedPath);
if (item) {
if (item.isDirectory) {
// Include all files within the directory
const nestedFiles = allItems.filter(child => child.path.startsWith(`${selectedPath}/`) && !child.isDirectory);
filesToProcess = filesToProcess.concat(nestedFiles.map(file => file.path));
} else {
filesToProcess.push(selectedPath);
}
}
});
// Remove duplicates
filesToProcess = [...new Set(filesToProcess)];
return filesToProcess;
};
/**
* Read file content with size limitation
*
* @param {string} filePath - The path of the file to read
* @param {number} maxSize - Maximum allowed file size in bytes
* @returns {string|null} - The file content or null if exceeds size limit
*/
const readFileContent = async (filePath, maxSize = 1_000_000) => {
try {
const stats = await fs.stat(filePath);
if (stats.size > maxSize) {
console.warn(`Skipping ${filePath} (exceeds size limit of ${maxSize} bytes).`);
return null;
}
const content = await fs.readFile(filePath, 'utf8');
return content;
} catch (err) {
console.error(`Error reading file ${filePath}:`, err.message);
return null;
}
};
/**
* Collect files from selected targets
*
* @param {Array} targets - List of file/directory paths
* @returns {Array} - List of file paths
*/
const collectFiles = async (targets) => {
let filesList = [];
for (const target of targets) {
const resolvedPath = path.resolve(process.cwd(), target);
try {
const stats = await fs.stat(resolvedPath);
if (stats.isDirectory()) {
const nestedFiles = await getAllFiles(resolvedPath);
filesList = filesList.concat(nestedFiles);
} else if (stats.isFile()) {
filesList.push(target);
}
} catch (err) {
console.warn(`Warning: Target path "${target}" does not exist or is inaccessible. Skipping.`);
continue;
}
}
return filesList;
};
/**
* Recursively get all files in a directory
*
* @param {string} dir - Directory path
* @returns {Array} - List of file paths
*/
const getAllFiles = async (dir) => {
let filesList = [];
let items;
try {
items = await fs.readdir(dir, { withFileTypes: true });
} catch (err) {
console.error(`Error reading directory ${dir}:`, err.message);
return filesList;
}
for (const item of items) {
if (isExcluded(item.name, item.isDirectory())) {
continue;
}
const fullPath = path.join(dir, item.name);
const relativePath = path.relative(process.cwd(), fullPath).split(path.sep).join('/');
if (item.isDirectory()) {
const nestedFiles = await getAllFiles(fullPath);
filesList = filesList.concat(nestedFiles);
} else {
filesList.push(relativePath);
}
}
return filesList;
};
/**
* Send data to OpenRouter API
*
* @param {string} prompt - The prompt to send to the LLM
* @returns {string} - The LLM's response
*/
const sendToOpenRouter = async (prompt) => {
try {
const response = await axios.post(
CONFIG.api.url,
{
model: 'anthropic/claude-3.5-sonnet:beta',
messages: [{ role: 'user', content: prompt }]
},
{
headers: {
Authorization: `Bearer ${CONFIG.api.key}`,
'HTTP-Referer': CONFIG.api.siteUrl,
'X-Title': CONFIG.api.siteName,
'Content-Type': 'application/json'
}
}
);
if (response.data?.choices?.[0]?.message?.content) {
return response.data.choices[0].message.content.trim();
} else {
console.warn('No response content from OpenRouter API.');
return '';
}
} catch (error) {
console.error('Error communicating with OpenRouter API:', error.message);
return '';
}
};
/**
* Analyze the codebase using LLM
*
* @param {string} directoryTree - The directory tree as a string
* @param {Object} fileData - An object containing file paths and their content
* @returns {string} - The analysis content
*/
const analyzeCodebase = async (directoryTree, fileData) => {
console.log('Analyzing codebase with LLM...');
if (Object.keys(fileData).length === 0) {
console.error('No files available for analysis.');
return '';
}
// Prepare prompt for LLM with an example
const prompt = `
I have a codebase with the following directory structure:
\`\`\`
${directoryTree.trim()}
\`\`\`
Below are the files and their contents:
${Object.entries(fileData)
.map(([file, data]) => {
const language = path.extname(file).substring(1) || 'plaintext';
const contentSection = `### ${file}
\`\`\`${language}
${data.content}
\`\`\``;
return contentSection;
})
.join('\n\n')}
**Your Task:**
For each file provided:
1. **Explain in detail** what the code file does.
2. **Describe** how it interacts with other files that it is importing.
3. **Output a mermaid diagram** representing the interactions for each file. Use appropriate mermaid syntax (e.g., \`graph TD\`).
Please format your response for each file as follows:
### [File Path]
**Explanation:**
[Your detailed explanation here.]
**Interactions:**
[Description of how this file interacts with other files.]
**Mermaid Diagram:**
\`\`\`mermaid
[Your mermaid diagram code here.]
\`\`\`
Avoid including unnecessary code snippets in your explanations. Be clear and concise.
`;
// Send prompt to OpenRouter
const analysis = await sendToOpenRouter(prompt);
if (analysis) {
return analysis;
} else {
console.error('Error: No analysis was received from OpenRouter.');
return '';
}
};
/**
* Generate a table of contents based on the sections
*
* @param {Array} sections - List of section names
* @returns {string} - The table of contents in Markdown format
*/
const generateTableOfContents = (sections) => {
let toc = '# Table of Contents\n\n';
sections.forEach(section => {
const anchor = section.toLowerCase().replace(/\s+/g, '-');
toc += `- [${section}](#${anchor})\n`;
});
toc += '\n';
return toc;
};
/**
* Add a folder to .gitignore
*
* @param {string} folderName - The folder to add to .gitignore
*/
const addFolderToGitignore = async (folderName) => {
const gitignorePath = path.resolve(process.cwd(), '.gitignore');
const folderEntry = `${folderName}/`;
try {
let gitignoreContent = '';
if (await fileExists(gitignorePath)) {
gitignoreContent = await fs.readFile(gitignorePath, 'utf8');
}
const lines = gitignoreContent.split(/\r?\n/);
if (!lines.includes(folderEntry)) {
await fs.appendFile(gitignorePath, `\n${folderEntry}\n`);
console.log(`Added "${folderEntry}" to .gitignore.`);
} else {
console.log(`"${folderEntry}" is already present in .gitignore.`);
}
} catch (err) {
console.error(`Error updating .gitignore: ${err.message}`);
}
};
/**
* Check if a file exists
*
* @param {string} filePath - Path to the file
* @returns {boolean} - True if exists, false otherwise
*/
const fileExists = async (filePath) => {
try {
await fs.access(filePath);
return true;
} catch {
return false;
}
};
// Main execution
const main = async () => {
try {
// Parse command-line arguments for targets
const args = process.argv.slice(2);
let targets = [];
let interactive = false;
for (let i = 0; i < args.length; i++) {
if (args[i] === '--target' || args[i] === '-t') {
const target = args[i + 1];
if (target) {
targets.push(...target.split(',').map(t => t.trim()));
i++; // Skip next argument as it's part of this flag
}
} else if (args[i].startsWith('--target=')) {
const target = args[i].split('=')[1];
if (target) {
targets.push(...target.split(',').map(t => t.trim()));
}
} else if (args[i] === '--interactive' || args[i] === '-i') {
interactive = true;
} else if (args[i] === '--help' || args[i] === '-h') {
displayHelp();
process.exit(0);
}
}
if (interactive) {
console.log('Launching interactive selection mode...');
} else if (targets.length === 0) {
console.log('No targets specified. Launching interactive selection mode...');
interactive = true;
}
// Traverse the directory and collect all selectable items
console.log('Scanning directories...');
const allItems = await traverseDirectory(process.cwd());
let selectedTargets = [];
if (interactive) {
selectedTargets = await launchInteractiveSelection(allItems);
} else {
selectedTargets = targets;
}
// Ensure the output directory exists
await ensureOutputDirectory();
// Add the output directory to .gitignore
await addFolderToGitignore(CONFIG.outputDir);
// Determine files to process
let filesToProcess = [];
if (selectedTargets.length > 0) {
console.log('Selected targets for analysis:', selectedTargets);
filesToProcess = processSelections(selectedTargets, allItems);
if (filesToProcess.length === 0) {
console.error('No valid files found for the specified targets. Exiting.');
process.exit(1);
}
} else {
console.log('Analyzing the entire project.');
filesToProcess = await collectFiles(['.']);
}
// Initialize the output file
const timestamp = getFormattedDate();
const outputFile = path.join(CONFIG.outputDir, `CodeAnalysis_${timestamp}.md`);
await fs.writeFile(outputFile, '', 'utf8');
console.log(`Initialized ${outputFile}`);
const sections = {};
// Generate directory tree based on selected targets
console.log('Generating directory tree...');
let directoryTree = '';
if (selectedTargets.length === 1 && await isDirectory(selectedTargets[0])) {
directoryTree = await generateDirectoryTree(path.resolve(process.cwd(), selectedTargets[0]));
} else {
// Multiple roots or single file
for (const target of selectedTargets) {
const resolvedPath = path.resolve(process.cwd(), target);
if (await isDirectory(resolvedPath)) {
directoryTree += await generateDirectoryTree(resolvedPath);
} else {
const fileName = path.basename(resolvedPath);
directoryTree += `${fileName}\n`;
}
}
}
sections['Directory'] = '```\n' + directoryTree + '\n```';
console.log('Generated directory tree.');
// Collect files and their contents
console.log('Collecting files and their contents...');
const fileData = {};
for (const filePath of filesToProcess) {
const relativePath = path.relative(process.cwd(), path.resolve(process.cwd(), filePath)).split(path.sep).join('/');
const content = await readFileContent(filePath);
if (content !== null) {
fileData[relativePath] = { content };
}
}
// Analyze codebase using LLM
console.log('Analyzing codebase...');
const analysis = await analyzeCodebase(directoryTree, fileData);
sections['Analysis'] = analysis || 'No analysis was generated.';
console.log('Completed analysis.');
// Prepare Code Files section
console.log('Preparing Code Files section...');
let codeFilesContent = '';
for (const [relativePath, data] of Object.entries(fileData)) {
codeFilesContent += `## ${relativePath}\n\n`;
const fileExtension = path.extname(relativePath).substring(1) || 'plaintext';
codeFilesContent += '```' + fileExtension + '\n' + data.content + '\n```\n\n';
}
sections['Code Files'] = codeFilesContent;
console.log('Prepared Code Files section.');
// Add timestamp to the report
const generationTime = new Date();
const formattedGenerationTime = generationTime.toLocaleString();
sections['Report Generated On'] = `*Generated on ${formattedGenerationTime}*\n`;
console.log('Added timestamp to the report.');
// Generate table of contents
console.log('Generating table of contents...');
const toc = generateTableOfContents(Object.keys(sections));
console.log('Generated table of contents.');
// Write all content to the output file
console.log('Writing content to the output file...');
let finalContent = toc;
for (const sectionName of ['Report Generated On', 'Directory', 'Analysis', 'Code Files']) {
finalContent += `# ${sectionName}\n\n`;
finalContent += sections[sectionName];
finalContent += '\n\n';
}
await fs.writeFile(outputFile, finalContent, 'utf8');
console.log(`File '${outputFile}' has been successfully created.`);
} catch (err) {
console.error(`An error occurred: ${err.message}`);
}
};
/**
* Check if a path is a directory
*
* @param {string} target - Path to check
* @returns {boolean} - True if directory, false otherwise
*/
const isDirectory = async (target) => {
try {
const stats = await fs.stat(target);
return stats.isDirectory();
} catch {
return false;
}
};
/**
* Generate directory tree using improved ASCII characters
*
* @param {string} dir - The directory path to start from
* @param {string} prefix - The prefix for the current level
* @returns {string} - The formatted directory tree as a string
*/
const generateDirectoryTree = async (dir, prefix = '') => {
let tree = '';
let items;
try {
items = await fs.readdir(dir, { withFileTypes: true });
} catch (err) {
console.error(`Error reading directory ${dir}:`, err.message);
return tree;
}
// Filter and sort items
items = items.filter(item => !isExcluded(item.name, item.isDirectory()));
items.sort((a, b) => {
if (a.isDirectory() && !b.isDirectory()) return -1;
if (!a.isDirectory() && b.isDirectory()) return 1;
return a.name.localeCompare(b.name);
});
for (let i = 0; i < items.length; i++) {
const item = items[i];
const isLast = i === items.length - 1;
const connector = isLast ? '└── ' : '├── ';
tree += `${prefix}${connector}${item.name}\n`;
if (item.isDirectory()) {
const newPrefix = prefix + (isLast ? ' ' : '│ ');
tree += await generateDirectoryTree(path.join(dir, item.name), newPrefix);
}
}
return tree;
};
// Execute the main function
main().catch(err => {
console.error(`An unexpected error occurred: ${err.message}`);
process.exit(1);
});