@@ -11,13 +11,7 @@ import {
1111import { tsquery } from '@phenomnomnominal/tsquery' ;
1212import { prompt } from 'enquirer' ;
1313import { join } from 'node:path' ;
14- import {
15- ArrayLiteralExpression ,
16- ClassDeclaration ,
17- NoSubstitutionTemplateLiteral ,
18- SourceFile ,
19- factory ,
20- } from 'typescript' ;
14+ import { ArrayLiteralExpression , NoSubstitutionTemplateLiteral } from 'typescript' ;
2115import { addMetadataJson } from '../utils' ;
2216import { ANGULAR_THREE_VERSION , THREE_TYPE_VERSION , THREE_VERSION } from '../versions' ;
2317
@@ -118,6 +112,7 @@ Please retry the generator with a "--project" specified.`);
118112 const templateExist = tree . exists ( appComponentTemplatePath ) ;
119113 const appComponentContent = exist ? tree . read ( appComponentPath , 'utf8' ) : null ;
120114 const appComponentTemplateContent = templateExist ? tree . read ( appComponentTemplatePath , 'utf8' ) : null ;
115+
121116 if ( isStandalone ) {
122117 if ( ! appComponentContent ) {
123118 logger . warn ( `
@@ -126,41 +121,39 @@ AppComponent not found at ${appComponentPath}. Angular Three was initialized suc
126121 let updatedContent = tsquery . replace (
127122 appComponentContent ,
128123 'Identifier[name="imports"] ~ ArrayLiteralExpression' ,
129- ( node : ArrayLiteralExpression ) =>
130- factory
131- . updateArrayLiteralExpression ( node , [
132- ...node . elements ,
133- factory . createIdentifier ( 'NgtCanvas' ) ,
134- ] )
135- . getFullText ( ) ,
124+ ( node : ArrayLiteralExpression ) => {
125+ return `[${ node . elements . map ( ( element ) => element [ 'escapedText' ] ) . join ( ', ' ) } , NgtCanvas]` ;
126+ } ,
136127 ) ;
137- updatedContent = tsquery . replace ( updatedContent , 'SourceFile' , ( node : SourceFile ) => {
128+
129+ updatedContent = tsquery . replace ( updatedContent , 'SourceFile' , ( node ) => {
138130 return `
139131import { NgtCanvas } from 'angular-three';
140132import { Experience } from './experience/experience.component';
141- ${ node . getFullText ( ) } `;
133+
134+ ${ node . getFullText ( ) }
135+ ` ;
142136 } ) ;
143- updatedContent = tsquery . replace ( updatedContent , 'ClassDeclaration' , ( node : ClassDeclaration ) =>
144- factory
145- . updateClassDeclaration (
146- node ,
147- node . modifiers ,
148- node . name ,
149- node . typeParameters ,
150- node . heritageClauses ,
151- [
152- factory . createPropertyDeclaration (
153- [ ] ,
154- 'scene' ,
155- undefined ,
156- undefined ,
157- factory . createIdentifier ( 'Experience' ) ,
158- ) ,
159- ...node . members ,
160- ] ,
161- )
162- . getFullText ( ) ,
163- ) ;
137+
138+ const contentNode = tsquery . ast ( appComponentContent ) ;
139+ const classMembersQuery = 'ClassDeclaration > :matches(PropertyDeclaration,MethodDeclaration)' ;
140+ const members = tsquery . match ( contentNode , classMembersQuery ) ;
141+
142+ if ( members . length === 0 ) {
143+ updatedContent = tsquery . replace ( updatedContent , 'ClassDeclaration' , ( node ) => {
144+ const withoutBraces = node . getFullText ( ) . slice ( 0 , - 1 ) ;
145+ return `
146+ ${ withoutBraces }
147+ scene = Experience;
148+ }` ;
149+ } ) ;
150+ } else {
151+ updatedContent = tsquery . replace ( updatedContent , classMembersQuery , ( node ) => {
152+ return `
153+ scene = Experience;
154+ ${ node . getFullText ( ) } `;
155+ } ) ;
156+ }
164157 if ( appComponentTemplateContent ) {
165158 const updatedTemplateContent =
166159 generateExperience === 'append'
0 commit comments