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
| class GeminiProjectGenerator { constructor(geminiAssistant) { this.gemini = geminiAssistant; }
async generateProjectSetup(projectRequirements) { const setup = { packageJson: {}, folderStructure: [], configurationFiles: {}, scripts: {}, dependencies: [] };
setup.packageJson = await this.generatePackageJson(projectRequirements);
setup.folderStructure = await this.generateFolderStructure(projectRequirements);
setup.configurationFiles = await this.generateConfigurationFiles(projectRequirements);
setup.scripts = await this.generateScripts(projectRequirements);
setup.dependencies = await this.determineDependencies(projectRequirements);
return setup; }
async generatePackageJson(requirements) { const prompt = ` Generate a package.json for a project with these requirements:
${JSON.stringify(requirements, null, 2)}
Include: - Project name and description - Dependencies based on requirements - Scripts for development/build/deployment - Keywords and author info - License and repository `;
const jsonStr = await this.gemini.generateCode(prompt);
try { return JSON.parse(jsonStr); } catch { const jsonMatch = jsonStr.match(/```json\n([\s\S]*?)\n```/); if (jsonMatch) { return JSON.parse(jsonMatch[1]); } throw new Error('Could not parse package.json'); } }
async generateFolderStructure(requirements) { const prompt = ` Generate a folder structure for a project with these requirements:
${JSON.stringify(requirements, null, 2)}
Return as a tree-like structure showing the recommended directory organization. `;
const structure = await this.gemini.generateCode(prompt);
const lines = structure.split('\n'); const folders = [];
for (const line of lines) { const trimmedLine = line.trim(); if (trimmedLine.endsWith('/') || trimmedLine.endsWith('\\')) { folders.push(trimmedLine.replace(/[\/\\]+$/, '')); } }
return folders; }
async generateConfigurationFiles(requirements) { const configs = {};
if (requirements.framework === 'react') { configs['.eslintrc.js'] = await this.generateESLintConfig(requirements); configs['.prettierrc'] = await this.generatePrettierConfig(requirements); }
if (requirements.typescript) { configs['tsconfig.json'] = await this.generateTSConfig(requirements); }
if (requirements.testing) { configs['jest.config.js'] = await this.generateJestConfig(requirements); }
if (requirements.build === 'vite') { configs['vite.config.js'] = await this.generateViteConfig(requirements); }
return configs; }
async generateESLintConfig(requirements) { const features = []; if (requirements.react) features.push('react'); if (requirements.typescript) features.push('typescript'); if (requirements.testing) features.push('testing-library');
const prompt = ` Generate an ESLint configuration for a project with features: ${features.join(', ')}
Include appropriate plugins, extends, and rules for modern development practices. `;
return await this.gemini.generateCode(prompt); }
async generateTSConfig(requirements) { const prompt = ` Generate a TypeScript configuration (tsconfig.json) for a project with requirements:
${JSON.stringify(requirements, null, 2)}
Consider the target framework, build system, and deployment environment. `;
const config = await this.gemini.generateCode(prompt); return JSON.parse(config); }
async generateScripts(requirements) { const scripts = { dev: 'Development server command', build: 'Build command', test: 'Test command', lint: 'Linting command', format: 'Formatting command' };
const prompt = ` Generate npm scripts for a project with these requirements:
${JSON.stringify(requirements, null, 2)}
Provide the actual command strings for each script type. `;
const scriptCommands = await this.gemini.generateCode(prompt);
const commandMap = { dev: scriptCommands.includes('dev') ? scriptCommands.match(/dev.*?npm run ([^\n\r]+)/)?.[1] || 'vite' : 'vite', build: scriptCommands.includes('build') ? scriptCommands.match(/build.*?npm run ([^\n\r]+)/)?.[1] || 'vite build' : 'vite build', test: 'vitest', lint: 'eslint . --ext .js,.jsx,.ts,.tsx', format: 'prettier --write .' };
return commandMap; }
async determineDependencies(requirements) { const prompt = ` Determine npm dependencies for a project with these requirements:
${JSON.stringify(requirements, null, 2)}
Separate into: - dependencies (production) - devDependencies (development) `;
const dependencies = await this.gemini.generateCode(prompt);
return { dependencies: [], devDependencies: [] }; }
async generateREADME(projectInfo) { const prompt = ` Generate a comprehensive README.md for this project:
${JSON.stringify(projectInfo, null, 2)}
Include sections for installation, usage, features, contributing, and license. `;
return await this.gemini.generateCode(prompt); }
async generateGitignore(techStack) { const prompt = ` Generate a comprehensive .gitignore file for a project using: ${techStack.join(', ')}
Include patterns for: - Build artifacts - Dependencies - Environment files - IDE files - OS files - Temporary files `;
return await this.gemini.generateCode(prompt); } }
const generator = new GeminiProjectGenerator(new GeminiProWebAssistant('KEY'));
const projectSetup = await generator.generateProjectSetup({ name: 'e-commerce-frontend', framework: 'react', typescript: true, build: 'vite', css: 'tailwind', state: 'zustand', testing: true, deployment: 'vercel' });
const readme = await generator.generateREADME({ name: 'e-commerce-frontend', description: 'Modern e-commerce frontend built with React and TypeScript', features: ['Product catalog', 'Shopping cart', 'User authentication', 'Payment integration'], techStack: ['React', 'TypeScript', 'Vite', 'Tailwind CSS', 'Zustand'] });
|