|
6 | 6 | * found in the LICENSE file at https://angular.dev/license |
7 | 7 | */ |
8 | 8 |
|
9 | | -import { |
10 | | - Rule, |
11 | | - SchematicsException, |
12 | | - Tree, |
13 | | - chain, |
14 | | - noop, |
15 | | - schematic, |
16 | | -} from '@angular-devkit/schematics'; |
| 9 | +import { Rule, SchematicsException, Tree, chain, schematic } from '@angular-devkit/schematics'; |
17 | 10 | import { dirname, join } from 'node:path/posix'; |
18 | 11 | import ts from '../third_party/github.com/Microsoft/TypeScript/lib/typescript'; |
19 | 12 | import { |
20 | | - addSymbolToNgModuleMetadata, |
21 | 13 | findNode, |
22 | 14 | findNodes, |
23 | 15 | getDecoratorMetadata, |
24 | 16 | getSourceNodes, |
25 | 17 | insertImport, |
26 | | - isImported, |
27 | 18 | } from '../utility/ast-utils'; |
28 | 19 | import { applyToUpdateRecorder } from '../utility/change'; |
29 | 20 | import { getAppModulePath, isStandaloneApp } from '../utility/ng-ast-utils'; |
30 | | -import { isUsingApplicationBuilder, targetBuildNotFoundError } from '../utility/project-targets'; |
| 21 | +import { targetBuildNotFoundError } from '../utility/project-targets'; |
31 | 22 | import { findBootstrapApplicationCall, getMainFilePath } from '../utility/standalone/util'; |
32 | 23 | import { getWorkspace } from '../utility/workspace'; |
33 | 24 | import { Schema as AppShellOptions } from './schema'; |
34 | 25 |
|
35 | | -const APP_SHELL_ROUTE = 'shell'; |
36 | | - |
37 | 26 | function getSourceFile(host: Tree, path: string): ts.SourceFile { |
38 | 27 | const content = host.readText(path); |
39 | 28 | const source = ts.createSourceFile(path, content, ts.ScriptTarget.Latest, true); |
@@ -156,126 +145,6 @@ function getMetadataProperty(metadata: ts.Node, propertyName: string): ts.Proper |
156 | 145 | return property; |
157 | 146 | } |
158 | 147 |
|
159 | | -function addServerRoutes(options: AppShellOptions): Rule { |
160 | | - return async (host: Tree) => { |
161 | | - // The workspace gets updated so this needs to be reloaded |
162 | | - const workspace = await getWorkspace(host); |
163 | | - const project = workspace.projects.get(options.project); |
164 | | - if (!project) { |
165 | | - throw new SchematicsException(`Invalid project name (${options.project})`); |
166 | | - } |
167 | | - |
168 | | - const modulePath = getServerModulePath(host, project.sourceRoot || 'src', 'main.server.ts'); |
169 | | - if (modulePath === null) { |
170 | | - throw new SchematicsException('Server module not found.'); |
171 | | - } |
172 | | - |
173 | | - let moduleSource = getSourceFile(host, modulePath); |
174 | | - if (!isImported(moduleSource, 'Routes', '@angular/router')) { |
175 | | - const recorder = host.beginUpdate(modulePath); |
176 | | - const routesChange = insertImport(moduleSource, modulePath, 'Routes', '@angular/router'); |
177 | | - if (routesChange) { |
178 | | - applyToUpdateRecorder(recorder, [routesChange]); |
179 | | - } |
180 | | - |
181 | | - const imports = getSourceNodes(moduleSource) |
182 | | - .filter((node) => node.kind === ts.SyntaxKind.ImportDeclaration) |
183 | | - .sort((a, b) => a.getStart() - b.getStart()); |
184 | | - const insertPosition = imports[imports.length - 1].getEnd(); |
185 | | - const routeText = `\n\nconst routes: Routes = [ { path: '${APP_SHELL_ROUTE}', component: AppShell }];`; |
186 | | - recorder.insertRight(insertPosition, routeText); |
187 | | - host.commitUpdate(recorder); |
188 | | - } |
189 | | - |
190 | | - moduleSource = getSourceFile(host, modulePath); |
191 | | - if (!isImported(moduleSource, 'RouterModule', '@angular/router')) { |
192 | | - const recorder = host.beginUpdate(modulePath); |
193 | | - const routerModuleChange = insertImport( |
194 | | - moduleSource, |
195 | | - modulePath, |
196 | | - 'RouterModule', |
197 | | - '@angular/router', |
198 | | - ); |
199 | | - |
200 | | - if (routerModuleChange) { |
201 | | - applyToUpdateRecorder(recorder, [routerModuleChange]); |
202 | | - } |
203 | | - |
204 | | - const metadataChange = addSymbolToNgModuleMetadata( |
205 | | - moduleSource, |
206 | | - modulePath, |
207 | | - 'imports', |
208 | | - 'RouterModule.forRoot(routes)', |
209 | | - ); |
210 | | - if (metadataChange) { |
211 | | - applyToUpdateRecorder(recorder, metadataChange); |
212 | | - } |
213 | | - host.commitUpdate(recorder); |
214 | | - } |
215 | | - }; |
216 | | -} |
217 | | - |
218 | | -function addStandaloneServerRoute(options: AppShellOptions): Rule { |
219 | | - return async (host: Tree) => { |
220 | | - const workspace = await getWorkspace(host); |
221 | | - const project = workspace.projects.get(options.project); |
222 | | - if (!project) { |
223 | | - throw new SchematicsException(`Project name "${options.project}" doesn't not exist.`); |
224 | | - } |
225 | | - |
226 | | - const configFilePath = join(project.sourceRoot ?? 'src', 'app/app.config.server.ts'); |
227 | | - if (!host.exists(configFilePath)) { |
228 | | - throw new SchematicsException(`Cannot find "${configFilePath}".`); |
229 | | - } |
230 | | - |
231 | | - const recorder = host.beginUpdate(configFilePath); |
232 | | - let configSourceFile = getSourceFile(host, configFilePath); |
233 | | - if (!isImported(configSourceFile, 'ROUTES', '@angular/router')) { |
234 | | - const routesChange = insertImport( |
235 | | - configSourceFile, |
236 | | - configFilePath, |
237 | | - 'ROUTES', |
238 | | - '@angular/router', |
239 | | - ); |
240 | | - |
241 | | - if (routesChange) { |
242 | | - applyToUpdateRecorder(recorder, [routesChange]); |
243 | | - } |
244 | | - } |
245 | | - |
246 | | - configSourceFile = getSourceFile(host, configFilePath); |
247 | | - const providersLiteral = findNodes(configSourceFile, ts.isPropertyAssignment).find( |
248 | | - (n) => ts.isArrayLiteralExpression(n.initializer) && n.name.getText() === 'providers', |
249 | | - )?.initializer as ts.ArrayLiteralExpression | undefined; |
250 | | - if (!providersLiteral) { |
251 | | - throw new SchematicsException( |
252 | | - `Cannot find the "providers" configuration in "${configFilePath}".`, |
253 | | - ); |
254 | | - } |
255 | | - |
256 | | - // Add route to providers literal. |
257 | | - recorder.remove(providersLiteral.getStart(), providersLiteral.getWidth()); |
258 | | - const updatedProvidersString = [ |
259 | | - ...providersLiteral.elements.map((element) => ' ' + element.getText()), |
260 | | - ` { |
261 | | - provide: ROUTES, |
262 | | - multi: true, |
263 | | - useValue: [{ |
264 | | - path: '${APP_SHELL_ROUTE}', |
265 | | - component: AppShell |
266 | | - }] |
267 | | - }\n `, |
268 | | - ]; |
269 | | - |
270 | | - recorder.insertRight(providersLiteral.getStart(), `[\n${updatedProvidersString.join(',\n')}]`); |
271 | | - |
272 | | - applyToUpdateRecorder(recorder, [ |
273 | | - insertImport(configSourceFile, configFilePath, 'AppShell', './app-shell/app-shell'), |
274 | | - ]); |
275 | | - host.commitUpdate(recorder); |
276 | | - }; |
277 | | -} |
278 | | - |
279 | 148 | function addServerRoutingConfig(options: AppShellOptions, isStandalone: boolean): Rule { |
280 | 149 | return async (host: Tree) => { |
281 | 150 | const workspace = await getWorkspace(host); |
@@ -335,11 +204,6 @@ export default function (options: AppShellOptions): Rule { |
335 | 204 | return chain([ |
336 | 205 | validateProject(browserEntryPoint), |
337 | 206 | schematic('server', options), |
338 | | - ...(isUsingApplicationBuilder(project) |
339 | | - ? [noop()] |
340 | | - : isStandalone |
341 | | - ? [addStandaloneServerRoute(options)] |
342 | | - : [addServerRoutes(options)]), |
343 | 207 | addServerRoutingConfig(options, isStandalone), |
344 | 208 | schematic('component', { |
345 | 209 | name: 'app-shell', |
|
0 commit comments