@@ -12,7 +12,7 @@ import {
1212 NEXT_PAGES_ROUTER_SCRIPT_WITH_AGENT ,
1313 SCRIPT_IMPORT ,
1414 TANSTACK_EFFECT_WITH_AGENT ,
15- VITE_SCRIPT_WITH_AGENT ,
15+ VITE_IMPORT_WITH_AGENT ,
1616 WEBPACK_IMPORT_WITH_AGENT ,
1717 type AgentIntegration ,
1818} from "./templates.js" ;
@@ -244,59 +244,7 @@ const addAgentToExistingNextApp = (
244244 } ;
245245} ;
246246
247- const addAgentToExistingVite = (
248- originalContent : string ,
249- agent : AgentIntegration ,
250- filePath : string ,
251- ) : TransformResult => {
252- if ( agent === "none" ) {
253- return {
254- success : true ,
255- filePath,
256- message : "React Grab is already configured" ,
257- noChanges : true ,
258- } ;
259- }
260-
261- const agentPackage = `@react-grab/${ agent } ` ;
262- if ( originalContent . includes ( agentPackage ) ) {
263- return {
264- success : true ,
265- filePath,
266- message : `Agent ${ agent } is already configured` ,
267- noChanges : true ,
268- } ;
269- }
270-
271- const agentImport = `import("${ agentPackage } /client");` ;
272- const reactGrabImportMatch = originalContent . match (
273- / i m p o r t \s * \( \s * [ " ' ] r e a c t - g r a b [ " ' ] \s * \) ; ? / ,
274- ) ;
275-
276- if ( reactGrabImportMatch ) {
277- const matchedText = reactGrabImportMatch [ 0 ] ;
278- const hasSemicolon = matchedText . endsWith ( ";" ) ;
279- const newContent = originalContent . replace (
280- matchedText ,
281- `${ hasSemicolon ? matchedText . slice ( 0 , - 1 ) : matchedText } ;\n ${ agentImport } ` ,
282- ) ;
283- return {
284- success : true ,
285- filePath,
286- message : `Add ${ agent } agent` ,
287- originalContent,
288- newContent,
289- } ;
290- }
291-
292- return {
293- success : false ,
294- filePath,
295- message : "Could not find React Grab import to add agent after" ,
296- } ;
297- } ;
298-
299- const addAgentToExistingWebpack = (
247+ const addAgentToExistingImport = (
300248 originalContent : string ,
301249 agent : AgentIntegration ,
302250 filePath : string ,
@@ -567,52 +515,61 @@ const transformNextPagesRouter = (
567515 } ;
568516} ;
569517
518+ const checkExistingInstallation = (
519+ filePath : string ,
520+ agent : AgentIntegration ,
521+ reactGrabAlreadyConfigured : boolean ,
522+ ) : TransformResult | null => {
523+ const content = readFileSync ( filePath , "utf-8" ) ;
524+ if ( ! hasReactGrabCode ( content ) ) return null ;
525+
526+ if ( reactGrabAlreadyConfigured ) {
527+ return addAgentToExistingImport ( content , agent , filePath ) ;
528+ }
529+ return {
530+ success : true ,
531+ filePath,
532+ message : "React Grab is already installed in this file" ,
533+ noChanges : true ,
534+ } ;
535+ } ;
536+
570537const transformVite = (
571538 projectRoot : string ,
572539 agent : AgentIntegration ,
573540 reactGrabAlreadyConfigured : boolean ,
574541 force : boolean = false ,
575542) : TransformResult => {
576- const indexPath = findIndexHtml ( projectRoot ) ;
543+ const entryPath = findEntryFile ( projectRoot ) ;
544+
545+ if ( ! force ) {
546+ const indexPath = findIndexHtml ( projectRoot ) ;
547+ if ( indexPath ) {
548+ const existingResult = checkExistingInstallation ( indexPath , agent , reactGrabAlreadyConfigured ) ;
549+ if ( existingResult ) return existingResult ;
550+ }
551+ }
577552
578- if ( ! indexPath ) {
553+ if ( ! entryPath ) {
579554 return {
580555 success : false ,
581556 filePath : "" ,
582- message : "Could not find index.html " ,
557+ message : "Could not find entry file (src/ index.tsx, src/main.tsx, etc.) " ,
583558 } ;
584559 }
585560
586- const originalContent = readFileSync ( indexPath , "utf-8" ) ;
587- let newContent = originalContent ;
588- const hasReactGrabInFile = hasReactGrabCode ( originalContent ) ;
589-
590- if ( ! force && hasReactGrabInFile && reactGrabAlreadyConfigured ) {
591- return addAgentToExistingVite ( originalContent , agent , indexPath ) ;
592- }
593-
594- if ( ! force && hasReactGrabInFile ) {
595- return {
596- success : true ,
597- filePath : indexPath ,
598- message : "React Grab is already installed in this file" ,
599- noChanges : true ,
600- } ;
561+ if ( ! force ) {
562+ const existingResult = checkExistingInstallation ( entryPath , agent , reactGrabAlreadyConfigured ) ;
563+ if ( existingResult ) return existingResult ;
601564 }
602565
603- const scriptBlock = VITE_SCRIPT_WITH_AGENT ( agent ) ;
604-
605- const headMatch = newContent . match ( / < h e a d [ ^ > ] * > / i) ;
606- if ( headMatch ) {
607- newContent = newContent . replace (
608- headMatch [ 0 ] ,
609- `${ headMatch [ 0 ] } \n ${ scriptBlock } ` ,
610- ) ;
611- }
566+ const originalContent = readFileSync ( entryPath , "utf-8" ) ;
567+ const importBlock = VITE_IMPORT_WITH_AGENT ( agent ) ;
568+ const newContent = `${ importBlock } \n\n${ originalContent } ` ;
612569
613570 return {
614571 success : true ,
615- filePath : indexPath ,
572+ filePath : entryPath ,
616573 message :
617574 "Add React Grab" + ( agent !== "none" ? ` with ${ agent } agent` : "" ) ,
618575 originalContent,
@@ -636,22 +593,12 @@ const transformWebpack = (
636593 } ;
637594 }
638595
639- const originalContent = readFileSync ( entryPath , "utf-8" ) ;
640- const hasReactGrabInFile = hasReactGrabCode ( originalContent ) ;
641-
642- if ( ! force && hasReactGrabInFile && reactGrabAlreadyConfigured ) {
643- return addAgentToExistingWebpack ( originalContent , agent , entryPath ) ;
644- }
645-
646- if ( ! force && hasReactGrabInFile ) {
647- return {
648- success : true ,
649- filePath : entryPath ,
650- message : "React Grab is already installed in this file" ,
651- noChanges : true ,
652- } ;
596+ if ( ! force ) {
597+ const existingResult = checkExistingInstallation ( entryPath , agent , reactGrabAlreadyConfigured ) ;
598+ if ( existingResult ) return existingResult ;
653599 }
654600
601+ const originalContent = readFileSync ( entryPath , "utf-8" ) ;
655602 const importBlock = WEBPACK_IMPORT_WITH_AGENT ( agent ) ;
656603 const newContent = `${ importBlock } \n\n${ originalContent } ` ;
657604
@@ -1128,8 +1075,17 @@ const findReactGrabFile = (
11281075 return findLayoutFile ( projectRoot ) ;
11291076 }
11301077 return findDocumentFile ( projectRoot ) ;
1131- case "vite" :
1132- return findIndexHtml ( projectRoot ) ;
1078+ case "vite" : {
1079+ const entryFile = findEntryFile ( projectRoot ) ;
1080+ if ( entryFile && hasReactGrabCode ( readFileSync ( entryFile , "utf-8" ) ) ) {
1081+ return entryFile ;
1082+ }
1083+ const indexHtml = findIndexHtml ( projectRoot ) ;
1084+ if ( indexHtml && hasReactGrabCode ( readFileSync ( indexHtml , "utf-8" ) ) ) {
1085+ return indexHtml ;
1086+ }
1087+ return entryFile ;
1088+ }
11331089 case "tanstack" :
11341090 return findTanStackRootFile ( projectRoot ) ;
11351091 case "webpack" :
0 commit comments