Skip to content

Commit 2d16772

Browse files
committed
fix(core): adjust tsqueyr usage for generator
1 parent 365d328 commit 2d16772

File tree

2 files changed

+90
-37
lines changed

2 files changed

+90
-37
lines changed

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

Lines changed: 30 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,7 @@ import {
1111
import { tsquery } from '@phenomnomnominal/tsquery';
1212
import { prompt } from 'enquirer';
1313
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';
2115
import { addMetadataJson } from '../utils';
2216
import { 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 `
139131
import { NgtCanvas } from 'angular-three';
140132
import { 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'

tools/scripts/test-bed.mjs

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import { tsquery } from '@phenomnomnominal/tsquery';
2+
3+
const content = `
4+
import { Component } from '@angular/core';
5+
import { NxWelcomeComponent } from './nx-welcome.component';
6+
7+
@Component({
8+
standalone: true,
9+
imports: [NxWelcomeComponent],
10+
selector: 'app-root',
11+
template: \`\`,
12+
styleUrls: ['./app.component.css'],
13+
})
14+
export class AppComponent {
15+
}`;
16+
17+
let updatedContent = tsquery.replace(content, 'Identifier[name="imports"] ~ ArrayLiteralExpression', (node) => {
18+
return `[${node.elements.map((element) => element.escapedText).join(', ')}, NgtCanvas]`;
19+
});
20+
21+
updatedContent = tsquery.replace(
22+
updatedContent,
23+
'Identifier[name="template"] ~ NoSubstitutionTemplateLiteral',
24+
(node) => {
25+
return `\`<ngt-canvas [sceneGraph]="scene" />\``;
26+
},
27+
);
28+
29+
updatedContent = tsquery.replace(updatedContent, 'SourceFile', (node) => {
30+
return `
31+
import { NgtCanvas } from 'angular-three';
32+
import { Experience } from './experience/experience.component';
33+
34+
${node.getFullText()}
35+
`;
36+
});
37+
38+
const contentNode = tsquery.ast(content);
39+
const classMembersQuery = 'ClassDeclaration > :matches(PropertyDeclaration,MethodDeclaration)';
40+
const members = tsquery.match(contentNode, classMembersQuery);
41+
42+
if (members.length === 0) {
43+
updatedContent = tsquery.replace(updatedContent, 'ClassDeclaration', (node) => {
44+
const withoutBraces = node.getFullText().slice(0, -1);
45+
return `
46+
${withoutBraces}
47+
scene = Experience;
48+
}
49+
`;
50+
});
51+
} else {
52+
updatedContent = tsquery.replace(updatedContent, classMembersQuery, (node) => {
53+
return `
54+
scene = Experience;
55+
${node.getFullText()}
56+
`;
57+
});
58+
}
59+
60+
console.log(updatedContent);

0 commit comments

Comments
 (0)