-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExportToGPT.js
executable file
·229 lines (193 loc) · 8.18 KB
/
ExportToGPT.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
#!/usr/bin/env node
const fs = require('fs');
const path = require('path');
const glob = require('glob');
const IGNORED_DIRS = [
'node_modules',
'.git',
'dist',
'build',
'.cache',
'.next',
'.nuxt',
'.vercel',
'.firebase',
'coverage',
'.parcel-cache',
'.expo',
'.env',
'tmp',
'temp',
'docs',
'logs',
'public',
'static'
];
const IGNORED_EXTENSIONS = [
'.png', '.jpg', '.jpeg', '.gif', '.bmp', // Image files
'.css', '.scss', '.less', '.sass' // Stylesheets
];
const TEMPLATE_EXTENSIONS = [
'.ejs', '.pug', '.hbs', '.handlebars', // Common template files
'.html' // If using embedded HTML templates
];
const FRAMEWORKS = {
EXPRESS: 'express',
REACT: 'react',
VUE: 'vue',
ANGULAR: 'angular',
KOA: 'koa',
NEST: 'nestjs',
};
function detectFramework(projectDir) {
if (fs.existsSync(path.join(projectDir, 'angular.json'))) return FRAMEWORKS.ANGULAR;
if (fs.existsSync(path.join(projectDir, 'vue.config.js'))) return FRAMEWORKS.VUE;
if (fs.existsSync(path.join(projectDir, 'package.json'))) {
const packageJson = require(path.join(projectDir, 'package.json'));
if (packageJson.dependencies['express']) return FRAMEWORKS.EXPRESS;
if (packageJson.dependencies['koa']) return FRAMEWORKS.KOA;
if (packageJson.dependencies['@nestjs/core']) return FRAMEWORKS.NEST;
if (packageJson.dependencies['react']) return FRAMEWORKS.REACT;
}
return null;
}
function getRelevantFiles(framework, entryFile, projectDir, seen = new Set()) {
let relevantFiles = new Set();
if (framework === FRAMEWORKS.EXPRESS) {
const entryPoints = ['app.js', 'server.js'];
entryPoints.forEach(entry => {
const fullPath = path.join(projectDir, entry);
if (fs.existsSync(fullPath)) relevantFiles.add(fullPath);
});
const routes = glob.sync(path.join(projectDir, 'routes/**/*.js'));
const controllers = glob.sync(path.join(projectDir, 'controllers/**/*.js'));
const models = glob.sync(path.join(projectDir, 'models/**/*.js'));
const views = glob.sync(path.join(projectDir, `views/**/*{${TEMPLATE_EXTENSIONS.join(',')}}`));
routes.forEach(file => relevantFiles.add(file));
controllers.forEach(file => relevantFiles.add(file));
models.forEach(file => relevantFiles.add(file));
views.forEach(file => relevantFiles.add(file));
} else if (framework === FRAMEWORKS.REACT) {
const components = glob.sync(path.join(projectDir, 'src/components/**/*.{js,jsx,tsx}'));
const hooks = glob.sync(path.join(projectDir, 'src/hooks/**/*.{js,jsx,tsx}'));
const redux = glob.sync(path.join(projectDir, 'src/redux/**/*.{js,jsx,tsx}'));
// Styles are ignored here based on requirement
// const styles = glob.sync(path.join(projectDir, 'src/styles/**/*.{css,scss}'));
components.forEach(file => relevantFiles.add(file));
hooks.forEach(file => relevantFiles.add(file));
redux.forEach(file => relevantFiles.add(file));
// styles.forEach(file => relevantFiles.add(file));
} else if (framework === FRAMEWORKS.VUE) {
const components = glob.sync(path.join(projectDir, 'src/components/**/*.vue'));
const store = glob.sync(path.join(projectDir, 'src/store/**/*.js'));
const mixins = glob.sync(path.join(projectDir, 'src/mixins/**/*.js'));
// Styles are ignored here based on requirement
// const styles = glob.sync(path.join(projectDir, 'src/assets/**/*.{css,scss}'));
components.forEach(file => relevantFiles.add(file));
store.forEach(file => relevantFiles.add(file));
mixins.forEach(file => relevantFiles.add(file));
// styles.forEach(file => relevantFiles.add(file));
} else if (framework === FRAMEWORKS.ANGULAR) {
const components = glob.sync(path.join(projectDir, 'src/app/**/*.{ts,html}'));
const services = glob.sync(path.join(projectDir, 'src/app/services/**/*.ts'));
const store = glob.sync(path.join(projectDir, 'src/app/store/**/*.ts'));
// Styles are ignored here based on requirement
// const styles = glob.sync(path.join(projectDir, 'src/styles/**/*.{css,scss}'));
components.forEach(file => relevantFiles.add(file));
services.forEach(file => relevantFiles.add(file));
store.forEach(file => relevantFiles.add(file));
// styles.forEach(file => relevantFiles.add(file));
} else {
const jsRelevantFiles = glob.sync(path.join(projectDir, `**/*.js`), {
ignore: IGNORED_DIRS.map(dir => `${dir}/**`)
});
jsRelevantFiles.forEach(file => relevantFiles.add(file));
}
relevantFiles.add(entryFile);
getDependencies(entryFile, projectDir, seen, relevantFiles);
return Array.from(relevantFiles).filter(file => !IGNORED_EXTENSIONS.some(ext => file.endsWith(ext)));
}
function getDependencies(filePath, projectDir, seen, relevantFiles) {
if (seen.has(filePath)) return;
seen.add(filePath);
const content = fs.readFileSync(filePath, 'utf-8');
const dir = path.dirname(filePath);
const requireMatches = content.match(/require\(['"]([^'"]+)['"]\)/g) || [];
const importMatches = content.match(/import\s+.*?\s+from\s+['"]([^'"]+)['"]/g) || [];
const includeMatches = content.match(/include\s+['"]([^'"]+)['"]/g) || [];
[...requireMatches, ...importMatches, ...includeMatches].forEach(match => {
const matchedPath = match.match(/['"]([^'"]+)['"]/)[1];
let resolvedPath;
if (!path.isAbsolute(matchedPath)) {
resolvedPath = path.resolve(dir, matchedPath);
if (!fs.existsSync(resolvedPath) && fs.existsSync(resolvedPath + '.js')) {
resolvedPath += '.js';
} else if (!fs.existsSync(resolvedPath) && fs.existsSync(resolvedPath + '.json')) {
resolvedPath += '.json';
} else if (!fs.existsSync(resolvedPath) && fs.existsSync(resolvedPath + '.hbs')) {
resolvedPath += '.hbs';
}
TEMPLATE_EXTENSIONS.forEach(templateExt => {
if (!fs.existsSync(resolvedPath) && fs.existsSync(resolvedPath + templateExt)) {
resolvedPath += templateExt;
}
});
} else {
resolvedPath = matchedPath;
}
if (fs.existsSync(resolvedPath) && !IGNORED_DIRS.some(ignoreDir => resolvedPath.includes(ignoreDir))) {
relevantFiles.add(resolvedPath);
getDependencies(resolvedPath, projectDir, seen, relevantFiles);
}
});
}
function getDirectoryStructure(dir) {
const structure = {};
function readDir(currentDir, parentStructure) {
const items = fs.readdirSync(currentDir);
items.forEach(item => {
const fullPath = path.join(currentDir, item);
const relativePath = path.relative(dir, fullPath);
const stat = fs.lstatSync(fullPath);
if (IGNORED_DIRS.some(ignoreDir => relativePath.startsWith(ignoreDir))) {
return;
}
if (stat.isDirectory()) {
parentStructure[item] = {};
readDir(fullPath, parentStructure[item]);
} else {
if (!IGNORED_EXTENSIONS.some(ext => item.endsWith(ext))) {
parentStructure[item] = 'file';
}
}
});
}
readDir(dir, structure);
return structure;
}
function outputResults(dir, relevantFiles, entryFile) {
console.log('Directory Structure:');
console.log(JSON.stringify(getDirectoryStructure(dir), null, 2));
const filesToOutput = relevantFiles.length > 5 ? [entryFile, ...relevantFiles, entryFile] : relevantFiles;
filesToOutput.forEach(file => {
console.log(`\nFile: ${file}`);
console.log('----------------------');
console.log(fs.readFileSync(file, 'utf-8'));
console.log('----------------------');
});
console.log("\n\n")
}
function main() {
const [projectDirInput, inputFile] = process.argv.slice(2);
if (!projectDirInput || !inputFile) {
console.error('Usage: node <script> <project_directory> <filename>');
process.exit(1);
}
const projectDir = path.resolve(projectDirInput);
const absInputFile = path.resolve(projectDir, inputFile);
const framework = detectFramework(projectDir);
const relevantFiles = getRelevantFiles(framework, absInputFile, projectDir);
console.log(`\n\n\nSYSTEM:\nYou are a Senior Node.js Software Engineer. Please use the following directory structure and provided project files to respond about the '${inputFile}' file. Also, please try to keep the programming style and structure similar to the provided file(s).\n\n`);
outputResults(projectDir, relevantFiles, absInputFile);
}
main();