-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
328 lines (270 loc) · 11.4 KB
/
index.js
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
const fs = require('fs').promises;
const path = require('path');
const minimist = require('minimist');
const micromatch = require('micromatch');
const ignore = require('ignore');
// Define file types and their corresponding languages for syntax highlighting
const FILE_TYPES = {
// TypeScript
'.ts': 'typescript',
'.tsx': 'typescript',
'.mts': 'typescript',
'.cts': 'typescript',
'.d.ts': 'typescript',
// JavaScript
'.js': 'javascript',
'.jsx': 'javascript',
'.mjs': 'javascript',
'.cjs': 'javascript',
// Framework-specific
'.svelte': 'svelte',
'.vue': 'vue',
'.astro': 'astro',
// Solid
'.jsx': 'javascript',
'.tsx': 'typescript',
// Style files
'.css': 'css',
'.scss': 'scss',
'.sass': 'sass',
'.less': 'less',
'.styl': 'stylus',
// Config files
'.postcss': 'css',
'.styled': 'javascript'
};
// Style-related file extensions
const STYLE_EXTENSIONS = new Set(['.css', '.scss', '.sass', '.less', '.styl', '.postcss']);
// Common type definition files
const TYPE_DEFINITION_FILES = new Set([
'app.d.ts',
'global.d.ts',
'types.d.ts',
'index.d.ts',
'environment.d.ts',
'vite-env.d.ts'
]);
// Load and parse .gitignore file
async function loadGitignore(directory) {
try {
const gitignorePath = path.join(directory, '.gitignore');
const content = await fs.readFile(gitignorePath, 'utf8');
return ignore().add(content);
} catch (error) {
// If .gitignore doesn't exist, return an ignore instance with default patterns
return ignore().add([
'node_modules',
'dist',
'build',
'.git',
'out',
'coverage',
'*.log'
].join('\n'));
}
}
async function main() {
try {
// Get command line arguments
const argv = minimist(process.argv.slice(2), {
string: ['exclude'],
boolean: ['no-styles', 'no-types', 'include-ignored'],
alias: {
e: 'exclude',
i: 'include-ignored'
}
});
const excludeStyles = argv['no-styles'];
const excludeTypes = argv['no-types'];
const includeIgnored = argv['include-ignored'];
const excludePatterns = argv.exclude ? argv.exclude.split(',') : [];
// Get the current directory
const currentDirectory = process.cwd();
const currentDirectoryName = path.basename(currentDirectory);
// Load .gitignore patterns
const ig = await loadGitignore(currentDirectory);
// Create the 'src' directory path
const srcDirectory = path.join(currentDirectory, 'src');
// Create the 'src' directory if it doesn't exist
try {
await fs.access(srcDirectory);
} catch {
await fs.mkdir(srcDirectory);
}
// Create the markdown file name based on the current directory name
const markdownFileName = `${currentDirectoryName}.md`;
const markdownFilePath = path.join(srcDirectory, markdownFileName);
// Initialize content array to store all file contents
const markdownContent = [];
// Get all files
const files = await getAllFiles(currentDirectory, Object.keys(FILE_TYPES), excludePatterns, ig, includeIgnored);
// Sort files by extension and then by path for better organization
files.sort((a, b) => {
const extA = path.extname(a);
const extB = path.extname(b);
if (extA === extB) {
return a.localeCompare(b);
}
return extA.localeCompare(extB);
});
// Process each file
for (const file of files) {
const extension = path.extname(file);
const basename = path.basename(file);
// Skip style files if --no-styles flag is present
if (excludeStyles && STYLE_EXTENSIONS.has(extension)) {
continue;
}
// Skip type definition files if --no-types flag is present
if (excludeTypes && (
extension === '.d.ts' ||
TYPE_DEFINITION_FILES.has(basename) ||
basename.endsWith('.d.ts')
)) {
continue;
}
let fileContent = await fs.readFile(file, 'utf8');
const relativePath = path.relative(currentDirectory, file);
const language = FILE_TYPES[extension] || 'plaintext';
// Process content based on flags
if (excludeStyles) {
fileContent = await removeStyles(fileContent, extension);
}
if (excludeTypes && (extension === '.ts' || extension === '.tsx')) {
fileContent = await removeTypeDefinitions(fileContent);
}
// Skip empty files after processing
if (!fileContent.trim()) {
continue;
}
// Add file content to markdown with a header showing the file type
markdownContent.push(`# ${relativePath} (${language})\n`);
markdownContent.push(`\`\`\`${language}`);
markdownContent.push(fileContent);
markdownContent.push('```\n');
}
// Write the markdown file
await fs.writeFile(markdownFilePath, markdownContent.join('\n'));
// Create custom instructions file
const customInstructionsPath = path.join(srcDirectory, 'custom_instructions.txt');
const synopsis = await getProjectSynopsis(currentDirectory, currentDirectoryName);
const frameworks = [
'TypeScript', 'JavaScript', 'React', 'Vue',
'Svelte', 'Solid', 'Astro'
].join('/');
const exclusions = [];
if (excludeStyles) exclusions.push('styles');
if (excludeTypes) exclusions.push('type definitions');
const exclusionText = exclusions.length ? ` (excluding ${exclusions.join(' and ')})` : '';
const customInstructions = [
synopsis || `[Please provide a synopsis of the ${currentDirectoryName} project.]`,
'',
`Please act as an expert ${frameworks} developer and software engineer. The attached ${markdownFileName} file contains the complete and up-to-date codebase for our application${exclusionText}. Your task is to thoroughly analyze the codebase, understand its programming flow and logic, and provide detailed insights, suggestions, and solutions to enhance the application's performance, efficiency, readability, and maintainability.`,
'',
'We highly value responses that demonstrate a deep understanding of the code. Please ensure your recommendations are thoughtful, well-analyzed, and contribute positively to the project\'s success. Your expertise is crucial in helping us improve and upgrade our application.'
].join('\n');
await fs.writeFile(customInstructionsPath, customInstructions);
// Print summary statistics
const fileStats = files.reduce((acc, file) => {
const ext = path.extname(file);
const basename = path.basename(file);
if (
(!excludeStyles || !STYLE_EXTENSIONS.has(ext)) &&
(!excludeTypes || !(ext === '.d.ts' || TYPE_DEFINITION_FILES.has(basename) || basename.endsWith('.d.ts')))
) {
acc[ext] = (acc[ext] || 0) + 1;
}
return acc;
}, {});
console.log(`All files have been compiled into ${markdownFilePath}`);
console.log(`Custom instructions have been created at ${customInstructionsPath}`);
console.log(`Styles ${excludeStyles ? 'excluded' : 'included'} in the output`);
console.log(`Type definitions ${excludeTypes ? 'excluded' : 'included'} in the output`);
console.log(`.gitignore patterns ${includeIgnored ? 'ignored' : 'respected'}`);
if (excludePatterns.length > 0) {
console.log(`Additional excluded patterns: ${excludePatterns.join(', ')}`);
}
console.log('\nFile statistics:');
Object.entries(fileStats)
.sort((a, b) => b[1] - a[1])
.forEach(([ext, count]) => {
console.log(`${ext}: ${count} files`);
});
} catch (error) {
console.error('An error occurred:', error);
process.exit(1);
}
}
async function getAllFiles(directory, extensions, excludePatterns, ig, includeIgnored) {
const files = [];
async function traverse(dir) {
const entries = await fs.readdir(dir, { withFileTypes: true });
for (const entry of entries) {
const fullPath = path.join(dir, entry.name);
const relativePath = path.relative(directory, fullPath);
// Skip files based on .gitignore unless includeIgnored is true
if (!includeIgnored && ig.ignores(relativePath)) {
continue;
}
// Check if the path matches any exclude patterns
if (excludePatterns.length > 0 && micromatch.isMatch(relativePath, excludePatterns)) {
continue;
}
if (entry.isDirectory()) {
await traverse(fullPath);
} else if (entry.isFile() && extensions.includes(path.extname(fullPath))) {
files.push(fullPath);
}
}
}
await traverse(directory);
return files;
}
async function removeStyles(content, extension) {
switch (extension) {
case '.vue':
case '.svelte':
case '.astro':
// Remove <style> blocks from component files
return content.replace(/<style(\s[^>]*)?>[^]*?<\/style>/gi, '');
default:
return content;
}
}
async function removeTypeDefinitions(content) {
// Remove interface definitions
content = content.replace(/^interface\s+\w+\s*{[^}]*}/gm, '');
// Remove type aliases
content = content.replace(/^type\s+\w+\s*=\s*[^;]+;/gm, '');
// Remove standalone type declarations
content = content.replace(/^declare\s+[^;]+;/gm, '');
// Remove enum definitions
content = content.replace(/^enum\s+\w+\s*{[^}]*}/gm, '');
// Remove namespace declarations
content = content.replace(/^namespace\s+\w+\s*{[^}]*}/gm, '');
// Remove type annotations from variables and parameters
content = content.replace(/:\s*([^=,\n\r{}]+)(?=[,)\n\r{])/g, '');
// Clean up multiple empty lines
content = content.replace(/\n\s*\n\s*\n/g, '\n\n');
return content;
}
async function getProjectSynopsis(currentDirectory, currentDirectoryName) {
try {
const readmePath = path.join(currentDirectory, 'README.md');
const readmeContent = await fs.readFile(readmePath, 'utf8');
// Extract the first non-header, non-empty line as the synopsis
const lines = readmeContent.split(/\r?\n/);
for (const line of lines) {
const trimmedLine = line.trim();
if (!trimmedLine) continue;
if (trimmedLine.startsWith('#')) continue;
if (trimmedLine.startsWith('```')) continue;
// Found a valid line for synopsis
return trimmedLine;
}
} catch {
return '';
}
}
// Run the script
main();