|
1 |
| -import { addDependenciesToPackageJson, installPackagesTask, logger, readJson, updateJson, type Tree } from '@nx/devkit'; |
| 1 | +import { |
| 2 | + addDependenciesToPackageJson, |
| 3 | + generateFiles, |
| 4 | + installPackagesTask, |
| 5 | + logger, |
| 6 | + readJson, |
| 7 | + readProjectConfiguration, |
| 8 | + updateJson, |
| 9 | + type Tree, |
| 10 | +} from '@nx/devkit'; |
| 11 | +import { tsquery } from '@phenomnomnominal/tsquery'; |
| 12 | +import { prompt } from 'enquirer'; |
| 13 | +import { join } from 'node:path'; |
| 14 | +import { |
| 15 | + ArrayLiteralExpression, |
| 16 | + ClassDeclaration, |
| 17 | + NoSubstitutionTemplateLiteral, |
| 18 | + SourceFile, |
| 19 | + factory, |
| 20 | +} from 'typescript'; |
2 | 21 | import { addMetadataJson } from '../utils';
|
3 | 22 | import { ANGULAR_THREE_VERSION, THREE_TYPE_VERSION, THREE_VERSION } from '../versions';
|
4 | 23 |
|
5 |
| -export default async function (tree: Tree) { |
| 24 | +type Schema = { |
| 25 | + project?: string; |
| 26 | +}; |
| 27 | + |
| 28 | +export default async function (tree: Tree, { project }: Schema) { |
6 | 29 | logger.log('Initializing Angular Three...');
|
7 | 30 |
|
8 | 31 | const packageJson = readJson(tree, 'package.json');
|
@@ -30,6 +53,135 @@ export default async function (tree: Tree) {
|
30 | 53 |
|
31 | 54 | addMetadataJson(tree, 'angular-three/metadata.json');
|
32 | 55 |
|
| 56 | + const { generateExperience } = await prompt<{ generateExperience: 'append' | 'replace' | 'none' }>({ |
| 57 | + type: 'select', |
| 58 | + name: 'generateExperience', |
| 59 | + message: 'Generate an Experience component?', |
| 60 | + choices: [ |
| 61 | + { value: 'append', name: 'append', message: 'Append <ngt-canvas /> to AppComponent template' }, |
| 62 | + { value: 'replace', name: 'replace', message: 'Replace AppComponent template with <ngt-canvas />' }, |
| 63 | + { value: 'none', name: 'none', message: 'Do not generate an Experience component' }, |
| 64 | + ], |
| 65 | + initial: 2, |
| 66 | + }); |
| 67 | + |
| 68 | + if (generateExperience !== 'none') { |
| 69 | + const isNx = tree.exists('nx.json'); |
| 70 | + const rootProjectJson = readJson(tree, 'project.json'); |
| 71 | + |
| 72 | + if (!project) { |
| 73 | + if (isNx) { |
| 74 | + if (!rootProjectJson) { |
| 75 | + throw new Error(` |
| 76 | +It seems like your workspace is an Integrated workspace but you did not provide a "project" name. |
| 77 | +Please retry the generator with a "--project" specified.`); |
| 78 | + } |
| 79 | + const rootName = rootProjectJson['name']; |
| 80 | + if (rootName === packageJson['name']) { |
| 81 | + project = rootName; |
| 82 | + } |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + if (!project) { |
| 87 | + throw new Error(` |
| 88 | +Angular Three generator could not find a default "project". |
| 89 | +Please retry the generator with a "--project" specified.`); |
| 90 | + } |
| 91 | + const projectConfig = readProjectConfiguration(tree, project); |
| 92 | + const sourceRoot = projectConfig.sourceRoot; |
| 93 | + |
| 94 | + if (sourceRoot) { |
| 95 | + // generate Experience component |
| 96 | + generateFiles(tree, join(__dirname, 'files'), sourceRoot, { __tmpl__: '' }); |
| 97 | + |
| 98 | + const { isStandalone } = await prompt<{ isStandalone: boolean }>({ |
| 99 | + type: 'confirm', |
| 100 | + initial: false, |
| 101 | + name: 'isStandalone', |
| 102 | + message: 'Is your project standalone?', |
| 103 | + }); |
| 104 | + const appComponentPath = `${sourceRoot}/app.component.ts`; |
| 105 | + const exist = tree.exists(appComponentPath); |
| 106 | + const appComponentTemplatePath = `${sourceRoot}/app.component.html`; |
| 107 | + const templateExist = tree.exists(appComponentTemplatePath); |
| 108 | + const appComponentContent = exist ? tree.read(appComponentPath, 'utf8') : null; |
| 109 | + const appComponentTemplateContent = templateExist ? tree.read(appComponentTemplatePath, 'utf8') : null; |
| 110 | + if (isStandalone) { |
| 111 | + if (!appComponentContent) { |
| 112 | + logger.warn(` |
| 113 | +AppComponent not found at ${appComponentPath}. Angular Three was initialized successfully but an Experience component was not generated.`); |
| 114 | + } else { |
| 115 | + let updatedContent = tsquery.replace( |
| 116 | + appComponentContent, |
| 117 | + 'Identifier[name="imports"] ~ ArrayLiteralExpression', |
| 118 | + (node: ArrayLiteralExpression) => |
| 119 | + factory |
| 120 | + .updateArrayLiteralExpression(node, [ |
| 121 | + ...node.elements, |
| 122 | + factory.createIdentifier('NgtCanvas'), |
| 123 | + ]) |
| 124 | + .getFullText(), |
| 125 | + ); |
| 126 | + updatedContent = tsquery.replace(updatedContent, 'SourceFile', (node: SourceFile) => { |
| 127 | + return ` |
| 128 | +import { NgtCanvas } from 'angular-three'; |
| 129 | +import { Experience } from './experience/experience.component'; |
| 130 | +${node.getFullText()}`; |
| 131 | + }); |
| 132 | + updatedContent = tsquery.replace(updatedContent, 'ClassDeclaration', (node: ClassDeclaration) => |
| 133 | + factory |
| 134 | + .updateClassDeclaration( |
| 135 | + node, |
| 136 | + node.modifiers, |
| 137 | + node.name, |
| 138 | + node.typeParameters, |
| 139 | + node.heritageClauses, |
| 140 | + [ |
| 141 | + factory.createPropertyDeclaration( |
| 142 | + [], |
| 143 | + 'scene', |
| 144 | + undefined, |
| 145 | + undefined, |
| 146 | + factory.createIdentifier('Experience'), |
| 147 | + ), |
| 148 | + ...node.members, |
| 149 | + ], |
| 150 | + ) |
| 151 | + .getFullText(), |
| 152 | + ); |
| 153 | + if (appComponentTemplateContent) { |
| 154 | + const updatedTemplateContent = |
| 155 | + generateExperience === 'append' |
| 156 | + ? ` |
| 157 | +${appComponentTemplateContent} |
| 158 | +<ngt-canvas [sceneGraph]="scene" />` |
| 159 | + : `<ngt-canvas [sceneGraph]="scene" />`; |
| 160 | + tree.write(appComponentTemplatePath, updatedTemplateContent); |
| 161 | + } else { |
| 162 | + updatedContent = tsquery.replace( |
| 163 | + updatedContent, |
| 164 | + 'Identifier[name="template"] ~ NoSubstitutionTemplateLiteral', |
| 165 | + (node: NoSubstitutionTemplateLiteral) => { |
| 166 | + return generateExperience === 'append' |
| 167 | + ? ` |
| 168 | +${node.getFullText()} |
| 169 | +<ngt-canvas [sceneGraph]="scene" />` |
| 170 | + : `<ngt-canvas [sceneGraph]="scene" />`; |
| 171 | + }, |
| 172 | + ); |
| 173 | + } |
| 174 | + |
| 175 | + tree.write(appComponentPath, updatedContent); |
| 176 | + } |
| 177 | + } else { |
| 178 | + } |
| 179 | + } else { |
| 180 | + logger.warn(` |
| 181 | +"sourceRoot" not found. Angular Three was initialized successfully but an Experience component was not generated.`); |
| 182 | + } |
| 183 | + } |
| 184 | + |
33 | 185 | return () => {
|
34 | 186 | installPackagesTask(tree);
|
35 | 187 | };
|
|
0 commit comments