-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
418 lines (362 loc) · 14.3 KB
/
index.js
File metadata and controls
418 lines (362 loc) · 14.3 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
const fs = require('fs');
const path = require('path');
let Parser, Python, JavaScript;
try {
Parser = require('tree-sitter');
Python = require('tree-sitter-python');
JavaScript = require('tree-sitter-javascript');
} catch (error) {
console.error('Failed to load tree-sitter:', error.message);
console.error('Please ensure tree-sitter is installed: npm install tree-sitter tree-sitter-python tree-sitter-javascript');
process.exit(1);
}
// Add this new function to find project root
function findProjectRoot(startDir = process.cwd()) {
let currentDir = startDir;
while (currentDir !== path.parse(currentDir).root) {
if (fs.existsSync(path.join(currentDir, 'package.json'))) {
return currentDir;
}
currentDir = path.dirname(currentDir);
}
return startDir;
}
// Add the project root as a constant
const PROJECT_ROOT = findProjectRoot();
// Initialize parser and languages
const parser = new Parser();
const pythonParser = Python; // Remove the function call
const javascriptParser = JavaScript;
function shouldIncludePath(filePath, includePaths) {
if (!includePaths || includePaths.length === 0) return true;
return includePaths.some(includePath => filePath.startsWith(includePath));
}
// Modify getPackageDirectories to include includePaths
function getPackageDirectories(includePaths = []) {
const packageDirs = [];
const currentDir = PROJECT_ROOT; // Changed from process.cwd()
// Check for JavaScript package.json in root only
const packageJsonPath = path.join(currentDir, 'package.json');
if (fs.existsSync(packageJsonPath)) {
console.log('Found package.json at:', packageJsonPath);
packageDirs.push({ path: currentDir, type: 'js' });
// Look for Python packages
const pypackages = [];
const entries = fs.readdirSync(currentDir, { withFileTypes: true });
for (const entry of entries) {
// Skip dist, node_modules and dot directories
if (entry.isDirectory() && !entry.name.startsWith('.') &&
entry.name !== 'node_modules' && entry.name !== 'dist') {
const subdir = path.join(currentDir, entry.name);
// Check for JavaScript packages
if (fs.existsSync(path.join(subdir, 'package.json'))) {
console.log('Found package.json at:', path.join(subdir, 'package.json'));
packageDirs.push({ path: subdir, type: 'js' });
}
// Check for Python packages
if (fs.existsSync(path.join(subdir, 'setup.py')) || fs.existsSync(path.join(subdir, 'pyproject.toml'))) {
console.log('Found Python package at:', subdir);
pypackages.push({ path: subdir, type: 'py' });
}
}
}
packageDirs.push(...pypackages);
}
// Include additional paths provided via command line arguments
includePaths.forEach(includePath => {
const fullPath = path.join(PROJECT_ROOT, includePath);
if (fs.existsSync(fullPath)) {
const stats = fs.statSync(fullPath);
if (stats.isDirectory()) {
// Determine package type based on content
if (fs.existsSync(path.join(fullPath, 'package.json'))) {
console.log('Found package.json at:', path.join(fullPath, 'package.json'));
packageDirs.push({ path: fullPath, type: 'js' });
}
if (fs.existsSync(path.join(fullPath, 'setup.py')) || fs.existsSync(path.join(fullPath, 'pyproject.toml'))) {
console.log('Found Python package at:', fullPath);
packageDirs.push({ path: fullPath, type: 'py' });
}
}
}
});
console.log('Found packages:', packageDirs);
return packageDirs;
}
// Modify listFilesAndExports to pass includePaths to getPackageDirectories
function listFilesAndExports(dir, fileList = [], includePaths = []) {
// Get package directories including the specified includePaths
const packageDirs = getPackageDirectories(includePaths);
console.log('Scanning directory:', dir);
if (!fs.existsSync(dir)) {
console.warn('Directory does not exist:', dir);
return fileList;
}
const files = fs.readdirSync(dir);
console.log(`Found ${files.length} files/directories in ${dir}`);
for (const file of files) {
const filePath = path.join(dir, file);
const stat = fs.statSync(filePath);
// Skip node_modules and dot directories
if (file === 'node_modules' || file.startsWith('.') || file === 'dist') {
console.log('Skipping directory:', file);
continue;
}
if (stat.isDirectory()) {
listFilesAndExports(filePath, fileList, includePaths);
} else if (file.endsWith('.js') || file.endsWith('.ts') || file.endsWith('.py')) {
console.log('Processing file:', filePath);
const exports = getExports(filePath);
console.log('Exports:', exports);
if (exports && exports.length > 0) {
fileList.push({
filePath: path.relative(PROJECT_ROOT, filePath), // Changed from process.cwd()
exports
});
console.log('Added to fileList:', filePath, 'with exports:', exports);
}
}
}
console.log('Current fileList:', fileList);
return fileList;
}
function getExports(filePath) {
try {
const ext = path.extname(filePath);
let code;
try {
code = fs.readFileSync(filePath, 'utf-8');
if (!code || typeof code !== 'string') {
console.warn(`Invalid file content for ${filePath}`);
return [];
}
// Ensure code ends with newline to prevent parsing issues
if (!code.endsWith('\n')) {
code += '\n';
}
} catch (readError) {
console.warn(`Failed to read file ${filePath}: ${readError.message}`);
return [];
}
// Create a new parser instance for each file
const fileParser = new Parser();
if (ext === '.py') {
fileParser.setLanguage(pythonParser);
} else if (ext === '.js' || ext === '.ts') {
fileParser.setLanguage(javascriptParser);
} else {
console.warn(`Unsupported file extension: ${ext}`);
return [];
}
try {
const tree = fileParser.parse(code);
if (!tree || !tree.rootNode) {
console.warn(`Failed to parse ${filePath}: Invalid syntax tree`);
return [];
}
return extractExports(tree.rootNode, ext);
} catch (parseError) {
console.warn(`Failed to parse ${filePath}: ${parseError.message}`);
return [];
}
} catch (error) {
console.error(`Error processing ${filePath}: ${error.message}`);
return [];
}
}
function extractExports(rootNode, extension) {
const exports = [];
const nodesToVisit = [rootNode];
while (nodesToVisit.length > 0) {
const node = nodesToVisit.pop();
console.log('Analyzing node type:', node.type);
if (extension === '.py') {
if (node.type === 'class_definition') {
const nameNode = node.childForFieldName('name');
const className = nameNode ? nameNode.text : 'UnnamedClass';
const classExports = { name: className, methods: [] };
const suiteNode = node.childForFieldName('body');
if (suiteNode) {
suiteNode.namedChildren.forEach(child => {
if (child.type === 'function_definition') {
const methodNameNode = child.childForFieldName('name');
if (methodNameNode) {
classExports.methods.push(methodNameNode.text);
}
}
});
}
exports.push(classExports);
} else if (node.type === 'function_definition') {
const nameNode = node.childForFieldName('name');
if (nameNode) exports.push({ name: nameNode.text });
}
} else if (extension === '.js' || extension === '.ts') {
if (node.type === 'class_declaration') {
const nameNode = node.childForFieldName('name');
const className = nameNode ? nameNode.text : 'UnnamedClass';
const classExports = { name: className, methods: [] };
node.namedChildren.forEach(child => {
if (child.type === 'method_definition') {
const methodNameNode = child.childForFieldName('name');
if (methodNameNode) {
classExports.methods.push(methodNameNode.text);
}
}
});
exports.push(classExports);
} else if (node.type === 'function_declaration') {
const nameNode = node.childForFieldName('name');
if (nameNode) exports.push({ name: nameNode.text });
} else if (node.type === 'export_statement' || node.type === 'lexical_declaration' || node.type === 'variable_declaration') {
// Add handling for more export types
const declarations = node.children.filter(child =>
child.type === 'variable_declarator' ||
child.type === 'function_declaration' ||
child.type === 'identifier'
);
declarations.forEach(decl => {
const nameNode = decl.childForFieldName('name') || decl;
if (nameNode && nameNode.text) {
exports.push({ name: nameNode.text });
}
});
}
}
nodesToVisit.push(...node.children);
}
return exports;
}
function generateMarkdownReport(fileList) {
const startMarker = '<!-- BEGIN GENERATED CONTENT -->';
const endMarker = '<!-- END GENERATED CONTENT -->';
let report = '';
const packageDirs = getPackageDirectories();
if (packageDirs.length === 0) {
report += `## Package (js): \`.\`\n`;
} else {
packageDirs.forEach(packageInfo => {
const packagePath = path.relative(PROJECT_ROOT, packageInfo.path) || '.';
const packageType = packageInfo.type;
report += `## Package (${packageType}): \`${packagePath}\`\n\n`;
const packageFiles = fileList.filter(file =>
file.filePath.startsWith(packagePath) || packagePath === '.');
const dirStructure = {};
packageFiles.forEach(file => {
const relativePath = path.relative(packageInfo.path, path.join(PROJECT_ROOT, file.filePath));
const parts = relativePath.split(path.sep);
let current = dirStructure;
parts.forEach((part, index) => {
if (!current[part]) {
current[part] = index === parts.length - 1 ? { exports: file.exports } : {};
}
current = current[part];
});
});
function generateTree(dir, indent = '') {
let output = '';
for (const key in dir) {
if (dir[key].exports) {
output += `${indent}- ${key}\n`;
dir[key].exports.forEach(exp => {
if (exp.methods && exp.methods.length > 0) {
output += `${indent} - ${exp.name}\n`;
exp.methods.forEach(method => {
output += `${indent} - ${method}\n`;
});
} else {
output += `${indent} - ${exp.name}\n`;
}
});
} else {
output += `${indent}- ${key}/\n`;
output += generateTree(dir[key], indent + ' ');
}
}
return output;
}
report += generateTree(dirStructure);
report += '\n';
});
}
return `${startMarker}\n${report}${endMarker}\n`;
}
// Modify writeReportIfChanged to use PROJECT_ROOT
function writeReportIfChanged(report) {
// Always write to .github directory in project root
const reportPath = path.join(PROJECT_ROOT, '.github', 'copilot-instructions.md'); // Changed from process.cwd()
const githubDir = path.dirname(reportPath);
console.log('\nFile Write Debug:');
console.log('1. Target file:', reportPath);
if (!fs.existsSync(githubDir)) {
console.log('2. Creating .github directory');
fs.mkdirSync(githubDir, { recursive: true });
}
let currentContent = '';
try {
currentContent = fs.readFileSync(reportPath, 'utf-8');
console.log(`Reading from ${reportPath}`);
} catch (error) {
console.log(`Creating new file at ${reportPath}`);
currentContent = `# Copilot Instructions for Project Structure Workflow\n\nJust some sample instructions that should stay in place...\n\n<!-- BEGIN GENERATED CONTENT -->\n## Package (js): \`.\`\n<!-- END GENERATED CONTENT -->\n`;
}
const startMarker = '<!-- BEGIN GENERATED CONTENT -->';
const endMarker = '<!-- END GENERATED CONTENT -->';
const startIndex = currentContent.indexOf(startMarker);
const endIndex = currentContent.indexOf(endMarker);
console.log('4. Markers found:', { startIndex, endIndex });
if (startIndex !== -1 && endIndex !== -1) {
const oldContent = currentContent.substring(startIndex, endIndex + endMarker.length);
console.log('5. Content comparison:');
console.log(' Old length:', oldContent.length);
console.log(' New length:', report.length);
console.log(' Content different:', oldContent !== report);
if (oldContent !== report) {
const before = currentContent.substring(0, startIndex);
const after = currentContent.substring(endIndex + endMarker.length);
const newContent = before + report + after;
try {
console.log('6. Writing updated content to file');
fs.writeFileSync(reportPath, newContent, 'utf-8');
console.log('7. File write successful');
} catch (error) {
console.error('7. Error writing file:', error);
}
} else {
console.log('6. No changes needed');
}
} else {
console.log('5. No markers found, writing new content');
try {
const newContent = currentContent.trim() + '\n\n' + report + '\n';
fs.writeFileSync(reportPath, newContent, 'utf-8');
console.log('6. File write successful');
} catch (error) {
console.error('6. Error writing file:', error);
}
}
}
// Modify main to pass includePaths to getPackageDirectories
function main() {
try {
const pathsArg = process.argv[2];
const includePaths = pathsArg ? pathsArg.split(',').map(p => p.trim()) : [];
console.log('Scanning directories...');
const allFiles = listFilesAndExports(PROJECT_ROOT, [], includePaths); // Changed from process.cwd()
console.log(`Found ${allFiles.length} files to analyze`);
console.log('Generating markdown report...');
const markdownReport = generateMarkdownReport(allFiles);
console.log('Writing report...');
writeReportIfChanged(markdownReport);
console.log('Done!');
} catch (error) {
console.error('Error in main:', error);
process.exit(1);
}
}
// Export for CLI usage
module.exports = { main };
// Run if called directly
if (require.main === module) {
main();
}