(state: GraphState, cache: CodeCache)
| 212 | * Reads existing App.tsx, adds import and renders the root component |
| 213 | */ |
| 214 | export async function injectRootComponentToApp(state: GraphState, cache: CodeCache): Promise<void> { |
| 215 | try { |
| 216 | // Check if already injected |
| 217 | if (isAppInjected(cache)) { |
| 218 | logger.printInfoLog('⏭️ Skipping App.tsx injection (already injected)'); |
| 219 | return; |
| 220 | } |
| 221 | |
| 222 | logger.printInfoLog('💉 Injecting root component into App.tsx...'); |
| 223 | |
| 224 | // Construct App.tsx path |
| 225 | const appTsxPath = workspaceManager.resolveAppSrc(state.workspace, 'App.tsx'); |
| 226 | |
| 227 | // Read existing App.tsx or use default template |
| 228 | let appContent: string; |
| 229 | try { |
| 230 | appContent = fs.readFileSync(appTsxPath, 'utf8'); |
| 231 | } catch { |
| 232 | // Use default template if App.tsx doesn't exist |
| 233 | logger.printWarnLog('App.tsx not found, using default template'); |
| 234 | appContent = DEFAULT_APP_CONTENT; |
| 235 | } |
| 236 | |
| 237 | // Get root component information |
| 238 | const rootNode = state.protocol!; |
| 239 | const componentName = rootNode.data.name || 'RootComponent'; |
| 240 | const componentPath = rootNode.data.path || ''; |
| 241 | |
| 242 | // Generate prompt |
| 243 | const prompt = injectRootComponentPrompt({ |
| 244 | appContent, |
| 245 | componentName, |
| 246 | componentPath, |
| 247 | }); |
| 248 | |
| 249 | // Call AI model |
| 250 | const updatedCode = await callModel({ |
| 251 | question: prompt, |
| 252 | }); |
| 253 | |
| 254 | // Extract code (no markdown blocks expected based on prompt requirements) |
| 255 | const finalCode = updatedCode.includes('```') ? extractCode(updatedCode) : updatedCode.trim(); |
| 256 | |
| 257 | // Write updated App.tsx |
| 258 | const appFolderPath = path.dirname(appTsxPath); |
| 259 | writeFile(appFolderPath, 'App.tsx', finalCode); |
| 260 | |
| 261 | // Mark as injected and save immediately |
| 262 | saveAppInjected(cache, state.workspace); |
| 263 | |
| 264 | logger.printSuccessLog(`Successfully injected ${componentName} into App.tsx`); |
| 265 | } catch (error) { |
| 266 | logger.printErrorLog(`Failed to inject root component: ${(error as Error).message}`); |
| 267 | // Don't throw - allow the process to continue even if injection fails |
| 268 | } |
| 269 | } |
no test coverage detected