-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcollectcode.js
More file actions
143 lines (122 loc) · 3.54 KB
/
collectcode.js
File metadata and controls
143 lines (122 loc) · 3.54 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
import fs from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';
const DEFAULT_CONFIG = {
extensions: ['.ts', '.tsx', '.js', '.jsx', '.json', '.css', '.sql'],
excludeDirs: [
'node_modules',
'.git',
'dist',
'.next',
'coverage',
'tests',
'scripts',
'components/ui',
],
excludeFiles: [
'.test.',
'.spec.',
'.d.ts',
'.map',
'next-env.d.ts',
'.gitignore',
'.eslintrc.json',
'.env.example',
'components.json',
'package-lock.json',
],
excludePaths: [
'lib/supabase.ts',
'lib/rate-limit.ts',
'lib/monitoring.ts',
'lib/cache.ts',
'lib/auth.ts',
'scripts/test-agent1.ts',
'scripts/test-agent2.ts',
'scripts/test-agent3.ts',
'scripts/test-orchestrator.ts',
'lib/graphql/queries.ts',
'lib/graphql/client.ts',
'hooks/use-toast.ts',
'components/WalletConnect.tsx',
'components/Learn.tsx',
'components/ContractInteraction.tsx',
'components/Analytics.tsx',
'app/providers.tsx',
'app/page.tsx',
'app/globals.css',
'app/layout.tsx',
],
maxFileSize: 1024 * 1024, // 1MB
};
function getRelativePath(fullPath, rootDir) {
return path.relative(rootDir, fullPath);
}
function shouldExcludeFile(filePath, config) {
const normalizedPath = path.normalize(filePath);
if (config.excludeFiles.some((pattern) => normalizedPath.includes(pattern))) {
return true;
}
if (config.excludePaths.some((excludePath) =>
normalizedPath.includes(path.normalize(excludePath))
)) {
return true;
}
return false;
}
function shouldExcludeDir(dirPath, config) {
const normalizedPath = path.normalize(dirPath);
return config.excludeDirs.some((excludeDir) =>
normalizedPath.includes(path.normalize(excludeDir))
);
}
function collectFiles(dir, rootDir, config) {
let results = [];
const items = fs.readdirSync(dir, { withFileTypes: true });
for (const item of items) {
const fullPath = path.join(dir, item.name);
const relativePath = getRelativePath(fullPath, rootDir);
if (item.isDirectory()) {
if (!shouldExcludeDir(fullPath, config)) {
results = results.concat(collectFiles(fullPath, rootDir, config));
}
} else {
const ext = path.extname(item.name).toLowerCase();
if (config.extensions.includes(ext) && !shouldExcludeFile(relativePath, config)) {
const stats = fs.statSync(fullPath);
if (stats.size <= config.maxFileSize) {
results.push({ path: fullPath, relativePath });
}
}
}
}
return results;
}
function collectCode(outputFile, customConfig = {}) {
const config = { ...DEFAULT_CONFIG, ...customConfig };
try {
fs.writeFileSync(outputFile, '');
const rootDir = process.cwd();
console.log(`Processing project directory: ${rootDir}`);
const files = collectFiles(rootDir, rootDir, config);
files.sort((a, b) => a.relativePath.localeCompare(b.relativePath));
files.forEach(({ path: filePath, relativePath }) => {
const content = fs.readFileSync(filePath, 'utf8');
const separator = '='.repeat(80);
fs.appendFileSync(
outputFile,
`\n\n${separator}\nFile: ${relativePath}\n${separator}\n\n${content}`
);
});
console.log('Collection complete!');
} catch (error) {
console.error('Error during collection:', error);
process.exit(1);
}
}
const isMainModule = process.argv[1] === fileURLToPath(import.meta.url);
if (isMainModule) {
const outputFile = process.argv[2] || 'code-collection.txt';
collectCode(outputFile);
}
export { collectCode };