Skip to content

Commit 8a14c89

Browse files
committed
fix(plugin): change init logic for geenrating experience
1 parent 1bf5884 commit 8a14c89

File tree

1 file changed

+14
-11
lines changed

1 file changed

+14
-11
lines changed

libs/plugin/src/generators/init/generator.ts

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,12 @@ export async function initGenerator(
7474

7575
if (generateExperience !== 'none') {
7676
const projects = getProjects(tree);
77-
const applicationProjects = Array.from(projects.values()).filter(
78-
(project) => project.projectType === 'application',
79-
);
77+
const applicationProjects = Array.from(projects.entries()).reduce((acc, [projectName, project]) => {
78+
if (project.projectType === 'application') {
79+
acc.push({ name: projectName, ...project });
80+
}
81+
return acc;
82+
}, []);
8083
let selectedProject: string;
8184
if (applicationProjects.length === 1) {
8285
selectedProject = applicationProjects[0].name;
@@ -130,7 +133,7 @@ export async function initGenerator(
130133

131134
let updatedContent = tsquery.replace(
132135
appComponentContent,
133-
'Identifier[name="imports"] ~ ArrayLiteralExpression',
136+
'PropertyAssignment:has(Identifier[name="imports"]) ArrayLiteralExpression',
134137
(node: ArrayLiteralExpression) => {
135138
return `[${node.elements.map((element) => element['escapedText']).join(', ')}, NgtCanvas]`;
136139
},
@@ -149,7 +152,7 @@ ${node.getFullText()}
149152
const classMembersQuery = 'ClassDeclaration > :matches(PropertyDeclaration,MethodDeclaration)';
150153
const members = tsquery.match(appComponentContentAst, classMembersQuery);
151154

152-
if (members.length === 0) {
155+
if (members.length === 0 || generateExperience === 'replace') {
153156
updatedContent = tsquery.replace(updatedContent, 'ClassDeclaration', (node) => {
154157
const withoutBraces = node.getFullText().slice(0, -1);
155158
return `
@@ -160,7 +163,7 @@ ${withoutBraces}
160163
} else {
161164
updatedContent = tsquery.replace(updatedContent, classMembersQuery, (node) => {
162165
return `
163-
scene = Experience;
166+
sceneGraph = Experience;
164167
${node.getFullText()}`;
165168
});
166169
}
@@ -170,19 +173,19 @@ ${node.getFullText()}`;
170173
generateExperience === 'append'
171174
? `
172175
${appComponentTemplateContent}
173-
<ngt-canvas [sceneGraph]="scene" />`
174-
: `<ngt-canvas [sceneGraph]="scene" />`;
176+
<ngt-canvas [sceneGraph]="sceneGraph" />`
177+
: `<ngt-canvas [sceneGraph]="sceneGraph" />`;
175178
tree.write(appComponentTemplatePath, updatedTemplateContent);
176179
} else {
177180
updatedContent = tsquery.replace(
178181
updatedContent,
179-
'Identifier[name="template"] ~ NoSubstitutionTemplateLiteral',
182+
'PropertyAssignment:has(Identifier[name="template"]) NoSubstitutionTemplateLiteral',
180183
(node: NoSubstitutionTemplateLiteral) => {
181184
return generateExperience === 'append'
182185
? `\`
183186
${node.getFullText()}
184-
<ngt-canvas [sceneGraph]="scene" />\``
185-
: `\`<ngt-canvas [sceneGraph]="scene" />\``;
187+
<ngt-canvas [sceneGraph]="sceneGraph" />\``
188+
: `\`<ngt-canvas [sceneGraph]="sceneGraph" />\``;
186189
},
187190
);
188191
}

0 commit comments

Comments
 (0)