( filePath: string, injection: PluginInjection, )
| 264 | * Reads the file, transforms it, and writes it back |
| 265 | */ |
| 266 | export function injectPluginIntoFile( |
| 267 | filePath: string, |
| 268 | injection: PluginInjection, |
| 269 | ): { success: boolean; error?: string } { |
| 270 | try { |
| 271 | const code = readFileSync(filePath, 'utf-8') |
| 272 | |
| 273 | const devtoolsComponentName = findDevtoolsComponentName(code) |
| 274 | if (!devtoolsComponentName) { |
| 275 | return { |
| 276 | success: false, |
| 277 | error: 'Could not find TanStackDevtools import', |
| 278 | } |
| 279 | } |
| 280 | |
| 281 | const result = transformAndInject(code, injection, devtoolsComponentName) |
| 282 | |
| 283 | if (!result?.transformed) { |
| 284 | return { |
| 285 | success: false, |
| 286 | error: 'Plugin already exists or no TanStackDevtools component found', |
| 287 | } |
| 288 | } |
| 289 | |
| 290 | writeFileSync(filePath, result.code, 'utf-8') |
| 291 | |
| 292 | return { success: true } |
| 293 | } catch (e) { |
| 294 | console.error('Error injecting plugin:', e) |
| 295 | return { |
| 296 | success: false, |
| 297 | error: e instanceof Error ? e.message : 'Unknown error', |
| 298 | } |
| 299 | } |
| 300 | } |
no test coverage detected