@@ -644,7 +644,7 @@ export function addComponentToEnd(
644644 }
645645
646646 if ( ! componentFile ) {
647- throw new Error ( `Component file not found: ${ componentPath } ` ) ;
647+ throw new DNDError ( `Component file not found: ${ componentPath } ` ) ;
648648 }
649649
650650 // determin the imported export
@@ -655,19 +655,41 @@ export function addComponentToEnd(
655655
656656 // If no export name is provided and there are multiple exports, we need to ask the user to specify one.
657657 if ( ! defaultExport && exportNames . length > 1 && ! exportName ) {
658- const error = new Error ( `Multiple exports found in ${ componentPath } , please specify an export to use.` ) as Error & { multipleExports : string [ ] ; type : string } ;
658+ const error = new DNDError ( `Multiple exports found in ${ componentPath } , please specify an export to use.` , 'multiple-exports' ) ;
659659 error . multipleExports = exportNames ;
660- error . type = 'multiple-exports' ;
661660 throw error ;
662661 }
663662 // If no exports found, throw.
664663 if ( ! defaultExport && exportNames . length === 0 ) {
665- throw new Error ( `No exports found in ${ componentPath } ` ) ;
664+ throw new DNDError ( `No exports found in ${ componentPath } ` ) ;
666665 }
667666
668667 // Determine which export to use
669668 const componentExportName = defaultExport ? "__default__" : exportName ?? exportNames [ 0 ] ;
670669
670+ // Determine the import name
671+
672+ if ( ! componentExportName ) {
673+ throw new DNDError ( `No exports found in ${ componentPath } ` ) ;
674+ }
675+
676+ // Ensure the import exists
677+ const componentImportName = ensureImport ( sceneFile , componentPath , componentExportName ) ;
678+
679+ const components = [ ...sceneFile . getExportedDeclarations ( ) . keys ( ) ] ;
680+ const sceneExportName = components . includes ( activeScene || "" ) ? activeScene : components [ 0 ] ;
681+
682+ if ( ! sceneExportName ) {
683+ throw new DNDError ( `No exports found in scene file` ) ;
684+ }
685+
686+ const jsxElement = getExportJsxElement ( sceneFile , sceneExportName ) ;
687+
688+ const newComponentJsx = `<${ componentImportName } />` ;
689+
690+ insertAtEnd ( sceneFile , jsxElement , newComponentJsx ) ;
691+ }
692+ export function getUniqueImportName ( componentExportName : string , componentPath : string , sceneFile : SourceFile ) {
671693 // Determine the import name
672694 let componentImportName = componentExportName === "__default__" ? toPascalCase ( basename ( componentPath ) . replace ( extname ( componentPath ) , '' ) ) : componentExportName ;
673695 let componentImportNameAddition = 0 ;
@@ -696,35 +718,16 @@ export function addComponentToEnd(
696718 componentImportNameAddition ++ ;
697719 }
698720 componentImportName = `${ componentImportName } ${ componentImportNameAddition === 0 ? "" : '_' + componentImportNameAddition } ` ;
699-
700- if ( ! componentExportName ) {
701- throw new Error ( `No exports found in ${ componentPath } ` ) ;
702- }
703-
704- // Ensure the import exists
705- componentImportName = ensureImport ( sceneFile , componentPath , componentExportName , componentImportName ) ;
706-
707- const components = [ ...sceneFile . getExportedDeclarations ( ) . keys ( ) ] ;
708- const sceneExportName = components . includes ( activeScene || "" ) ? activeScene : components [ 0 ] ;
709-
710- if ( ! sceneExportName ) {
711- throw new Error ( `No exports found in scene file` ) ;
712- }
713-
714- const jsxElement = getExportJsxElement ( sceneFile , sceneExportName ) ;
715-
716- const newComponentJsx = `<${ componentImportName } />` ;
717-
718- insertAtEnd ( sceneFile , jsxElement , newComponentJsx ) ;
721+ return componentImportName ;
719722}
720723
721724/** Ensures an import exists for the component */
722725function ensureImport (
723726 sourceFile : SourceFile ,
724727 modulePath : string ,
725728 exportName : string ,
726- importName : string ,
727729) : string {
730+ const importName = getUniqueImportName ( exportName , modulePath , sourceFile ) ;
728731 const baseFolderPath = dirname ( sourceFile . getFilePath ( ) ) ;
729732 const relativePath = omitFileExtension ( prefixLocalPath ( relative ( baseFolderPath , modulePath ) ) ) ;
730733
0 commit comments