@@ -11,13 +11,7 @@ import {
11
11
import { tsquery } from '@phenomnomnominal/tsquery' ;
12
12
import { prompt } from 'enquirer' ;
13
13
import { 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' ;
21
15
import { addMetadataJson } from '../utils' ;
22
16
import { ANGULAR_THREE_VERSION , THREE_TYPE_VERSION , THREE_VERSION } from '../versions' ;
23
17
@@ -118,6 +112,7 @@ Please retry the generator with a "--project" specified.`);
118
112
const templateExist = tree . exists ( appComponentTemplatePath ) ;
119
113
const appComponentContent = exist ? tree . read ( appComponentPath , 'utf8' ) : null ;
120
114
const appComponentTemplateContent = templateExist ? tree . read ( appComponentTemplatePath , 'utf8' ) : null ;
115
+
121
116
if ( isStandalone ) {
122
117
if ( ! appComponentContent ) {
123
118
logger . warn ( `
@@ -126,41 +121,39 @@ AppComponent not found at ${appComponentPath}. Angular Three was initialized suc
126
121
let updatedContent = tsquery . replace (
127
122
appComponentContent ,
128
123
'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
+ } ,
136
127
) ;
137
- updatedContent = tsquery . replace ( updatedContent , 'SourceFile' , ( node : SourceFile ) => {
128
+
129
+ updatedContent = tsquery . replace ( updatedContent , 'SourceFile' , ( node ) => {
138
130
return `
139
131
import { NgtCanvas } from 'angular-three';
140
132
import { Experience } from './experience/experience.component';
141
- ${ node . getFullText ( ) } `;
133
+
134
+ ${ node . getFullText ( ) }
135
+ ` ;
142
136
} ) ;
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
+ }
164
157
if ( appComponentTemplateContent ) {
165
158
const updatedTemplateContent =
166
159
generateExperience === 'append'
0 commit comments