|
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 | importtsfrom'../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{SchemaasAppShellOptions}from'./schema';
|
34 | 25 |
|
35 |
| -constAPP_SHELL_ROUTE='shell'; |
36 |
| - |
37 | 26 | functiongetSourceFile(host:Tree,path:string):ts.SourceFile{
|
38 | 27 | constcontent=host.readText(path);
|
39 | 28 | constsource=ts.createSourceFile(path,content,ts.ScriptTarget.Latest,true);
|
@@ -156,126 +145,6 @@ function getMetadataProperty(metadata: ts.Node, propertyName: string): ts.Proper
|
156 | 145 | returnproperty;
|
157 | 146 | }
|
158 | 147 |
|
159 |
| -functionaddServerRoutes(options:AppShellOptions):Rule{ |
160 |
| -returnasync(host:Tree)=>{ |
161 |
| -// The workspace gets updated so this needs to be reloaded |
162 |
| -constworkspace=awaitgetWorkspace(host); |
163 |
| -constproject=workspace.projects.get(options.project); |
164 |
| -if(!project){ |
165 |
| -thrownewSchematicsException(`Invalid project name (${options.project})`); |
166 |
| -} |
167 |
| - |
168 |
| -constmodulePath=getServerModulePath(host,project.sourceRoot||'src','main.server.ts'); |
169 |
| -if(modulePath===null){ |
170 |
| -thrownewSchematicsException('Server module not found.'); |
171 |
| -} |
172 |
| - |
173 |
| -letmoduleSource=getSourceFile(host,modulePath); |
174 |
| -if(!isImported(moduleSource,'Routes','@angular/router')){ |
175 |
| -constrecorder=host.beginUpdate(modulePath); |
176 |
| -constroutesChange=insertImport(moduleSource,modulePath,'Routes','@angular/router'); |
177 |
| -if(routesChange){ |
178 |
| -applyToUpdateRecorder(recorder,[routesChange]); |
179 |
| -} |
180 |
| - |
181 |
| -constimports=getSourceNodes(moduleSource) |
182 |
| -.filter((node)=>node.kind===ts.SyntaxKind.ImportDeclaration) |
183 |
| -.sort((a,b)=>a.getStart()-b.getStart()); |
184 |
| -constinsertPosition=imports[imports.length-1].getEnd(); |
185 |
| -constrouteText=`\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 |
| -constrecorder=host.beginUpdate(modulePath); |
193 |
| -constrouterModuleChange=insertImport( |
194 |
| -moduleSource, |
195 |
| -modulePath, |
196 |
| -'RouterModule', |
197 |
| -'@angular/router', |
198 |
| -); |
199 |
| - |
200 |
| -if(routerModuleChange){ |
201 |
| -applyToUpdateRecorder(recorder,[routerModuleChange]); |
202 |
| -} |
203 |
| - |
204 |
| -constmetadataChange=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 |
| -functionaddStandaloneServerRoute(options:AppShellOptions):Rule{ |
219 |
| -returnasync(host:Tree)=>{ |
220 |
| -constworkspace=awaitgetWorkspace(host); |
221 |
| -constproject=workspace.projects.get(options.project); |
222 |
| -if(!project){ |
223 |
| -thrownewSchematicsException(`Project name "${options.project}" doesn't not exist.`); |
224 |
| -} |
225 |
| - |
226 |
| -constconfigFilePath=join(project.sourceRoot??'src','app/app.config.server.ts'); |
227 |
| -if(!host.exists(configFilePath)){ |
228 |
| -thrownewSchematicsException(`Cannot find "${configFilePath}".`); |
229 |
| -} |
230 |
| - |
231 |
| -constrecorder=host.beginUpdate(configFilePath); |
232 |
| -letconfigSourceFile=getSourceFile(host,configFilePath); |
233 |
| -if(!isImported(configSourceFile,'ROUTES','@angular/router')){ |
234 |
| -constroutesChange=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 |
| -constprovidersLiteral=findNodes(configSourceFile,ts.isPropertyAssignment).find( |
248 |
| -(n)=>ts.isArrayLiteralExpression(n.initializer)&&n.name.getText()==='providers', |
249 |
| -)?.initializerasts.ArrayLiteralExpression|undefined; |
250 |
| -if(!providersLiteral){ |
251 |
| -thrownewSchematicsException( |
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 |
| -constupdatedProvidersString=[ |
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 | functionaddServerRoutingConfig(options:AppShellOptions,isStandalone:boolean):Rule{
|
280 | 149 | returnasync(host:Tree)=>{
|
281 | 150 | constworkspace=awaitgetWorkspace(host);
|
@@ -335,11 +204,6 @@ export default function (options: AppShellOptions): Rule {
|
335 | 204 | returnchain([
|
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',
|
|