-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
executable file
·567 lines (481 loc) · 15.7 KB
/
index.js
File metadata and controls
executable file
·567 lines (481 loc) · 15.7 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
#!/usr/bin/env node
const { program } = require('commander');
const inquirer = require('inquirer');
const chalk = require('chalk');
const fs = require('fs');
const path = require('path');
const { execSync } = require('child_process');
const packageJson = require('./package.json');
// Configuration constants
const CONFIG = {
// Default directories to exclude
COMMON_EXCLUDE_DIRS: [
'node_modules',
'.git',
'vendor',
'.next',
'dist',
'build',
'.husky',
'public',
'docs',
'assets/fonts',
'assets/images',
'assets/svg',
'src/assets/images',
'assets/icons',
'languages',
'images',
'tests',
],
// Default file patterns to exclude
COMMON_EXCLUDE_FILES: [
'package-lock.json',
'composer.lock',
'yarn.lock',
'LICENSE.md',
'*.min.js',
'*.min.css',
'*.jpg',
'*.png',
'*.svg',
'*.webp',
'*.csv',
]
};
// Utility functions
const utils = {
// Get current directory name for default output file
getCurrentDirectoryName() {
const currentPath = process.cwd();
return path.basename(currentPath) + '.md';
},
// Format file sizes for display
formatSize(bytes) {
const units = ['B', 'KB', 'MB', 'GB'];
let size = bytes;
let unitIndex = 0;
while (size >= 1024 && unitIndex < units.length - 1) {
size /= 1024;
unitIndex++;
}
return `${size.toFixed(2)} ${units[unitIndex]}`;
},
// Calculate directory size recursively
getDirSize(dirPath) {
let totalSize = 0;
try {
const entries = fs.readdirSync(dirPath);
for (const entry of entries) {
const entryPath = path.join(dirPath, entry);
try {
const stats = fs.statSync(entryPath);
if (stats.isDirectory()) {
totalSize += this.getDirSize(entryPath);
} else {
totalSize += stats.size;
}
} catch (err) {
// Skip files/directories we can't access
}
}
} catch (err) {
// Skip directories we can't read
}
return totalSize;
},
// Pattern matching for file and directory paths
isPathExcluded(itemPath, pattern, isDirectory = false) {
// For directory patterns with '/**'
if (pattern.endsWith('/**')) {
const dirName = pattern.slice(0, -3);
// For top-level directories
if (!dirName.includes('/')) {
return itemPath === dirName || itemPath.startsWith(dirName + '/');
}
// For path-specified directories
else {
return itemPath === dirName || itemPath.startsWith(dirName + '/');
}
}
// For exact file matches (no wildcards)
else if (!pattern.includes('*')) {
return itemPath === pattern;
}
// For other wildcard patterns
else {
const regex = new RegExp('^' + pattern.replace(/\*\*/g, '.*').replace(/\*/g, '[^/]*') + '$');
return regex.test(itemPath);
}
},
// Check if item is excluded by any pattern in the list
isExcluded(itemPath, excludePatterns, isDirectory = false) {
return excludePatterns.some(pattern =>
this.isPathExcluded(itemPath, pattern, isDirectory));
},
// Log with color
log: {
info: (msg) => console.log(chalk.blue(msg)),
success: (msg) => console.log(chalk.green(msg)),
warning: (msg) => console.log(chalk.yellow(msg)),
error: (msg) => console.error(chalk.red(msg))
}
};
// Set up command line options
function setupCommandLine() {
program
.name('code2prompt-manager')
.description('A CLI tool to manage code2prompt file size limits')
.version(packageJson.version)
.option('-l, --limit <size>', 'Size limit for the generated MD file in KB',
(value) => parseInt(value, 10), 400)
.option('-d, --directory <path>', 'Directory to scan', '.')
.option('-e, --extra-exclude <patterns>', 'Additional exclude patterns (comma-separated)')
.option('-i, --include <patterns>', 'Include patterns (comma-separated)')
.option('-O, --output-file <file>', 'Output file name', utils.getCurrentDirectoryName())
.option('-F, --output-format <format>', 'Output format: markdown, json, or xml', 'markdown')
.option('--include-priority', 'Include files in case of conflict between include and exclude patterns')
.option('--full-directory-tree', 'List the full directory tree')
.option('-c, --encoding <encoding>', 'Optional tokenizer to use for token count (cl100k, p50k, etc.)')
.option('--line-numbers', 'Add line numbers to the source code')
.option('-n, --no-execute', 'Only show the command, don\'t execute it')
.option('--auto-exclude', 'Automatically exclude files to stay under size limit', false);
program.parse(process.argv);
return program.opts();
}
// Prepare exclude directories list
function getExcludeDirs(options) {
// Default directories to skip during scanning
const skipDirs = [...CONFIG.COMMON_EXCLUDE_DIRS];
// Add extra exclude directories from command line
if (options.extraExclude) {
options.extraExclude.split(',').forEach(pattern => {
pattern = pattern.trim();
// For directory patterns with '/**'
if (pattern.endsWith('/**')) {
const dirName = pattern.slice(0, -3);
if (!skipDirs.includes(dirName)) {
skipDirs.push(dirName);
}
}
// For simple directory names without wildcards
else if (!pattern.includes('*')) {
if (!skipDirs.includes(pattern)) {
skipDirs.push(pattern);
}
}
});
}
return skipDirs;
}
// Get files and directories recursively
function scanDirectory(rootDir, skipDirs) {
const items = [];
function scan(dir, baseDir = '') {
try {
const entries = fs.readdirSync(dir);
for (const entry of entries) {
const fullPath = path.join(dir, entry);
const relativePath = path.join(baseDir, entry);
try {
const stats = fs.statSync(fullPath);
if (stats.isDirectory()) {
// Check if this directory should be skipped
const shouldSkip = skipDirs.some(skipPath => {
// For top-level directories (e.g., "styles")
if (!skipPath.includes('/')) {
return entry === skipPath && baseDir === '';
}
// For path-specified directories (e.g., "components/annual-report-2022/styles")
else {
return relativePath === skipPath;
}
});
if (shouldSkip) {
items.push({
path: relativePath,
isDirectory: true,
size: 0,
prettySize: utils.formatSize(0)
});
continue;
}
// Add the directory
const dirSize = utils.getDirSize(fullPath);
items.push({
path: relativePath,
isDirectory: true,
size: dirSize,
prettySize: utils.formatSize(dirSize)
});
// Scan subdirectory
scan(fullPath, relativePath);
} else {
// Add the file
items.push({
path: relativePath,
isDirectory: false,
size: stats.size,
prettySize: utils.formatSize(stats.size)
});
}
} catch (err) {
utils.log.warning(`Warning: Could not access ${fullPath}: ${err.message}`);
}
}
} catch (err) {
utils.log.warning(`Warning: Could not read directory ${dir}: ${err.message}`);
}
}
// Start scanning from the root directory
scan(rootDir);
// Sort items by size (largest first)
return items.sort((a, b) => b.size - a.size);
}
// Prepare exclude patterns
function prepareExcludePatterns(options) {
// Default excludes
const defaultExcludes = [
// Add directory patterns with /**
...CONFIG.COMMON_EXCLUDE_DIRS.map(dir => `${dir}/**`),
// Add file patterns
...CONFIG.COMMON_EXCLUDE_FILES
];
// Add extra exclude patterns from command line
const extraExcludes = [];
if (options.extraExclude) {
options.extraExclude.split(',').forEach(pattern => {
pattern = pattern.trim();
if (pattern) {
// Add '/**' suffix for directory patterns that don't have wildcards
if (!pattern.includes('*') &&
fs.existsSync(path.join(options.directory, pattern)) &&
fs.statSync(path.join(options.directory, pattern)).isDirectory()) {
extraExcludes.push(pattern + '/**');
} else {
extraExcludes.push(pattern);
}
}
});
}
return { defaultExcludes, extraExcludes, allExcludes: [...defaultExcludes, ...extraExcludes] };
}
// Estimate the final size after excluding files
function estimateFinalSize(items, excludePatterns) {
let totalSize = 0;
// Sum sizes of all files that are not excluded
items.forEach(item => {
if (!item.isDirectory && !utils.isExcluded(item.path, excludePatterns)) {
totalSize += item.size;
}
});
// Add some overhead for markdown formatting
const markdownOverhead = Math.min(items.length * 100, 50 * 1024); // ~100 bytes per file, max 50KB
return totalSize + markdownOverhead;
}
// Create choices for selection UI
function createSelectionChoices(items, allExcludes) {
// Create file choices
const fileChoices = items
.filter(item => !item.isDirectory) // Only include files in the choices
.sort((a, b) => b.size - a.size)
.map(item => {
const sizeStr = item.prettySize.padStart(10);
const pathStr = item.path;
return {
name: `${sizeStr} │ ${pathStr}`,
value: item.path,
short: item.path,
size: item.size, // Store size for auto-exclude feature
checked: utils.isExcluded(item.path, allExcludes)
};
});
// Create directory choices
const directoryChoices = items
.filter(item => item.isDirectory)
.sort((a, b) => b.size - a.size)
.map(item => {
const sizeStr = item.prettySize.padStart(10);
const pathStr = item.path + '/';
return {
name: `${sizeStr} │ ${pathStr}`,
value: `${item.path}/**`,
short: item.path,
checked: utils.isExcluded(item.path, allExcludes, true)
};
});
return {
fileChoices,
directoryChoices,
allChoices: [
new inquirer.Separator(' === Files (sorted by size) === '),
...fileChoices,
new inquirer.Separator(' === Directories === '),
...directoryChoices
]
};
}
// Auto-exclude large files to stay under the size limit
function autoExcludeFiles(choices, initialSize, sizeLimit, autoExclude) {
const autoExcluded = [];
if (autoExclude && initialSize > sizeLimit) {
let remainingSize = initialSize;
const targetSize = sizeLimit * 0.95; // Target 95% of limit to allow some margin
// Sort files by size (largest first) that aren't already excluded
const filesToConsider = [...choices]
.filter(choice => !choice.checked)
.sort((a, b) => b.size - a.size);
for (const file of filesToConsider) {
if (remainingSize > targetSize) {
file.checked = true;
autoExcluded.push(file.value);
remainingSize -= file.size;
} else {
break;
}
}
if (autoExcluded.length > 0) {
utils.log.warning(`\nAuto-excluded ${autoExcluded.length} files to meet size limit:`);
autoExcluded.forEach(file => {
utils.log.warning(` - ${file}`);
});
utils.log.success(`New estimated size: ${utils.formatSize(remainingSize)}`);
}
}
return autoExcluded;
}
// Build the code2prompt command
function buildCommand(options, allExcludes) {
let cmd = `code2prompt`;
// Add options that match code2prompt's format
if (options.outputFile) {
cmd += ` -O "${options.outputFile}"`;
}
if (options.outputFormat && options.outputFormat !== 'markdown') {
cmd += ` -F ${options.outputFormat}`;
}
// Add boolean flags
const booleanFlags = {
includePriority: '--include-priority',
fullDirectoryTree: '--full-directory-tree',
lineNumbers: '--line-numbers'
};
// Add each enabled boolean flag
Object.entries(booleanFlags).forEach(([option, flag]) => {
if (options[option]) cmd += ` ${flag}`;
});
// Add encoding option if present
if (options.encoding) {
cmd += ` -c ${options.encoding}`;
}
// Add exclude options - use a single -e flag with comma-separated patterns
if (allExcludes.length > 0) {
cmd += ` -e "${allExcludes.join(',')}"`;
}
// Add include options - use a single -i flag with comma-separated patterns
if (options.include) {
const includePatterns = options.include.split(',')
.map(pattern => pattern.trim())
.filter(pattern => pattern);
if (includePatterns.length > 0) {
cmd += ` -i "${includePatterns.join(',')}"`;
}
}
// Add the directory path at the end
cmd += ` "${options.directory}"`;
return cmd;
}
// Execute the generated command
function executeCommand(cmd, execute) {
if (execute) {
utils.log.info('\nExecuting command...');
try {
execSync(cmd, { stdio: 'inherit' });
utils.log.success('\nCommand executed successfully!');
} catch (error) {
utils.log.error('\nError executing command: ' + error.message);
}
}
}
// Process any unselected auto-excluded files
function processUnselectedAutoExcludes(autoExcluded, selectedExcludes) {
const unselectedAutoExcludes = autoExcluded.filter(item => !selectedExcludes.includes(item));
if (unselectedAutoExcludes.length > 0) {
utils.log.warning(`You un-selected ${unselectedAutoExcludes.length} auto-excluded files that will be INCLUDED in the output:`);
unselectedAutoExcludes.forEach(file => {
utils.log.warning(` + ${file}`);
});
}
}
// Display size information
function displaySizeInfo(size, limit, message) {
const formattedSize = utils.formatSize(size);
if (size > limit) {
const exceedMessage = `(exceeds limit by ${utils.formatSize(size - limit)})`;
utils.log.info(`\n${message}: ${formattedSize} ${chalk.red(exceedMessage)}`);
} else {
utils.log.info(`\n${message}: ${formattedSize} ${chalk.green('(within limit)')}`);
}
}
// Main function
async function main() {
try {
// Set up command line options
const options = setupCommandLine();
utils.log.info(`Scanning directory "${options.directory}" for files...`);
utils.log.warning('This may take a while for large codebases...');
// Prepare exclude directories
const skipDirs = getExcludeDirs(options);
// Scan the directory
const items = scanDirectory(options.directory, skipDirs);
utils.log.success(`Found ${items.length} files and directories.`);
// Prepare exclude patterns
const { defaultExcludes, extraExcludes, allExcludes } = prepareExcludePatterns(options);
// Calculate initial size with just default excludes
const initialSize = estimateFinalSize(items, allExcludes);
const sizeLimit = options.limit * 1024; // Convert KB to bytes
utils.log.info(`\nSize limit: ${utils.formatSize(sizeLimit)} (${options.limit} KB)`);
utils.log.info(`Estimated size with default excludes: ${utils.formatSize(initialSize)}`);
if (initialSize > sizeLimit) {
utils.log.warning(`\nWARNING: Current selection exceeds size limit by ${utils.formatSize(initialSize - sizeLimit)}`);
}
// Create choices for selection UI
const { fileChoices, directoryChoices, allChoices } = createSelectionChoices(items, allExcludes);
// Auto-exclude large files if needed and requested
const autoExcluded = autoExcludeFiles(fileChoices, initialSize, sizeLimit, options.autoExclude);
// Show selection UI
const { selectedExcludes } = await inquirer.prompt([
{
type: 'checkbox',
name: 'selectedExcludes',
message: 'Select files/directories to EXCLUDE (sorted by size - Space to toggle, Enter to confirm):',
choices: allChoices,
pageSize: 20
}
]);
utils.log.info(`\nFinal user selection: ${selectedExcludes.length} items`);
// Process any unselected auto-excluded files
processUnselectedAutoExcludes(autoExcluded, selectedExcludes);
// The final list should contain default excludes plus user's selection
const finalExcludes = [...defaultExcludes, ...extraExcludes, ...selectedExcludes];
const finalSize = estimateFinalSize(items, finalExcludes);
// Display final size information
displaySizeInfo(finalSize, sizeLimit, "Final estimated size");
// Combine default and selected excludes, removing duplicates
const dedupedExcludes = Array.from(new Set(finalExcludes));
// Create the code2prompt command
const cmd = buildCommand(options, dedupedExcludes);
console.log('\n' + chalk.green('Generated code2prompt command:'));
console.log(chalk.yellow(cmd));
// Execute the command if requested
executeCommand(cmd, options.execute);
} catch (error) {
utils.log.error('Error: ' + error.message);
process.exit(1);
}
}
// Run the main function
main().catch(err => {
utils.log.error('Error: ' + err.message);
process.exit(1);
});