@igniteui/angular-templates 17.1.13115 → 17.1.13116-beta.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,120 @@
1
+ import * as ts from 'typescript';
2
+ import { KeyValuePair, TemplateDependency, IFormatSettings } from '@igniteui/cli-core';
3
+ import { IAngularRouteLike } from './types';
4
+ export declare class AngularTypeScriptFileUpdate {
5
+ private standalone;
6
+ private readonly astTransformer;
7
+ constructor(targetPath: string, standalone?: boolean, formatSettings?: IFormatSettings);
8
+ /**
9
+ * Adds a route to the Angular routing module.
10
+ * @param route The route to add.
11
+ * @param multiline Whether to format the new entry as multiline.
12
+ */
13
+ addRoute(route: IAngularRouteLike, multiline?: boolean): ts.SourceFile;
14
+ /**
15
+ * Adds a child route to a parent route.
16
+ * @param parentPath The path of the parent route.
17
+ * @param route The child route to add.
18
+ * @param multiline Whether to format the new entry as multiline.
19
+ * @param prepend Whether to prepend the new added routes.
20
+ * @remarks The `parentPath` is used to determine where the child route should be added.
21
+ */
22
+ addChildRoute(parentPath: string, route: IAngularRouteLike, multiline?: boolean, prepend?: boolean): ts.SourceFile;
23
+ /**
24
+ * Adds an import identifier to a standalone component's metadata.
25
+ * @param dep The dependency to add to the standalone component's metadata.
26
+ * @param variables Variables to replace in the dependency strings.
27
+ * @param multiline Whether to format the new entry as multiline.
28
+ */
29
+ addStandaloneComponentMeta(dep: TemplateDependency, variables?: KeyValuePair<string>, multiline?: boolean): ts.SourceFile;
30
+ /**
31
+ * Adds metadata to an NgModule decorator.
32
+ * @param dep The dependency to add to the NgModule decorator.
33
+ * @param variables Variables to replace in the dependency strings.
34
+ * @param multiline Whether to format the new entry as multiline.
35
+ */
36
+ addNgModuleMeta(dep: TemplateDependency, variables?: KeyValuePair<string>, multiline?: boolean): ts.SourceFile;
37
+ /**
38
+ * Parses the AST and returns the resulting formatted source code.
39
+ * @remarks This method should be called after all modifications have been made to the AST.
40
+ */
41
+ finalize(): string;
42
+ /**
43
+ * Updates an Angular decorator's metadata.
44
+ * @param name The name of the decorator to update.
45
+ * @param meta Names of identifiers to be added.
46
+ * @param target The target metadata property to update.
47
+ * @param multiline Whether to format the new entry as multiline.
48
+ */
49
+ private mutateNgDecoratorMeta;
50
+ /**
51
+ * Checks if a node has an ancestor with a specific decorator.
52
+ * @param name The name of the decorator.
53
+ * @param node The node to start checking from.
54
+ */
55
+ private checkNgDecorator;
56
+ /**
57
+ * Finds a property assignment that exists in a specific NG decorator meta.
58
+ * @param decoratorName The name of the decorator to check.
59
+ * @param propName The property name to check.
60
+ * @param node The node to start checking from.
61
+ */
62
+ private findNgDecoratorProperty;
63
+ /**
64
+ * Creates an arrow function of the form `() => import(path).then(m => m.prop)`.
65
+ * @param path The path to the module to import.
66
+ * @param importedEntity The entity to import from the module.
67
+ * @param root Whether it should be `loadChildren` or `loadComponent`.
68
+ * @remarks If the `root` param is `true` the function will use `routes` as an `importedEntity`.
69
+ */
70
+ private createDynamicImport;
71
+ /**
72
+ * Converts a string or string array union to array.
73
+ * @param value The value to convert.
74
+ * @param variables Variables to replace in the strings.
75
+ * @remarks Splits strings as comma delimited.
76
+ */
77
+ private asArray;
78
+ /**
79
+ * Applies a configuration transformation to a string.
80
+ * @param data The string to transform.
81
+ * @param configuration The items to replace in the string.
82
+ */
83
+ private applyConfigTransformation;
84
+ /**
85
+ * Adds a new entry to the routes array. With a component identifier and path or with redirect option.
86
+ * @param route The route to add.
87
+ * @param visitCondition The condition used to find the appropriate route node.
88
+ * @param anchorElement The anchor element to insert.
89
+ * @param multiline Whether to format the new entry as multiline.
90
+ * @param prepend Whether to insert the new entry before the anchor element.
91
+ */
92
+ private addRedirectOrSimpleRouteEntry;
93
+ /**
94
+ * Adds a new not lazy-loaded route entry to the routes array. With a component identifier and path.
95
+ * @param route The route to add.
96
+ * @param visitCondition The condition used to find the appropriate route node.
97
+ * @param anchorElement The anchor element to insert to.
98
+ * @param multiline Whether to format the new entry as multiline.
99
+ * @param prepend Whether to insert the new entry before the anchor element.
100
+ */
101
+ private addRouteEntry;
102
+ /**
103
+ * Adds a new not lazy-loaded route entry to the routes array. With a redirect option.
104
+ * @param route The route to add.
105
+ * @param visitCondition The condition used to find the appropriate route node.
106
+ * @param anchorElement The anchor element to insert to.
107
+ * @param multiline Whether to format the new entry as multiline.
108
+ * @param prepend Whether to insert the new entry before the anchor element.
109
+ */
110
+ private addRedirectRouteEntry;
111
+ /**
112
+ * Adds a new lazy-loaded route entry to the routes array.
113
+ * @param route The route to add.
114
+ * @param visitCondition The condition used to find the appropriate route node.
115
+ * @param anchorElement The anchor element to insert to.
116
+ * @param multiline Whether to format the new entry as multiline.
117
+ * @param prepend Whether to insert the new entry before the anchor element.
118
+ */
119
+ private addLazyLoadedRouteEntry;
120
+ }
@@ -0,0 +1,412 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.AngularTypeScriptFileUpdate = void 0;
4
+ const ts = require("typescript");
5
+ const os_1 = require("os");
6
+ const cli_core_1 = require("@igniteui/cli-core");
7
+ const types_1 = require("./types");
8
+ // TODO: consider creating a global-constants file
9
+ const ROUTES_VARIABLE_NAME = 'routes';
10
+ const THEN_IDENTIFIER_NAME = 'then';
11
+ const IMPORT_IDENTIFIER_NAME = 'import';
12
+ const NG_MODULE_DECORATOR_NAME = 'NgModule';
13
+ const NG_SA_DECORATOR_NAME = 'Component';
14
+ const NG_FOR_ROOT_IDENTIFIER_NAME = 'forRoot';
15
+ // insert before node that contains this element
16
+ const anchorElement = {
17
+ name: 'path',
18
+ value: ts.factory.createStringLiteral('**'),
19
+ };
20
+ const printerOptions = {
21
+ newLine: os_1.EOL === '\n'
22
+ ? ts.NewLineKind.LineFeed
23
+ : ts.NewLineKind.CarriageReturnLineFeed,
24
+ };
25
+ class AngularTypeScriptFileUpdate {
26
+ constructor(targetPath, standalone = false, formatSettings) {
27
+ this.standalone = standalone;
28
+ /**
29
+ * Applies a configuration transformation to a string.
30
+ * @param data The string to transform.
31
+ * @param configuration The items to replace in the string.
32
+ */
33
+ this.applyConfigTransformation = (data, configuration) => {
34
+ let key;
35
+ for (key in configuration) {
36
+ data = data.replace(new RegExp(cli_core_1.Util.escapeRegExp(key), 'g'), configuration[key]);
37
+ }
38
+ return data;
39
+ };
40
+ const sourceFile = cli_core_1.TypeScriptUtils.getFileSource(targetPath);
41
+ this.astTransformer = new cli_core_1.TypeScriptASTTransformer(sourceFile, new cli_core_1.FormattingService(sourceFile, null, formatSettings, printerOptions), printerOptions // TODO: may not be needed
42
+ );
43
+ }
44
+ //#region Public API
45
+ /**
46
+ * Adds a route to the Angular routing module.
47
+ * @param route The route to add.
48
+ * @param multiline Whether to format the new entry as multiline.
49
+ */
50
+ addRoute(route, multiline = false) {
51
+ const visitCondition = (node) => !!this.astTransformer.findNodeAncenstor(node, (node) => ts.isVariableDeclaration(node) &&
52
+ node.name.getText() === ROUTES_VARIABLE_NAME);
53
+ if (route.lazyload &&
54
+ route.path &&
55
+ route.identifierName &&
56
+ route.modulePath) {
57
+ return this.addLazyLoadedRouteEntry(route, visitCondition, anchorElement, multiline, true // prepend
58
+ );
59
+ }
60
+ return this.addRedirectOrSimpleRouteEntry(route, visitCondition, anchorElement, multiline, true // prepend
61
+ );
62
+ }
63
+ /**
64
+ * Adds a child route to a parent route.
65
+ * @param parentPath The path of the parent route.
66
+ * @param route The child route to add.
67
+ * @param multiline Whether to format the new entry as multiline.
68
+ * @param prepend Whether to prepend the new added routes.
69
+ * @remarks The `parentPath` is used to determine where the child route should be added.
70
+ */
71
+ addChildRoute(parentPath, route, multiline = false, prepend = false) {
72
+ // find a node that has the parent's path
73
+ const visitCondition = (node) => ts.isPropertyAssignment(node) &&
74
+ node.name.getText() === cli_core_1.RouteTarget.Path &&
75
+ ts.isStringLiteral(node.initializer) &&
76
+ node.initializer.text === parentPath;
77
+ const parentRoute = this.astTransformer.findPropertyAssignment(visitCondition);
78
+ if (!parentRoute)
79
+ return this.astTransformer.sourceFile;
80
+ const childrenKey = 'children';
81
+ const parentContainsChildrenKey = ts.isObjectLiteralExpression(parentRoute.parent) &&
82
+ parentRoute.parent.properties.find((p) => { var _a; return ts.isPropertyAssignment(p) && ((_a = p.name) === null || _a === void 0 ? void 0 : _a.getText()) === childrenKey; });
83
+ if (!parentContainsChildrenKey) {
84
+ // if the parent route does not have the children array, create it
85
+ this.astTransformer.addMemberToObjectLiteral((node) => ts.isObjectLiteralExpression(node) &&
86
+ node.properties.some(visitCondition), {
87
+ name: childrenKey,
88
+ value: this.astTransformer.createArrayLiteralExpression([], multiline),
89
+ });
90
+ }
91
+ if (route.lazyload &&
92
+ route.path &&
93
+ route.identifierName &&
94
+ route.modulePath) {
95
+ return this.addLazyLoadedRouteEntry(route, visitCondition, anchorElement, multiline, true // prepend
96
+ );
97
+ }
98
+ // add the child route to the parent's children array
99
+ return this.addRedirectOrSimpleRouteEntry(route, (node) => ts.isPropertyAssignment(node.parent) &&
100
+ node.parent.name.getText() === childrenKey &&
101
+ // check if the parent route is the one we're looking for
102
+ !!this.astTransformer.findNodeAncenstor(node, (node) => ts.isObjectLiteralExpression(node) &&
103
+ node.properties.some(visitCondition)), anchorElement, multiline, prepend);
104
+ }
105
+ /**
106
+ * Adds an import identifier to a standalone component's metadata.
107
+ * @param dep The dependency to add to the standalone component's metadata.
108
+ * @param variables Variables to replace in the dependency strings.
109
+ * @param multiline Whether to format the new entry as multiline.
110
+ */
111
+ addStandaloneComponentMeta(dep, variables, multiline = false) {
112
+ if (!this.standalone || !dep.standalone)
113
+ return this.astTransformer.sourceFile;
114
+ const copy = {
115
+ import: this.asArray(dep.import || [], variables || {}),
116
+ provide: this.asArray(dep.provide || [], variables || {}),
117
+ };
118
+ if (dep.from) {
119
+ // add an import declaration for the dependency
120
+ const uniqueIdentifiers = new Set([
121
+ ...copy.import,
122
+ ...copy.provide,
123
+ ]);
124
+ const identifiers = [...uniqueIdentifiers].map((name) => ({ name }));
125
+ this.astTransformer.addImportDeclaration(identifiers.filter((i) => !this.astTransformer.importDeclarationCollides(i)), this.applyConfigTransformation(dep.from, variables));
126
+ }
127
+ if (copy.import.length > 0) {
128
+ return this.mutateNgDecoratorMeta(NG_SA_DECORATOR_NAME, copy.import, types_1.AngularDecoratorMetaTarget.Imports, multiline);
129
+ }
130
+ if (copy.provide.length > 0) {
131
+ return this.mutateNgDecoratorMeta(NG_SA_DECORATOR_NAME, copy.provide, types_1.AngularDecoratorMetaTarget.Providers, multiline);
132
+ }
133
+ return this.astTransformer.sourceFile;
134
+ }
135
+ /**
136
+ * Adds metadata to an NgModule decorator.
137
+ * @param dep The dependency to add to the NgModule decorator.
138
+ * @param variables Variables to replace in the dependency strings.
139
+ * @param multiline Whether to format the new entry as multiline.
140
+ */
141
+ addNgModuleMeta(dep, variables, multiline = false) {
142
+ if (this.standalone || dep.standalone)
143
+ return this.astTransformer.sourceFile;
144
+ const copy = {
145
+ import: this.asArray(dep.import || [], variables || {}),
146
+ declare: this.asArray(dep.declare || [], variables || {}),
147
+ provide: this.asArray(dep.provide || [], variables || {}),
148
+ export: this.asArray(dep.export || [], variables || {}),
149
+ };
150
+ if (dep.from) {
151
+ // add an import declaration for the dependency
152
+ const uniqueIdentifiers = new Set([
153
+ ...copy.import,
154
+ ...copy.declare,
155
+ ...copy.provide,
156
+ ...copy.export,
157
+ ]);
158
+ const identifiers = [...uniqueIdentifiers].map((name) => ({ name }));
159
+ this.astTransformer.addImportDeclaration(identifiers.filter((i) => !this.astTransformer.importDeclarationCollides(i)), this.applyConfigTransformation(dep.from, variables));
160
+ }
161
+ if (dep.root && copy.import.length > 0) {
162
+ // add forRoot to the module
163
+ copy.import = copy.import.map((i) => this.astTransformer.printer.printNode(ts.EmitHint.Unspecified, this.astTransformer.createCallExpression(i, NG_FOR_ROOT_IDENTIFIER_NAME), this.astTransformer.sourceFile));
164
+ }
165
+ if (copy.declare.length > 0) {
166
+ this.mutateNgDecoratorMeta(NG_MODULE_DECORATOR_NAME, copy.declare, types_1.AngularDecoratorMetaTarget.Declarations, multiline);
167
+ }
168
+ if (copy.import.length > 0) {
169
+ this.mutateNgDecoratorMeta(NG_MODULE_DECORATOR_NAME, copy.import, types_1.AngularDecoratorMetaTarget.Imports, multiline);
170
+ }
171
+ if (copy.provide.length > 0) {
172
+ this.mutateNgDecoratorMeta(NG_MODULE_DECORATOR_NAME, copy.provide, types_1.AngularDecoratorMetaTarget.Providers, multiline);
173
+ }
174
+ if (copy.export.length > 0) {
175
+ this.mutateNgDecoratorMeta(NG_MODULE_DECORATOR_NAME, copy.export, types_1.AngularDecoratorMetaTarget.Exports, multiline);
176
+ }
177
+ return this.astTransformer.sourceFile;
178
+ }
179
+ /**
180
+ * Parses the AST and returns the resulting formatted source code.
181
+ * @remarks This method should be called after all modifications have been made to the AST.
182
+ */
183
+ finalize() {
184
+ return this.astTransformer.finalize();
185
+ }
186
+ //#endregion
187
+ //#region Internals
188
+ /**
189
+ * Updates an Angular decorator's metadata.
190
+ * @param name The name of the decorator to update.
191
+ * @param meta Names of identifiers to be added.
192
+ * @param target The target metadata property to update.
193
+ * @param multiline Whether to format the new entry as multiline.
194
+ */
195
+ mutateNgDecoratorMeta(name, meta, target, multiline = false) {
196
+ const identifiers = meta.map(ts.factory.createIdentifier);
197
+ const targetMetaProp = this.findNgDecoratorProperty(name, target);
198
+ if (!targetMetaProp) {
199
+ return this.astTransformer.addMemberToObjectLiteral((node) => this.checkNgDecorator(name, node), {
200
+ name: target,
201
+ value: this.astTransformer.createArrayLiteralExpression(identifiers, multiline),
202
+ });
203
+ }
204
+ if (ts.isArrayLiteralExpression(targetMetaProp.initializer)) {
205
+ // prop assignment of the form { member: [...] }
206
+ // store to avoid second type check due to different context in the filter
207
+ const elementsAsNodeArray = targetMetaProp.initializer.elements;
208
+ const elementsToAdd = identifiers.filter((i) => !elementsAsNodeArray.some((el) => el.getText() === i.text));
209
+ return this.astTransformer.addMembersToArrayLiteral((node) => ts.isPropertyAssignment(node.parent) &&
210
+ node.parent.name.getText() === target &&
211
+ this.checkNgDecorator(name, node), elementsToAdd);
212
+ }
213
+ // prop assignment of the form { member: <some-val> }
214
+ return this.astTransformer.updateObjectLiteralMember((node) => this.checkNgDecorator(name, node), {
215
+ name: target,
216
+ value: this.astTransformer.createArrayLiteralExpression([targetMetaProp.initializer, ...identifiers], multiline),
217
+ });
218
+ }
219
+ /**
220
+ * Checks if a node has an ancestor with a specific decorator.
221
+ * @param name The name of the decorator.
222
+ * @param node The node to start checking from.
223
+ */
224
+ checkNgDecorator(name, node) {
225
+ return !!this.astTransformer.findNodeAncenstor(node, (node) => {
226
+ var _a;
227
+ return ts.isDecorator(node) &&
228
+ ((_a = node.expression.getFirstToken()) === null || _a === void 0 ? void 0 : _a.getText()) === name;
229
+ });
230
+ }
231
+ /**
232
+ * Finds a property assignment that exists in a specific NG decorator meta.
233
+ * @param decoratorName The name of the decorator to check.
234
+ * @param propName The property name to check.
235
+ * @param node The node to start checking from.
236
+ */
237
+ findNgDecoratorProperty(decoratorName, propName) {
238
+ return this.astTransformer.findPropertyAssignment((node) => ts.isPropertyAssignment(node) &&
239
+ node.name.getText() === propName &&
240
+ !!this.astTransformer.findNodeAncenstor(node, (node) => {
241
+ var _a;
242
+ return ts.isDecorator(node) &&
243
+ ((_a = node.expression.getFirstToken()) === null || _a === void 0 ? void 0 : _a.getText()) === decoratorName;
244
+ }));
245
+ }
246
+ /**
247
+ * Creates an arrow function of the form `() => import(path).then(m => m.prop)`.
248
+ * @param path The path to the module to import.
249
+ * @param importedEntity The entity to import from the module.
250
+ * @param root Whether it should be `loadChildren` or `loadComponent`.
251
+ * @remarks If the `root` param is `true` the function will use `routes` as an `importedEntity`.
252
+ */
253
+ createDynamicImport(path, importedEntity, root = false) {
254
+ const thenClause = root ? ROUTES_VARIABLE_NAME : importedEntity;
255
+ // create the 'import(path)' expression
256
+ const importExpression = ts.factory.createCallExpression(ts.factory.createIdentifier(IMPORT_IDENTIFIER_NAME), undefined, // type arguments
257
+ [ts.factory.createStringLiteral(path)]);
258
+ const thenFuncParamName = 'm';
259
+ // create the 'm => m.prop' arrow function
260
+ const thenExprArrowFuncBody = ts.factory.createArrowFunction(undefined, // modifiers
261
+ undefined, // type parameters
262
+ [
263
+ ts.factory.createParameterDeclaration(undefined, // decorators
264
+ undefined, // modifiers
265
+ ts.factory.createIdentifier(thenFuncParamName)),
266
+ ], undefined, ts.factory.createToken(ts.SyntaxKind.EqualsGreaterThanToken), ts.factory.createPropertyAccessChain(ts.factory.createIdentifier(thenFuncParamName), undefined, // question-dot token
267
+ thenClause));
268
+ // build the '.then(m => m.prop)' expression and add it to the import expression
269
+ const body = ts.factory.createCallExpression(ts.factory.createPropertyAccessExpression(importExpression, THEN_IDENTIFIER_NAME), undefined, // type arguments
270
+ [thenExprArrowFuncBody]);
271
+ // Create the '() => import(path).then(m => m.prop)' arrow function
272
+ const dynamicImport = ts.factory.createArrowFunction(undefined, // modifiers
273
+ undefined, // type parameters
274
+ [], // parameters
275
+ undefined, // type
276
+ ts.factory.createToken(ts.SyntaxKind.EqualsGreaterThanToken), body);
277
+ return dynamicImport;
278
+ }
279
+ /**
280
+ * Converts a string or string array union to array.
281
+ * @param value The value to convert.
282
+ * @param variables Variables to replace in the strings.
283
+ * @remarks Splits strings as comma delimited.
284
+ */
285
+ asArray(value, variables) {
286
+ let result = [];
287
+ if (value) {
288
+ result = typeof value === 'string' ? value.split(/\s*,\s*/) : value;
289
+ result = result.map((x) => this.applyConfigTransformation(x, variables));
290
+ }
291
+ return result;
292
+ }
293
+ /**
294
+ * Adds a new entry to the routes array. With a component identifier and path or with redirect option.
295
+ * @param route The route to add.
296
+ * @param visitCondition The condition used to find the appropriate route node.
297
+ * @param anchorElement The anchor element to insert.
298
+ * @param multiline Whether to format the new entry as multiline.
299
+ * @param prepend Whether to insert the new entry before the anchor element.
300
+ */
301
+ addRedirectOrSimpleRouteEntry(route, visitCondition, anchorElement, multiline = false, prepend = false) {
302
+ var _a;
303
+ if (route.lazyload)
304
+ return this.astTransformer.sourceFile;
305
+ if (route.path && route.identifierName) {
306
+ this.addRouteEntry(route, visitCondition, anchorElement, multiline, prepend);
307
+ }
308
+ if (route.redirectTo) {
309
+ this.addRedirectRouteEntry(route, visitCondition, anchorElement, multiline, prepend);
310
+ }
311
+ if ((_a = route.children) === null || _a === void 0 ? void 0 : _a.length) {
312
+ route.children.forEach((child) => {
313
+ this.addChildRoute(route.path, child, multiline, false);
314
+ });
315
+ }
316
+ return this.astTransformer.sourceFile;
317
+ }
318
+ /**
319
+ * Adds a new not lazy-loaded route entry to the routes array. With a component identifier and path.
320
+ * @param route The route to add.
321
+ * @param visitCondition The condition used to find the appropriate route node.
322
+ * @param anchorElement The anchor element to insert to.
323
+ * @param multiline Whether to format the new entry as multiline.
324
+ * @param prepend Whether to insert the new entry before the anchor element.
325
+ */
326
+ addRouteEntry(route, visitCondition, anchorElement, multiline = false, prepend = false) {
327
+ if (route.modulePath) {
328
+ // add an import for the given identifier
329
+ const routeIdentifier = { name: route.identifierName };
330
+ if (!this.astTransformer.importDeclarationCollides(routeIdentifier)) {
331
+ // if identifierName, there must be a modulePath as well
332
+ this.astTransformer.addImportDeclaration([routeIdentifier], route.modulePath);
333
+ }
334
+ }
335
+ const structure = [
336
+ {
337
+ name: cli_core_1.RouteTarget.Path,
338
+ value: ts.factory.createStringLiteral(route.path),
339
+ },
340
+ {
341
+ name: cli_core_1.RouteTarget.Component,
342
+ value: ts.factory.createIdentifier(route.identifierName),
343
+ },
344
+ ];
345
+ if (route.data) {
346
+ structure.push({
347
+ name: types_1.AngularRouteTarget.Data,
348
+ value: this.astTransformer.createObjectLiteralExpression([route.data], multiline, ts.factory.createStringLiteral),
349
+ });
350
+ }
351
+ const newRoute = this.astTransformer.createObjectLiteralExpression(structure, multiline);
352
+ return this.astTransformer.addMembersToArrayLiteral(visitCondition, [newRoute], prepend, anchorElement);
353
+ }
354
+ /**
355
+ * Adds a new not lazy-loaded route entry to the routes array. With a redirect option.
356
+ * @param route The route to add.
357
+ * @param visitCondition The condition used to find the appropriate route node.
358
+ * @param anchorElement The anchor element to insert to.
359
+ * @param multiline Whether to format the new entry as multiline.
360
+ * @param prepend Whether to insert the new entry before the anchor element.
361
+ */
362
+ addRedirectRouteEntry(route, visitCondition, anchorElement, multiline = false, prepend = false) {
363
+ const structure = [
364
+ {
365
+ name: cli_core_1.RouteTarget.Path,
366
+ value: ts.factory.createStringLiteral(route.path || ''),
367
+ },
368
+ {
369
+ name: types_1.AngularRouteTarget.RedirectTo,
370
+ value: ts.factory.createStringLiteral(route.redirectTo),
371
+ },
372
+ ];
373
+ if (route.pathMatch) {
374
+ structure.push({
375
+ name: types_1.AngularRouteTarget.PathMatch,
376
+ value: ts.factory.createStringLiteral(route.pathMatch),
377
+ });
378
+ }
379
+ const newRoute = this.astTransformer.createObjectLiteralExpression(structure, multiline);
380
+ return this.astTransformer.addMembersToArrayLiteral(visitCondition, [newRoute], prepend, anchorElement);
381
+ }
382
+ /**
383
+ * Adds a new lazy-loaded route entry to the routes array.
384
+ * @param route The route to add.
385
+ * @param visitCondition The condition used to find the appropriate route node.
386
+ * @param anchorElement The anchor element to insert to.
387
+ * @param multiline Whether to format the new entry as multiline.
388
+ * @param prepend Whether to insert the new entry before the anchor element.
389
+ */
390
+ addLazyLoadedRouteEntry(route, visitCondition, anchorElement, multiline = false, prepend = false) {
391
+ const lazyLoadedModule = this.createDynamicImport(route.modulePath, route.identifierName, route.root);
392
+ const propAssignmentName = route.root
393
+ ? types_1.AngularRouteTarget.LoadChildren
394
+ : types_1.AngularRouteTarget.LoadComponent;
395
+ const structure = [
396
+ {
397
+ name: cli_core_1.RouteTarget.Path,
398
+ value: ts.factory.createStringLiteral(route.path),
399
+ },
400
+ { name: propAssignmentName, value: lazyLoadedModule },
401
+ ];
402
+ if (route.data) {
403
+ structure.push({
404
+ name: types_1.AngularRouteTarget.Data,
405
+ value: this.astTransformer.createObjectLiteralExpression([route.data], multiline, ts.factory.createStringLiteral),
406
+ });
407
+ }
408
+ const newRoute = this.astTransformer.createObjectLiteralExpression(structure, multiline);
409
+ return this.astTransformer.addMembersToArrayLiteral(visitCondition, [newRoute], prepend, anchorElement);
410
+ }
411
+ }
412
+ exports.AngularTypeScriptFileUpdate = AngularTypeScriptFileUpdate;
@@ -1,9 +1,10 @@
1
- import { AddTemplateArgs, ControlExtraConfiguration, Template, TemplateDependency, TypeScriptFileUpdate } from "@igniteui/cli-core";
1
+ import { AddTemplateArgs, ControlExtraConfiguration, Template, TemplateDependency } from "@igniteui/cli-core";
2
2
  export declare class IgniteUIForAngularTemplate implements Template {
3
3
  private rootPath;
4
4
  components: string[];
5
5
  controlGroup: string;
6
6
  listInComponentTemplates: boolean;
7
+ addAsNgModelDeclaration: boolean;
7
8
  listInCustomTemplates: boolean;
8
9
  id: string;
9
10
  name: string;
@@ -32,7 +33,6 @@ export declare class IgniteUIForAngularTemplate implements Template {
32
33
  getExtraConfiguration(): ControlExtraConfiguration[];
33
34
  setExtraConfiguration(extraConfigKeys: {}): void;
34
35
  fileExists(filePath: string): boolean;
35
- protected addClassDeclaration(mainModule: TypeScriptFileUpdate, projPath: string, name: string, modulePath: string): void;
36
36
  protected getBaseVariables(name: string): {
37
37
  [key: string]: string;
38
38
  };
@@ -10,6 +10,7 @@ class IgniteUIForAngularTemplate {
10
10
  constructor(rootPath) {
11
11
  this.rootPath = rootPath;
12
12
  this.listInComponentTemplates = true;
13
+ this.addAsNgModelDeclaration = true;
13
14
  this.listInCustomTemplates = false;
14
15
  this.framework = "angular";
15
16
  this.projectType = "igx-ts";
@@ -46,32 +47,46 @@ class IgniteUIForAngularTemplate {
46
47
  // tslint:disable-next-line:variable-name
47
48
  const TsUpdate =
48
49
  // tslint:disable-next-line:no-submodule-imports
49
- require("@igniteui/cli-core/typescript").TypeScriptFileUpdate;
50
- // standalone components
50
+ require("@igniteui/angular-templates").AngularTypeScriptFileUpdate;
51
51
  const mainModulePath = path.join(projectPath, `src/app/${modulePath}`);
52
+ const folderName = this.folderName(name);
53
+ const fileName = this.fileName(name);
54
+ const componentFilePath = path.join(projectPath, `src/app/${folderName}/${fileName}.component.ts`);
55
+ const className = `${cli_core_1.Util.className(cli_core_1.Util.nameFromPath(name))}Component`;
56
+ // standalone components
52
57
  if (!this.fileExists(mainModulePath)) {
53
58
  const appRoutesPath = "src/app/app.routes.ts";
54
- const folderName = this.folderName(name);
55
- const fileName = this.fileName(name);
56
59
  if (!(options && options.skipRoute) && this.fileExists(appRoutesPath)) {
57
- const rountesConfig = new TsUpdate(path.join(projectPath, appRoutesPath));
58
- rountesConfig.addRoute(path.join(projectPath, `src/app/${folderName}/${fileName}.component.ts`), this.fileName(name), //path
59
- cli_core_1.Util.nameFromPath(name) //text
60
- );
60
+ const routesConfigPath = path.join(projectPath, appRoutesPath);
61
+ const rountesConfig = new TsUpdate(routesConfigPath, true, { indentSize: 2 });
62
+ rountesConfig.addRoute({
63
+ modulePath: cli_core_1.Util.relativePath(routesConfigPath, componentFilePath, true, true),
64
+ path: this.fileName(name),
65
+ data: { text: cli_core_1.Util.nameFromPath(name) },
66
+ identifierName: className
67
+ });
68
+ const content = rountesConfig.finalize();
69
+ if (content) {
70
+ // add to a finalize override in the NG File Update instead?
71
+ cli_core_1.TypeScriptUtils.saveFile(routesConfigPath, content);
72
+ }
61
73
  }
62
- const componentFile = new TsUpdate(path.join(projectPath, `src/app/${folderName}/${fileName}.component.ts`));
74
+ const componentFile = new TsUpdate(componentFilePath, true, { indentSize: 2 });
63
75
  for (const dep of this.dependencies) {
64
76
  if (dep.from && dep.from.startsWith(".")) {
65
77
  // relative file dependency
66
78
  const copy = Object.assign({}, dep);
67
- copy.from = cli_core_1.Util.relativePath(path.join(projectPath, `src/app/${folderName}/${fileName}.component.ts`), path.join(projectPath, copy.from), true, true);
68
- // can use addNgModuleMeta here instead?
69
- componentFile.addStandaloneImport(copy, cli_core_1.Util.applyDelimiters(this.getBaseVariables(name), this.delimiters.content));
79
+ copy.from = cli_core_1.Util.relativePath(componentFilePath, path.join(projectPath, copy.from), true, true);
80
+ componentFile.addStandaloneComponentMeta(dep, cli_core_1.Util.applyDelimiters(this.getBaseVariables(name), this.delimiters.content));
70
81
  continue;
71
82
  }
72
- componentFile.addStandaloneImport(dep, cli_core_1.Util.applyDelimiters(this.getBaseVariables(name), this.delimiters.content));
83
+ componentFile.addStandaloneComponentMeta(dep, cli_core_1.Util.applyDelimiters(this.getBaseVariables(name), this.delimiters.content));
84
+ }
85
+ const content = componentFile.finalize();
86
+ if (content) {
87
+ // add to a finalize override in the NG File Update instead?
88
+ cli_core_1.TypeScriptUtils.saveFile(componentFilePath, content);
73
89
  }
74
- componentFile.finalize();
75
90
  return;
76
91
  }
77
92
  // ngModule based components
@@ -79,15 +94,28 @@ class IgniteUIForAngularTemplate {
79
94
  //1) import the component class name,
80
95
  //2) and populate the Routes array with the path and component
81
96
  //for example: { path: 'combo', component: ComboComponent }
82
- const routingModule = new TsUpdate(path.join(projectPath, "src/app/app-routing.module.ts"));
83
- routingModule.addRoute(path.join(projectPath, `src/app/${this.folderName(name)}/${this.fileName(name)}.component.ts`), this.fileName(name), //path
84
- cli_core_1.Util.nameFromPath(name) //text
85
- );
97
+ const routingModulePath = path.join(projectPath, "src/app/app-routing.module.ts");
98
+ const routingModule = new TsUpdate(routingModulePath, false, { indentSize: 2 });
99
+ routingModule.addRoute({
100
+ modulePath: cli_core_1.Util.relativePath(routingModulePath, componentFilePath, true, true),
101
+ path: this.fileName(name),
102
+ data: { text: cli_core_1.Util.nameFromPath(name) },
103
+ identifierName: className
104
+ });
105
+ const content = routingModule.finalize();
106
+ if (content) {
107
+ // add to a finalize override in the NG File Update instead?
108
+ cli_core_1.TypeScriptUtils.saveFile(routingModulePath, content);
109
+ }
86
110
  }
87
111
  //3) add an import of the component class from its file location.
88
112
  //4) populate the declarations portion of the @NgModule with the component class name.
89
- const mainModule = new TsUpdate(mainModulePath);
90
- this.addClassDeclaration(mainModule, projectPath, name, modulePath);
113
+ const mainModule = new TsUpdate(mainModulePath, false, { indentSize: 2 });
114
+ mainModule.addNgModuleMeta({
115
+ declare: this.addAsNgModelDeclaration ? [className] : [],
116
+ from: cli_core_1.Util.relativePath(mainModulePath, componentFilePath, true, true),
117
+ export: modulePath !== "app.module.ts" ? [className] : []
118
+ });
91
119
  // import IgxModules and other dependencies
92
120
  for (const dep of this.dependencies) {
93
121
  if (dep.from && dep.from.startsWith(".")) {
@@ -100,7 +128,11 @@ class IgniteUIForAngularTemplate {
100
128
  mainModule.addNgModuleMeta(dep, cli_core_1.Util.applyDelimiters(this.getBaseVariables(name), this.delimiters.content));
101
129
  }
102
130
  }
103
- mainModule.finalize();
131
+ const content = mainModule.finalize();
132
+ if (content) {
133
+ // add to a finalize override in the NG File Update instead?
134
+ cli_core_1.TypeScriptUtils.saveFile(mainModulePath, content);
135
+ }
104
136
  }
105
137
  getExtraConfiguration() {
106
138
  return [];
@@ -109,9 +141,6 @@ class IgniteUIForAngularTemplate {
109
141
  fileExists(filePath) {
110
142
  return cli_core_1.App.container.get(cli_core_1.FS_TOKEN).fileExists(filePath);
111
143
  }
112
- addClassDeclaration(mainModule, projPath, name, modulePath) {
113
- mainModule.addDeclaration(path.join(projPath, `src/app/${this.folderName(name)}/${this.fileName(name)}.component.ts`), modulePath !== "app.module.ts");
114
- }
115
144
  getBaseVariables(name) {
116
145
  const config = {};
117
146
  config["name"] = cli_core_1.Util.nameFromPath(name);
@@ -15,8 +15,5 @@ class IgcDockManagerTemplate extends IgniteUIForAngularTemplate_1.IgniteUIForAng
15
15
  // "igniteui-dockmanager@~1.0.0":
16
16
  this.packages = [`${(0, cli_core_1.resolvePackage)(cli_core_1.NPM_DOCK_MANAGER)}@~1.8.0`];
17
17
  }
18
- addClassDeclaration(mainModule, projPath, name, modulePath) {
19
- // not applicable with custom module
20
- }
21
18
  }
22
19
  module.exports = new IgcDockManagerTemplate();
@@ -95,7 +95,7 @@ class IgxCustomGridTemplate extends IgniteUIForAngularTemplate_1.IgniteUIForAngu
95
95
  checkBoxBind = `[ngModel]="cell.value" (ngModelChange)="cell.update($event)"`;
96
96
  // enable Date Picker, ngModel
97
97
  // this.dependencies.push({ import: "IgxDatePickerModule", from: "<%=igxPackage%>" });
98
- this.dependencies.push({ import: "FormsModule", from: "@angular/forms" });
98
+ this.dependencies.push({ import: "FormsModule", from: "@angular/forms", standalone: true });
99
99
  // datePickerEditor = EOL +
100
100
  // `<ng-template igxCellEditor let-cell="cell">` + EOL +
101
101
  // ` <igx-date-picker cancelButtonLabel="cancel" todayButtonLabel="today" [(ngModel)]="cell.value"` +
@@ -107,7 +107,7 @@ class IgxCustomGridTemplate extends IgniteUIForAngularTemplate_1.IgniteUIForAngu
107
107
  break;
108
108
  case "Row Editing":
109
109
  gridFeatures.push(`[rowEditable]="true"`);
110
- this.dependencies.push({ import: "IgxDatePickerComponent", from: "<%=igxPackage%>" });
110
+ this.dependencies.push({ import: "IgxDatePickerComponent", from: "<%=igxPackage%>", standalone: true });
111
111
  break;
112
112
  case "Row Selection":
113
113
  const gridFeatureText = `rowSelection="multiple"`;
@@ -111,7 +111,7 @@ class IgxHierarchicalGridTemplate extends IgniteUIForAngularTemplate_1.IgniteUIF
111
111
  break;
112
112
  case "Cell Editing":
113
113
  columnFeatures.push(`[editable]="true"`);
114
- this.dependencies.push({ import: "FormsModule", from: "@angular/forms" });
114
+ this.dependencies.push({ import: "FormsModule", from: "@angular/forms", standalone: true });
115
115
  break;
116
116
  case "Row Selection":
117
117
  const gridFeatureText = `rowSelection="multiple"`;
@@ -19,7 +19,7 @@ jobs:
19
19
 
20
20
  strategy:
21
21
  matrix:
22
- node-version: [16.x, 18.x]
22
+ node-version: [18.x, 20.x]
23
23
  # See supported Node.js release schedule at https://nodejs.org/en/about/releases/
24
24
 
25
25
  steps:
@@ -99,7 +99,12 @@
99
99
  "styles": [
100
100
  "src/styles.scss"
101
101
  ],
102
- "scripts": []
102
+ "scripts": [],
103
+ "stylePreprocessorOptions": {
104
+ "includePaths": [
105
+ "node_modules"
106
+ ]
107
+ }
103
108
  }
104
109
  }
105
110
  }
@@ -19,7 +19,7 @@ jobs:
19
19
 
20
20
  strategy:
21
21
  matrix:
22
- node-version: [16.x, 18.x]
22
+ node-version: [18.x, 20.x]
23
23
  # See supported Node.js release schedule at https://nodejs.org/en/about/releases/
24
24
 
25
25
  steps:
@@ -8,6 +8,7 @@ class IgcDockManagerTemplate extends IgniteUIForAngularTemplate_1.IgniteUIForAng
8
8
  this.components = ["Dock Manager"];
9
9
  this.controlGroup = "Layouts";
10
10
  this.listInComponentTemplates = true;
11
+ this.addAsNgModelDeclaration = false;
11
12
  this.id = "dock-manager";
12
13
  this.projectType = "igx-ts";
13
14
  this.name = "Dock Manager";
@@ -18,8 +19,5 @@ class IgcDockManagerTemplate extends IgniteUIForAngularTemplate_1.IgniteUIForAng
18
19
  // "igniteui-dockmanager@~1.0.0":
19
20
  this.packages = [`${(0, cli_core_1.resolvePackage)(cli_core_1.NPM_DOCK_MANAGER)}@~1.8.0`];
20
21
  }
21
- addClassDeclaration(mainModule, projPath, name, modulePath) {
22
- // not applicable with custom module
23
- }
24
22
  }
25
23
  module.exports = new IgcDockManagerTemplate();
@@ -19,7 +19,7 @@ jobs:
19
19
 
20
20
  strategy:
21
21
  matrix:
22
- node-version: [16.x, 18.x]
22
+ node-version: [18.x, 20.x]
23
23
  # See supported Node.js release schedule at https://nodejs.org/en/about/releases/
24
24
 
25
25
  steps:
@@ -19,7 +19,7 @@ jobs:
19
19
 
20
20
  strategy:
21
21
  matrix:
22
- node-version: [16.x, 18.x]
22
+ node-version: [18.x, 20.x]
23
23
  # See supported Node.js release schedule at https://nodejs.org/en/about/releases/
24
24
 
25
25
  steps:
package/index.d.ts CHANGED
@@ -1,4 +1,6 @@
1
- import * as standalone from "./igx-ts";
2
- export * from "./IgniteUIForAngularTemplate";
1
+ import * as standalone from './igx-ts';
2
+ export * from './IgniteUIForAngularTemplate';
3
+ export * from './AngularTypeScriptFileUpdate';
4
+ export * from './types';
3
5
  declare const _default: (typeof standalone)[];
4
6
  export default _default;
package/index.js CHANGED
@@ -17,4 +17,6 @@ Object.defineProperty(exports, "__esModule", { value: true });
17
17
  const standalone = require("./igx-ts");
18
18
  const legacy = require("./igx-ts-legacy");
19
19
  __exportStar(require("./IgniteUIForAngularTemplate"), exports);
20
+ __exportStar(require("./AngularTypeScriptFileUpdate"), exports);
21
+ __exportStar(require("./types"), exports);
20
22
  exports.default = [standalone, legacy];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@igniteui/angular-templates",
3
- "version": "17.1.13115",
3
+ "version": "17.1.13116-beta.2",
4
4
  "description": "Templates for Ignite UI for Angular projects and components",
5
5
  "repository": {
6
6
  "type": "git",
@@ -12,7 +12,7 @@
12
12
  "author": "Infragistics",
13
13
  "license": "MIT",
14
14
  "dependencies": {
15
- "@igniteui/cli-core": "~13.1.15",
15
+ "@igniteui/cli-core": "~13.1.16-beta.2",
16
16
  "typescript": "~5.4.3"
17
17
  }
18
18
  }
@@ -0,0 +1,5 @@
1
+ import { IRouteEntry, RouteTarget } from "@igniteui/cli-core";
2
+ import { AngularRouteTarget } from "./enumerations/AngularRouteTarget";
3
+ export interface IAngularRouteEntry extends Pick<IRouteEntry, 'value'> {
4
+ name: RouteTarget | AngularRouteTarget;
5
+ }
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,8 @@
1
+ import { IRouteLike, KeyValuePair } from "@igniteui/cli-core";
2
+ export interface IAngularRouteLike extends IRouteLike {
3
+ pathMatch?: 'prefix' | 'full';
4
+ root?: boolean;
5
+ data?: KeyValuePair<string>;
6
+ redirectTo?: string;
7
+ children?: IAngularRouteLike[];
8
+ }
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,6 @@
1
+ export declare enum AngularDecoratorMetaTarget {
2
+ Imports = "imports",
3
+ Declarations = "declarations",
4
+ Providers = "providers",
5
+ Exports = "exports"
6
+ }
@@ -0,0 +1,10 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.AngularDecoratorMetaTarget = void 0;
4
+ var AngularDecoratorMetaTarget;
5
+ (function (AngularDecoratorMetaTarget) {
6
+ AngularDecoratorMetaTarget["Imports"] = "imports";
7
+ AngularDecoratorMetaTarget["Declarations"] = "declarations";
8
+ AngularDecoratorMetaTarget["Providers"] = "providers";
9
+ AngularDecoratorMetaTarget["Exports"] = "exports";
10
+ })(AngularDecoratorMetaTarget || (exports.AngularDecoratorMetaTarget = AngularDecoratorMetaTarget = {}));
@@ -0,0 +1,8 @@
1
+ export declare enum AngularRouteTarget {
2
+ PathMatch = "pathMatch",
3
+ Root = "root",
4
+ Data = "data",
5
+ RedirectTo = "redirectTo",
6
+ LoadChildren = "loadChildren",
7
+ LoadComponent = "loadComponent"
8
+ }
@@ -0,0 +1,12 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.AngularRouteTarget = void 0;
4
+ var AngularRouteTarget;
5
+ (function (AngularRouteTarget) {
6
+ AngularRouteTarget["PathMatch"] = "pathMatch";
7
+ AngularRouteTarget["Root"] = "root";
8
+ AngularRouteTarget["Data"] = "data";
9
+ AngularRouteTarget["RedirectTo"] = "redirectTo";
10
+ AngularRouteTarget["LoadChildren"] = "loadChildren";
11
+ AngularRouteTarget["LoadComponent"] = "loadComponent";
12
+ })(AngularRouteTarget || (exports.AngularRouteTarget = AngularRouteTarget = {}));
@@ -0,0 +1,4 @@
1
+ export * from './enumerations/AngularDecoratorMetaTarget';
2
+ export * from './enumerations/AngularRouteTarget';
3
+ export * from './IAngularRouteEntry';
4
+ export * from './IAngularRouteLike';
package/types/index.js ADDED
@@ -0,0 +1,20 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./enumerations/AngularDecoratorMetaTarget"), exports);
18
+ __exportStar(require("./enumerations/AngularRouteTarget"), exports);
19
+ __exportStar(require("./IAngularRouteEntry"), exports);
20
+ __exportStar(require("./IAngularRouteLike"), exports);