@magic-xpa/cli 4.1100.0-dev4110.20 → 4.1100.0-dev4110.203

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.
Files changed (158) hide show
  1. package/index.d.ts +1 -1
  2. package/index.js +4 -4
  3. package/package.json +2 -2
  4. package/src/public_api.d.ts +3 -3
  5. package/src/public_api.js +6 -6
  6. package/src/schematics/magic-utils/Util.d.ts +6 -6
  7. package/src/schematics/magic-utils/Util.js +26 -26
  8. package/src/schematics/magic-utils/config.d.ts +12 -12
  9. package/src/schematics/magic-utils/config.js +56 -56
  10. package/src/schematics/magic-utils/env.d.ts +23 -23
  11. package/src/schematics/magic-utils/env.js +39 -39
  12. package/src/schematics/magic-utils/env.js.map +1 -1
  13. package/src/schematics/magic-utils/env.ts +5 -5
  14. package/src/schematics/magic-utils/manifest.d.ts +11 -11
  15. package/src/schematics/magic-utils/manifest.js +40 -40
  16. package/src/schematics/magic-utils/manifest.js.map +1 -1
  17. package/src/schematics/magic-utils/metadata.class.d.ts +51 -51
  18. package/src/schematics/magic-utils/metadata.class.js +124 -124
  19. package/src/schematics/magic-utils/metadata.class.js.map +1 -1
  20. package/src/schematics/magic-utils/process-json.d.ts +2 -2
  21. package/src/schematics/magic-utils/process-json.js +158 -158
  22. package/src/schematics/magic-utils/process-json.js.map +1 -1
  23. package/src/schematics/magic-utils/rules/build-control.rule.d.ts +5 -5
  24. package/src/schematics/magic-utils/rules/build-control.rule.js +74 -74
  25. package/src/schematics/magic-utils/rules/build-control.rule.js.map +1 -1
  26. package/src/schematics/magic-utils/rules/build-view.rule.d.ts +3 -3
  27. package/src/schematics/magic-utils/rules/build-view.rule.js +83 -80
  28. package/src/schematics/magic-utils/rules/build-view.rule.js.map +1 -1
  29. package/src/schematics/magic-utils/rules/build-view.rule.ts +4 -0
  30. package/src/schematics/magic-utils/rules/component-list.rule.d.ts +3 -3
  31. package/src/schematics/magic-utils/rules/component-list.rule.js +317 -317
  32. package/src/schematics/magic-utils/rules/component-list.rule.js.map +1 -1
  33. package/src/schematics/magic-utils/rules/generate.rule.d.ts +5 -5
  34. package/src/schematics/magic-utils/rules/generate.rule.js +54 -54
  35. package/src/schematics/magic-utils/rules/generate.rule.js.map +1 -1
  36. package/src/schematics/magic-utils/rules/init-add-modules.rule.d.ts +12 -12
  37. package/src/schematics/magic-utils/rules/init-add-modules.rule.js +296 -296
  38. package/src/schematics/magic-utils/rules/init-add-modules.rule.js.map +1 -1
  39. package/src/schematics/magic-utils/rules/init-gen-components.rule.d.ts +4 -4
  40. package/src/schematics/magic-utils/rules/init-gen-components.rule.js +66 -66
  41. package/src/schematics/magic-utils/rules/init-gen-components.rule.js.map +1 -1
  42. package/src/schematics/magic-utils/rules/init-magic-metadata.rule.d.ts +3 -3
  43. package/src/schematics/magic-utils/rules/init-magic-metadata.rule.js +18 -18
  44. package/src/schematics/magic-utils/rules/init-magic-metadata.rule.js.map +1 -1
  45. package/src/schematics/magic-utils/rules/load-views-gen.rule.d.ts +3 -3
  46. package/src/schematics/magic-utils/rules/load-views-gen.rule.js +29 -29
  47. package/src/schematics/magic-utils/rules/magic-option.scheme.d.ts +22 -21
  48. package/src/schematics/magic-utils/rules/magic-option.scheme.js +2 -2
  49. package/src/schematics/magic-utils/rules/magic-option.scheme.js.map +1 -1
  50. package/src/schematics/magic-utils/rules/magic-option.scheme.ts +1 -0
  51. package/src/schematics/magic-utils/utils.d.ts +12 -12
  52. package/src/schematics/magic-utils/utils.js +117 -117
  53. package/src/schematics/magic-utils/utils.js.map +1 -1
  54. package/src/schematics/mg-Install-prerequisites/index.d.ts +3 -3
  55. package/src/schematics/mg-Install-prerequisites/index.js +25 -25
  56. package/src/schematics/mg-Install-prerequisites/index.js.map +1 -1
  57. package/src/schematics/mg-add/files/src/app/magic/lazy-loader.service.ts +2 -1
  58. package/src/schematics/mg-add/files/src/app/magic/magic.gen.lib.module.ts +19 -19
  59. package/src/schematics/mg-add/files/src/magic-styles.css +2 -2
  60. package/src/schematics/mg-add/index.d.ts +3 -3
  61. package/src/schematics/mg-add/index.js +151 -151
  62. package/src/schematics/mg-add/index.js.map +1 -1
  63. package/src/schematics/mg-add/utils/add-declaration-to-root-module.d.ts +2 -2
  64. package/src/schematics/mg-add/utils/add-declaration-to-root-module.js +11 -11
  65. package/src/schematics/mg-add-tasks/index.d.ts +3 -3
  66. package/src/schematics/mg-add-tasks/index.js +30 -30
  67. package/src/schematics/mg-delete-files/index.d.ts +3 -3
  68. package/src/schematics/mg-delete-files/index.js +13 -13
  69. package/src/schematics/mg-gen-module/index.d.ts +3 -3
  70. package/src/schematics/mg-gen-module/index.js +15 -15
  71. package/src/schematics/mg-generate/index.d.ts +3 -3
  72. package/src/schematics/mg-generate/index.js +13 -13
  73. package/src/schematics/mg-generate-component/index.d.ts +3 -3
  74. package/src/schematics/mg-generate-component/index.js +35 -35
  75. package/src/schematics/mg-generate-component/index.js.map +1 -1
  76. package/src/schematics/mg-generate-component/schema.json +6 -0
  77. package/src/schematics/mg-install/index.d.ts +3 -3
  78. package/src/schematics/mg-install/index.js +30 -30
  79. package/src/schematics/mg-install/index.js.map +1 -1
  80. package/src/schematics/mg-routes/index.d.ts +6 -6
  81. package/src/schematics/mg-routes/index.js +63 -63
  82. package/src/schematics/mg-routes/index.js.map +1 -1
  83. package/src/schematics/mg-single-control/index.d.ts +3 -3
  84. package/src/schematics/mg-single-control/index.js +29 -29
  85. package/src/schematics/utils/ast.d.ts +21 -21
  86. package/src/schematics/utils/ast.js +91 -91
  87. package/src/schematics/utils/ast.js.map +1 -1
  88. package/src/schematics/utils/ast.ts +1 -1
  89. package/src/schematics/utils/devkit-utils/ast-utils.d.ts +87 -87
  90. package/src/schematics/utils/devkit-utils/ast-utils.js +490 -490
  91. package/src/schematics/utils/devkit-utils/ast-utils.js.map +1 -1
  92. package/src/schematics/utils/devkit-utils/ast-utils_old.d.ts +68 -68
  93. package/src/schematics/utils/devkit-utils/ast-utils_old.js +420 -420
  94. package/src/schematics/utils/devkit-utils/ast-utils_old.js.map +1 -1
  95. package/src/schematics/utils/devkit-utils/change.d.ts +66 -66
  96. package/src/schematics/utils/devkit-utils/change.js +94 -94
  97. package/src/schematics/utils/devkit-utils/change.js.map +1 -1
  98. package/src/schematics/utils/devkit-utils/component.d.ts +2 -2
  99. package/src/schematics/utils/devkit-utils/component.js +97 -97
  100. package/src/schematics/utils/devkit-utils/component.js.map +1 -1
  101. package/src/schematics/utils/devkit-utils/config.d.ts +78 -78
  102. package/src/schematics/utils/devkit-utils/config.js +52 -52
  103. package/src/schematics/utils/devkit-utils/config.js.map +1 -1
  104. package/src/schematics/utils/devkit-utils/find-module.d.ts +28 -28
  105. package/src/schematics/utils/devkit-utils/find-module.js +93 -93
  106. package/src/schematics/utils/devkit-utils/find-module.js.map +1 -1
  107. package/src/schematics/utils/devkit-utils/ng-ast-utils.d.ts +5 -5
  108. package/src/schematics/utils/devkit-utils/ng-ast-utils.js +74 -74
  109. package/src/schematics/utils/devkit-utils/ng-ast-utils.js.map +1 -1
  110. package/src/schematics/utils/devkit-utils/parse-name.d.ts +13 -13
  111. package/src/schematics/utils/devkit-utils/parse-name.js +20 -20
  112. package/src/schematics/utils/devkit-utils/route-utils.d.ts +20 -20
  113. package/src/schematics/utils/devkit-utils/route-utils.js +134 -134
  114. package/src/schematics/utils/devkit-utils/route-utils.js.map +1 -1
  115. package/src/schematics/utils/devkit-utils/validation.d.ts +1 -1
  116. package/src/schematics/utils/devkit-utils/validation.js +18 -18
  117. package/src/schematics/utils/devkit-utils/validation.js.map +1 -1
  118. package/src/schematics/utils/html.d.ts +18 -18
  119. package/src/schematics/utils/html.js +60 -60
  120. package/src/schematics/utils/html.js.map +1 -1
  121. package/src/schematics/utils/lib-versions.d.ts +3 -3
  122. package/src/schematics/utils/lib-versions.js +6 -6
  123. package/src/schematics/utils/package.d.ts +5 -5
  124. package/src/schematics/utils/package.js +21 -21
  125. package/src/schematics/utils/package.js.map +1 -1
  126. package/src/schematics/utils/testing.d.ts +5 -5
  127. package/src/schematics/utils/testing.js +27 -27
  128. package/src/schematics/utils/testing.js.map +1 -1
  129. package/src/schematics/utils/testing.ts +2 -2
  130. package/src/types/enums/Enums.d.ts +1646 -1646
  131. package/src/types/enums/Enums.js +1862 -1862
  132. package/src/types/enums/Enums.js.map +1 -1
  133. package/src/types/enums/generated-file-types.enum.d.ts +6 -6
  134. package/src/types/enums/generated-file-types.enum.js +10 -10
  135. package/src/types/enums/generated-file-types.enum.js.map +1 -1
  136. package/src/types/enums/horizontal-alignments.enum.d.ts +5 -5
  137. package/src/types/enums/horizontal-alignments.enum.js +9 -9
  138. package/src/types/enums/horizontal-alignments.enum.js.map +1 -1
  139. package/src/types/enums/themes.enum.d.ts +6 -6
  140. package/src/types/enums/themes.enum.js +10 -10
  141. package/src/types/enums/themes.enum.js.map +1 -1
  142. package/src/types/index.d.ts +7 -7
  143. package/src/types/index.js +10 -10
  144. package/src/types/interfaces/app.d.ts +53 -53
  145. package/src/types/interfaces/app.js +9 -9
  146. package/src/types/interfaces/control.d.ts +82 -82
  147. package/src/types/interfaces/control.js +2 -2
  148. package/src/types/interfaces/env.interface.d.ts +19 -19
  149. package/src/types/interfaces/env.interface.js +10 -10
  150. package/src/types/interfaces/template.config.d.ts +8 -8
  151. package/src/types/interfaces/template.config.js +2 -2
  152. package/templates/themes/basicHTML/checkbox.ejs +1 -1
  153. package/templates/themes/basicHTML/edit.ejs +2 -1
  154. package/templates/themes/basicHTML/label.ejs +1 -1
  155. package/templates/themes/material/checkbox.ejs +1 -1
  156. package/templates/themes/material/listbox.ejs +0 -1
  157. package/templates/themes/table/HTMLTable/columnHeader.ejs +3 -1
  158. package/templates/themes/table/grid/column.ejs +3 -1
@@ -1,421 +1,421 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.isImported = exports.addBootstrapToModule = exports.addExportToModule = exports.addProviderToModule = exports.addImportToModule = exports.addDeclarationToModule = exports.addSymbolToNgModuleMetadata = exports.getFirstNgModuleName = exports.getDecoratorMetadata = exports.getContentOfKeyLiteral = exports.insertAfterLastOccurrence = exports.findNode = exports.getSourceNodes = exports.findNodes = void 0;
4
- /**
5
- * @license
6
- * Copyright Google Inc. All Rights Reserved.
7
- *
8
- * Use of this source code is governed by an MIT-style license that can be
9
- * found in the LICENSE file at https://angular.io/license
10
- */
11
- const ts = require("typescript");
12
- const change_1 = require("./change");
13
- const route_utils_1 = require("./route-utils");
14
- /**
15
- * Find all nodes from the AST in the subtree of node of SyntaxKind kind.
16
- * @param node
17
- * @param kind
18
- * @param max The maximum number of items to return.
19
- * @return all nodes of kind, or [] if none is found
20
- */
21
- function findNodes(node, kind, max = Infinity) {
22
- if (!node || max == 0) {
23
- return [];
24
- }
25
- const arr = [];
26
- if (node.kind === kind) {
27
- arr.push(node);
28
- max--;
29
- }
30
- if (max > 0) {
31
- for (const child of node.getChildren()) {
32
- findNodes(child, kind, max).forEach(node => {
33
- if (max > 0) {
34
- arr.push(node);
35
- }
36
- max--;
37
- });
38
- if (max <= 0) {
39
- break;
40
- }
41
- }
42
- }
43
- return arr;
44
- }
45
- exports.findNodes = findNodes;
46
- function getSourceNodes(sourceFile) {
47
- const nodes = [sourceFile];
48
- const result = [];
49
- while (nodes.length > 0) {
50
- const node = nodes.shift();
51
- if (node) {
52
- result.push(node);
53
- if (node.getChildCount(sourceFile) >= 0) {
54
- nodes.unshift(...node.getChildren());
55
- }
56
- }
57
- }
58
- return result;
59
- }
60
- exports.getSourceNodes = getSourceNodes;
61
- function findNode(node, kind, text) {
62
- if (node.kind === kind && node.getText() === text) {
63
- // throw new Error(node.getText());
64
- return node;
65
- }
66
- let foundNode = null;
67
- ts.forEachChild(node, childNode => {
68
- foundNode = foundNode || findNode(childNode, kind, text);
69
- });
70
- return foundNode;
71
- }
72
- exports.findNode = findNode;
73
- /**
74
- * Helper for sorting nodes.
75
- * @return function to sort nodes in increasing order of position in sourceFile
76
- */
77
- function nodesByPosition(first, second) {
78
- return first.getStart() - second.getStart();
79
- }
80
- /**
81
- * Insert `toInsert` after the last occurence of `ts.SyntaxKind[nodes[i].kind]`
82
- * or after the last of occurence of `syntaxKind` if the last occurence is a sub child
83
- * of ts.SyntaxKind[nodes[i].kind] and save the changes in file.
84
- *
85
- * @param nodes insert after the last occurence of nodes
86
- * @param toInsert string to insert
87
- * @param file file to insert changes into
88
- * @param fallbackPos position to insert if toInsert happens to be the first occurence
89
- * @param syntaxKind the ts.SyntaxKind of the subchildren to insert after
90
- * @return Change instance
91
- * @throw Error if toInsert is first occurence but fall back is not set
92
- */
93
- function insertAfterLastOccurrence(nodes, toInsert, file, fallbackPos, syntaxKind) {
94
- let lastItem = nodes.sort(nodesByPosition).pop();
95
- if (!lastItem) {
96
- //throw new Error();
97
- // No imports exist....
98
- new change_1.InsertChange(file, 0, toInsert);
99
- }
100
- if (syntaxKind) {
101
- lastItem = findNodes(lastItem, syntaxKind).sort(nodesByPosition).pop();
102
- }
103
- if (!lastItem && fallbackPos == undefined) {
104
- throw new Error(`tried to insert ${toInsert} as first occurence with no fallback position`);
105
- }
106
- const lastItemPosition = lastItem ? lastItem.getEnd() : fallbackPos;
107
- return new change_1.InsertChange(file, lastItemPosition, toInsert);
108
- }
109
- exports.insertAfterLastOccurrence = insertAfterLastOccurrence;
110
- function getContentOfKeyLiteral(_source, node) {
111
- if (node.kind == ts.SyntaxKind.Identifier) {
112
- return node.text;
113
- }
114
- else if (node.kind == ts.SyntaxKind.StringLiteral) {
115
- return node.text;
116
- }
117
- else {
118
- return null;
119
- }
120
- }
121
- exports.getContentOfKeyLiteral = getContentOfKeyLiteral;
122
- function _angularImportsFromNode(node, _sourceFile) {
123
- const ms = node.moduleSpecifier;
124
- let modulePath;
125
- switch (ms.kind) {
126
- case ts.SyntaxKind.StringLiteral:
127
- modulePath = ms.text;
128
- break;
129
- default:
130
- return {};
131
- }
132
- if (!modulePath.startsWith('@angular/')) {
133
- return {};
134
- }
135
- if (node.importClause) {
136
- if (node.importClause.name) {
137
- // This is of the form `import Name from 'path'`. Ignore.
138
- return {};
139
- }
140
- else if (node.importClause.namedBindings) {
141
- const nb = node.importClause.namedBindings;
142
- if (nb.kind == ts.SyntaxKind.NamespaceImport) {
143
- // This is of the form `import * as name from 'path'`. Return `name.`.
144
- return {
145
- [nb.name.text + '.']: modulePath,
146
- };
147
- }
148
- else {
149
- // This is of the form `import {a,b,c} from 'path'`
150
- const namedImports = nb;
151
- return namedImports.elements
152
- .map((is) => is.propertyName ? is.propertyName.text : is.name.text)
153
- .reduce((acc, curr) => {
154
- acc[curr] = modulePath;
155
- return acc;
156
- }, {});
157
- }
158
- }
159
- return {};
160
- }
161
- else {
162
- // This is of the form `import 'path';`. Nothing to do.
163
- return {};
164
- }
165
- }
166
- function getDecoratorMetadata(source, identifier, module) {
167
- const angularImports = findNodes(source, ts.SyntaxKind.ImportDeclaration)
168
- .map((node) => _angularImportsFromNode(node, source))
169
- .reduce((acc, current) => {
170
- for (const key of Object.keys(current)) {
171
- acc[key] = current[key];
172
- }
173
- return acc;
174
- }, {});
175
- return getSourceNodes(source)
176
- .filter(node => {
177
- return node.kind == ts.SyntaxKind.Decorator
178
- && node.expression.kind == ts.SyntaxKind.CallExpression;
179
- })
180
- .map(node => node.expression)
181
- .filter(expr => {
182
- if (expr.expression.kind == ts.SyntaxKind.Identifier) {
183
- const id = expr.expression;
184
- return id.getFullText(source) == identifier
185
- && angularImports[id.getFullText(source)] === module;
186
- }
187
- else if (expr.expression.kind == ts.SyntaxKind.PropertyAccessExpression) {
188
- // This covers foo.NgModule when importing * as foo.
189
- const paExpr = expr.expression;
190
- // If the left expression is not an identifier, just give up at that point.
191
- if (paExpr.expression.kind !== ts.SyntaxKind.Identifier) {
192
- return false;
193
- }
194
- const id = paExpr.name.text;
195
- const moduleId = paExpr.expression.getText(source);
196
- return id === identifier && (angularImports[moduleId + '.'] === module);
197
- }
198
- return false;
199
- })
200
- .filter(expr => expr.arguments[0]
201
- && expr.arguments[0].kind == ts.SyntaxKind.ObjectLiteralExpression)
202
- .map(expr => expr.arguments[0]);
203
- }
204
- exports.getDecoratorMetadata = getDecoratorMetadata;
205
- function findClassDeclarationParent(node) {
206
- if (ts.isClassDeclaration(node)) {
207
- return node;
208
- }
209
- return node.parent && findClassDeclarationParent(node.parent);
210
- }
211
- /**
212
- * Given a source file with @NgModule class(es), find the name of the first @NgModule class.
213
- *
214
- * @param source source file containing one or more @NgModule
215
- * @returns the name of the first @NgModule, or `undefined` if none is found
216
- */
217
- function getFirstNgModuleName(source) {
218
- // First, find the @NgModule decorators.
219
- const ngModulesMetadata = getDecoratorMetadata(source, 'NgModule', '@angular/core');
220
- if (ngModulesMetadata.length === 0) {
221
- return undefined;
222
- }
223
- // Then walk parent pointers up the AST, looking for the ClassDeclaration parent of the NgModule
224
- // metadata.
225
- const moduleClass = findClassDeclarationParent(ngModulesMetadata[0]);
226
- if (!moduleClass || !moduleClass.name) {
227
- return undefined;
228
- }
229
- // Get the class name of the module ClassDeclaration.
230
- return moduleClass.name.text;
231
- }
232
- exports.getFirstNgModuleName = getFirstNgModuleName;
233
- function addSymbolToNgModuleMetadata(source, ngModulePath, metadataField, symbolName, importPath = null) {
234
- const nodes = getDecoratorMetadata(source, 'NgModule', '@angular/core');
235
- let node = nodes[0]; // tslint:disable-line:no-any
236
- // Find the decorator declaration.
237
- if (!node) {
238
- return [];
239
- }
240
- // Get all the children property assignment of object literals.
241
- const matchingProperties = node.properties
242
- .filter(prop => prop.kind == ts.SyntaxKind.PropertyAssignment)
243
- // Filter out every fields that's not "metadataField". Also handles string literals
244
- // (but not expressions).
245
- .filter((prop) => {
246
- const name = prop.name;
247
- switch (name.kind) {
248
- case ts.SyntaxKind.Identifier:
249
- return name.getText(source) == metadataField;
250
- case ts.SyntaxKind.StringLiteral:
251
- return name.text == metadataField;
252
- }
253
- return false;
254
- });
255
- // Get the last node of the array literal.
256
- if (!matchingProperties) {
257
- return [];
258
- }
259
- if (matchingProperties.length == 0) {
260
- // We haven't found the field in the metadata declaration. Insert a new field.
261
- const expr = node;
262
- let position;
263
- let toInsert;
264
- if (expr.properties.length == 0) {
265
- position = expr.getEnd() - 1;
266
- toInsert = ` ${metadataField}: [${symbolName}]\n`;
267
- }
268
- else {
269
- node = expr.properties[expr.properties.length - 1];
270
- position = node.getEnd();
271
- // Get the indentation of the last element, if any.
272
- const text = node.getFullText(source);
273
- const matches = text.match(/^\r?\n\s*/);
274
- if (matches.length > 0) {
275
- toInsert = `,${matches[0]}${metadataField}: [${symbolName}]`;
276
- }
277
- else {
278
- toInsert = `, ${metadataField}: [${symbolName}]`;
279
- }
280
- }
281
- if (importPath !== null) {
282
- return [
283
- new change_1.InsertChange(ngModulePath, position, toInsert),
284
- (0, route_utils_1.insertImport)(source, ngModulePath, symbolName.replace(/\..*$/, ''), importPath),
285
- ];
286
- }
287
- else {
288
- return [new change_1.InsertChange(ngModulePath, position, toInsert)];
289
- }
290
- }
291
- const assignment = matchingProperties[0];
292
- // If it's not an array, nothing we can do really.
293
- if (assignment.initializer.kind !== ts.SyntaxKind.ArrayLiteralExpression) {
294
- return [];
295
- }
296
- const arrLiteral = assignment.initializer;
297
- if (arrLiteral.elements.length == 0) {
298
- // Forward the property.
299
- node = arrLiteral;
300
- }
301
- else {
302
- node = arrLiteral.elements;
303
- }
304
- if (!node) {
305
- console.log('No app module found. Please add your new class to your component.');
306
- return [];
307
- }
308
- if (Array.isArray(node)) {
309
- const nodeArray = node;
310
- const symbolsArray = nodeArray.map(node => node.getText());
311
- if (symbolsArray.includes(symbolName)) {
312
- return [];
313
- }
314
- node = node[node.length - 1];
315
- }
316
- let toInsert;
317
- let position = node.getEnd();
318
- if (node.kind == ts.SyntaxKind.ObjectLiteralExpression) {
319
- // We haven't found the field in the metadata declaration. Insert a new
320
- // field.
321
- const expr = node;
322
- if (expr.properties.length == 0) {
323
- position = expr.getEnd() - 1;
324
- toInsert = ` ${metadataField}: [${symbolName}]\n`;
325
- }
326
- else {
327
- node = expr.properties[expr.properties.length - 1];
328
- position = node.getEnd();
329
- // Get the indentation of the last element, if any.
330
- const text = node.getFullText(source);
331
- if (text.match('^\r?\r?\n')) {
332
- toInsert = `,${text.match(/^\r?\n\s+/)[0]}${metadataField}: [${symbolName}]`;
333
- }
334
- else {
335
- toInsert = `, ${metadataField}: [${symbolName}]`;
336
- }
337
- }
338
- }
339
- else if (node.kind == ts.SyntaxKind.ArrayLiteralExpression) {
340
- // We found the field but it's empty. Insert it just before the `]`.
341
- position--;
342
- toInsert = `${symbolName}`;
343
- }
344
- else {
345
- // Get the indentation of the last element, if any.
346
- const text = node.getFullText(source);
347
- if (text.match(/^\r?\n/)) {
348
- toInsert = `,${text.match(/^\r?\n(\r?)\s+/)[0]}${symbolName}`;
349
- }
350
- else {
351
- toInsert = `, ${symbolName}`;
352
- }
353
- }
354
- if (importPath !== null) {
355
- return [
356
- new change_1.InsertChange(ngModulePath, position, toInsert),
357
- (0, route_utils_1.insertImport)(source, ngModulePath, symbolName.replace(/\..*$/, ''), importPath),
358
- ];
359
- }
360
- return [new change_1.InsertChange(ngModulePath, position, toInsert)];
361
- }
362
- exports.addSymbolToNgModuleMetadata = addSymbolToNgModuleMetadata;
363
- /**
364
- * Custom function to insert a declaration (component, pipe, directive)
365
- * into NgModule declarations. It also imports the component.
366
- */
367
- function addDeclarationToModule(source, modulePath, classifiedName, importPath) {
368
- return addSymbolToNgModuleMetadata(source, modulePath, 'declarations', classifiedName, importPath);
369
- }
370
- exports.addDeclarationToModule = addDeclarationToModule;
371
- /**
372
- * Custom function to insert an NgModule into NgModule imports. It also imports the module.
373
- */
374
- function addImportToModule(source, modulePath, classifiedName, importPath) {
375
- return addSymbolToNgModuleMetadata(source, modulePath, 'imports', classifiedName, importPath);
376
- }
377
- exports.addImportToModule = addImportToModule;
378
- /**
379
- * Custom function to insert a provider into NgModule. It also imports it.
380
- */
381
- function addProviderToModule(source, modulePath, classifiedName, importPath) {
382
- return addSymbolToNgModuleMetadata(source, modulePath, 'providers', classifiedName, importPath);
383
- }
384
- exports.addProviderToModule = addProviderToModule;
385
- /**
386
- * Custom function to insert an export into NgModule. It also imports it.
387
- */
388
- function addExportToModule(source, modulePath, classifiedName, importPath) {
389
- return addSymbolToNgModuleMetadata(source, modulePath, 'exports', classifiedName, importPath);
390
- }
391
- exports.addExportToModule = addExportToModule;
392
- /**
393
- * Custom function to insert an export into NgModule. It also imports it.
394
- */
395
- function addBootstrapToModule(source, modulePath, classifiedName, importPath) {
396
- return addSymbolToNgModuleMetadata(source, modulePath, 'bootstrap', classifiedName, importPath);
397
- }
398
- exports.addBootstrapToModule = addBootstrapToModule;
399
- /**
400
- * Determine if an import already exists.
401
- */
402
- function isImported(source, classifiedName, importPath) {
403
- const allNodes = getSourceNodes(source);
404
- const matchingNodes = allNodes
405
- .filter(node => node.kind === ts.SyntaxKind.ImportDeclaration)
406
- .filter((imp) => imp.moduleSpecifier.kind === ts.SyntaxKind.StringLiteral)
407
- .filter((imp) => {
408
- return imp.moduleSpecifier.text === importPath;
409
- })
410
- .filter((imp) => {
411
- if (!imp.importClause) {
412
- return false;
413
- }
414
- const nodes = findNodes(imp.importClause, ts.SyntaxKind.ImportSpecifier)
415
- .filter(n => n.getText() === classifiedName);
416
- return nodes.length > 0;
417
- });
418
- return matchingNodes.length > 0;
419
- }
420
- exports.isImported = isImported;
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.isImported = exports.addBootstrapToModule = exports.addExportToModule = exports.addProviderToModule = exports.addImportToModule = exports.addDeclarationToModule = exports.addSymbolToNgModuleMetadata = exports.getFirstNgModuleName = exports.getDecoratorMetadata = exports.getContentOfKeyLiteral = exports.insertAfterLastOccurrence = exports.findNode = exports.getSourceNodes = exports.findNodes = void 0;
4
+ /**
5
+ * @license
6
+ * Copyright Google Inc. All Rights Reserved.
7
+ *
8
+ * Use of this source code is governed by an MIT-style license that can be
9
+ * found in the LICENSE file at https://angular.io/license
10
+ */
11
+ const ts = require("typescript");
12
+ const change_1 = require("./change");
13
+ const route_utils_1 = require("./route-utils");
14
+ /**
15
+ * Find all nodes from the AST in the subtree of node of SyntaxKind kind.
16
+ * @param node
17
+ * @param kind
18
+ * @param max The maximum number of items to return.
19
+ * @return all nodes of kind, or [] if none is found
20
+ */
21
+ function findNodes(node, kind, max = Infinity) {
22
+ if (!node || max == 0) {
23
+ return [];
24
+ }
25
+ const arr = [];
26
+ if (node.kind === kind) {
27
+ arr.push(node);
28
+ max--;
29
+ }
30
+ if (max > 0) {
31
+ for (const child of node.getChildren()) {
32
+ findNodes(child, kind, max).forEach(node => {
33
+ if (max > 0) {
34
+ arr.push(node);
35
+ }
36
+ max--;
37
+ });
38
+ if (max <= 0) {
39
+ break;
40
+ }
41
+ }
42
+ }
43
+ return arr;
44
+ }
45
+ exports.findNodes = findNodes;
46
+ function getSourceNodes(sourceFile) {
47
+ const nodes = [sourceFile];
48
+ const result = [];
49
+ while (nodes.length > 0) {
50
+ const node = nodes.shift();
51
+ if (node) {
52
+ result.push(node);
53
+ if (node.getChildCount(sourceFile) >= 0) {
54
+ nodes.unshift(...node.getChildren());
55
+ }
56
+ }
57
+ }
58
+ return result;
59
+ }
60
+ exports.getSourceNodes = getSourceNodes;
61
+ function findNode(node, kind, text) {
62
+ if (node.kind === kind && node.getText() === text) {
63
+ // throw new Error(node.getText());
64
+ return node;
65
+ }
66
+ let foundNode = null;
67
+ ts.forEachChild(node, childNode => {
68
+ foundNode = foundNode || findNode(childNode, kind, text);
69
+ });
70
+ return foundNode;
71
+ }
72
+ exports.findNode = findNode;
73
+ /**
74
+ * Helper for sorting nodes.
75
+ * @return function to sort nodes in increasing order of position in sourceFile
76
+ */
77
+ function nodesByPosition(first, second) {
78
+ return first.getStart() - second.getStart();
79
+ }
80
+ /**
81
+ * Insert `toInsert` after the last occurence of `ts.SyntaxKind[nodes[i].kind]`
82
+ * or after the last of occurence of `syntaxKind` if the last occurence is a sub child
83
+ * of ts.SyntaxKind[nodes[i].kind] and save the changes in file.
84
+ *
85
+ * @param nodes insert after the last occurence of nodes
86
+ * @param toInsert string to insert
87
+ * @param file file to insert changes into
88
+ * @param fallbackPos position to insert if toInsert happens to be the first occurence
89
+ * @param syntaxKind the ts.SyntaxKind of the subchildren to insert after
90
+ * @return Change instance
91
+ * @throw Error if toInsert is first occurence but fall back is not set
92
+ */
93
+ function insertAfterLastOccurrence(nodes, toInsert, file, fallbackPos, syntaxKind) {
94
+ let lastItem = nodes.sort(nodesByPosition).pop();
95
+ if (!lastItem) {
96
+ //throw new Error();
97
+ // No imports exist....
98
+ new change_1.InsertChange(file, 0, toInsert);
99
+ }
100
+ if (syntaxKind) {
101
+ lastItem = findNodes(lastItem, syntaxKind).sort(nodesByPosition).pop();
102
+ }
103
+ if (!lastItem && fallbackPos == undefined) {
104
+ throw new Error(`tried to insert ${toInsert} as first occurence with no fallback position`);
105
+ }
106
+ const lastItemPosition = lastItem ? lastItem.getEnd() : fallbackPos;
107
+ return new change_1.InsertChange(file, lastItemPosition, toInsert);
108
+ }
109
+ exports.insertAfterLastOccurrence = insertAfterLastOccurrence;
110
+ function getContentOfKeyLiteral(_source, node) {
111
+ if (node.kind == ts.SyntaxKind.Identifier) {
112
+ return node.text;
113
+ }
114
+ else if (node.kind == ts.SyntaxKind.StringLiteral) {
115
+ return node.text;
116
+ }
117
+ else {
118
+ return null;
119
+ }
120
+ }
121
+ exports.getContentOfKeyLiteral = getContentOfKeyLiteral;
122
+ function _angularImportsFromNode(node, _sourceFile) {
123
+ const ms = node.moduleSpecifier;
124
+ let modulePath;
125
+ switch (ms.kind) {
126
+ case ts.SyntaxKind.StringLiteral:
127
+ modulePath = ms.text;
128
+ break;
129
+ default:
130
+ return {};
131
+ }
132
+ if (!modulePath.startsWith('@angular/')) {
133
+ return {};
134
+ }
135
+ if (node.importClause) {
136
+ if (node.importClause.name) {
137
+ // This is of the form `import Name from 'path'`. Ignore.
138
+ return {};
139
+ }
140
+ else if (node.importClause.namedBindings) {
141
+ const nb = node.importClause.namedBindings;
142
+ if (nb.kind == ts.SyntaxKind.NamespaceImport) {
143
+ // This is of the form `import * as name from 'path'`. Return `name.`.
144
+ return {
145
+ [nb.name.text + '.']: modulePath,
146
+ };
147
+ }
148
+ else {
149
+ // This is of the form `import {a,b,c} from 'path'`
150
+ const namedImports = nb;
151
+ return namedImports.elements
152
+ .map((is) => is.propertyName ? is.propertyName.text : is.name.text)
153
+ .reduce((acc, curr) => {
154
+ acc[curr] = modulePath;
155
+ return acc;
156
+ }, {});
157
+ }
158
+ }
159
+ return {};
160
+ }
161
+ else {
162
+ // This is of the form `import 'path';`. Nothing to do.
163
+ return {};
164
+ }
165
+ }
166
+ function getDecoratorMetadata(source, identifier, module) {
167
+ const angularImports = findNodes(source, ts.SyntaxKind.ImportDeclaration)
168
+ .map((node) => _angularImportsFromNode(node, source))
169
+ .reduce((acc, current) => {
170
+ for (const key of Object.keys(current)) {
171
+ acc[key] = current[key];
172
+ }
173
+ return acc;
174
+ }, {});
175
+ return getSourceNodes(source)
176
+ .filter(node => {
177
+ return node.kind == ts.SyntaxKind.Decorator
178
+ && node.expression.kind == ts.SyntaxKind.CallExpression;
179
+ })
180
+ .map(node => node.expression)
181
+ .filter(expr => {
182
+ if (expr.expression.kind == ts.SyntaxKind.Identifier) {
183
+ const id = expr.expression;
184
+ return id.getFullText(source) == identifier
185
+ && angularImports[id.getFullText(source)] === module;
186
+ }
187
+ else if (expr.expression.kind == ts.SyntaxKind.PropertyAccessExpression) {
188
+ // This covers foo.NgModule when importing * as foo.
189
+ const paExpr = expr.expression;
190
+ // If the left expression is not an identifier, just give up at that point.
191
+ if (paExpr.expression.kind !== ts.SyntaxKind.Identifier) {
192
+ return false;
193
+ }
194
+ const id = paExpr.name.text;
195
+ const moduleId = paExpr.expression.getText(source);
196
+ return id === identifier && (angularImports[moduleId + '.'] === module);
197
+ }
198
+ return false;
199
+ })
200
+ .filter(expr => expr.arguments[0]
201
+ && expr.arguments[0].kind == ts.SyntaxKind.ObjectLiteralExpression)
202
+ .map(expr => expr.arguments[0]);
203
+ }
204
+ exports.getDecoratorMetadata = getDecoratorMetadata;
205
+ function findClassDeclarationParent(node) {
206
+ if (ts.isClassDeclaration(node)) {
207
+ return node;
208
+ }
209
+ return node.parent && findClassDeclarationParent(node.parent);
210
+ }
211
+ /**
212
+ * Given a source file with @NgModule class(es), find the name of the first @NgModule class.
213
+ *
214
+ * @param source source file containing one or more @NgModule
215
+ * @returns the name of the first @NgModule, or `undefined` if none is found
216
+ */
217
+ function getFirstNgModuleName(source) {
218
+ // First, find the @NgModule decorators.
219
+ const ngModulesMetadata = getDecoratorMetadata(source, 'NgModule', '@angular/core');
220
+ if (ngModulesMetadata.length === 0) {
221
+ return undefined;
222
+ }
223
+ // Then walk parent pointers up the AST, looking for the ClassDeclaration parent of the NgModule
224
+ // metadata.
225
+ const moduleClass = findClassDeclarationParent(ngModulesMetadata[0]);
226
+ if (!moduleClass || !moduleClass.name) {
227
+ return undefined;
228
+ }
229
+ // Get the class name of the module ClassDeclaration.
230
+ return moduleClass.name.text;
231
+ }
232
+ exports.getFirstNgModuleName = getFirstNgModuleName;
233
+ function addSymbolToNgModuleMetadata(source, ngModulePath, metadataField, symbolName, importPath = null) {
234
+ const nodes = getDecoratorMetadata(source, 'NgModule', '@angular/core');
235
+ let node = nodes[0]; // tslint:disable-line:no-any
236
+ // Find the decorator declaration.
237
+ if (!node) {
238
+ return [];
239
+ }
240
+ // Get all the children property assignment of object literals.
241
+ const matchingProperties = node.properties
242
+ .filter(prop => prop.kind == ts.SyntaxKind.PropertyAssignment)
243
+ // Filter out every fields that's not "metadataField". Also handles string literals
244
+ // (but not expressions).
245
+ .filter((prop) => {
246
+ const name = prop.name;
247
+ switch (name.kind) {
248
+ case ts.SyntaxKind.Identifier:
249
+ return name.getText(source) == metadataField;
250
+ case ts.SyntaxKind.StringLiteral:
251
+ return name.text == metadataField;
252
+ }
253
+ return false;
254
+ });
255
+ // Get the last node of the array literal.
256
+ if (!matchingProperties) {
257
+ return [];
258
+ }
259
+ if (matchingProperties.length == 0) {
260
+ // We haven't found the field in the metadata declaration. Insert a new field.
261
+ const expr = node;
262
+ let position;
263
+ let toInsert;
264
+ if (expr.properties.length == 0) {
265
+ position = expr.getEnd() - 1;
266
+ toInsert = ` ${metadataField}: [${symbolName}]\n`;
267
+ }
268
+ else {
269
+ node = expr.properties[expr.properties.length - 1];
270
+ position = node.getEnd();
271
+ // Get the indentation of the last element, if any.
272
+ const text = node.getFullText(source);
273
+ const matches = text.match(/^\r?\n\s*/);
274
+ if (matches.length > 0) {
275
+ toInsert = `,${matches[0]}${metadataField}: [${symbolName}]`;
276
+ }
277
+ else {
278
+ toInsert = `, ${metadataField}: [${symbolName}]`;
279
+ }
280
+ }
281
+ if (importPath !== null) {
282
+ return [
283
+ new change_1.InsertChange(ngModulePath, position, toInsert),
284
+ (0, route_utils_1.insertImport)(source, ngModulePath, symbolName.replace(/\..*$/, ''), importPath),
285
+ ];
286
+ }
287
+ else {
288
+ return [new change_1.InsertChange(ngModulePath, position, toInsert)];
289
+ }
290
+ }
291
+ const assignment = matchingProperties[0];
292
+ // If it's not an array, nothing we can do really.
293
+ if (assignment.initializer.kind !== ts.SyntaxKind.ArrayLiteralExpression) {
294
+ return [];
295
+ }
296
+ const arrLiteral = assignment.initializer;
297
+ if (arrLiteral.elements.length == 0) {
298
+ // Forward the property.
299
+ node = arrLiteral;
300
+ }
301
+ else {
302
+ node = arrLiteral.elements;
303
+ }
304
+ if (!node) {
305
+ console.log('No app module found. Please add your new class to your component.');
306
+ return [];
307
+ }
308
+ if (Array.isArray(node)) {
309
+ const nodeArray = node;
310
+ const symbolsArray = nodeArray.map(node => node.getText());
311
+ if (symbolsArray.includes(symbolName)) {
312
+ return [];
313
+ }
314
+ node = node[node.length - 1];
315
+ }
316
+ let toInsert;
317
+ let position = node.getEnd();
318
+ if (node.kind == ts.SyntaxKind.ObjectLiteralExpression) {
319
+ // We haven't found the field in the metadata declaration. Insert a new
320
+ // field.
321
+ const expr = node;
322
+ if (expr.properties.length == 0) {
323
+ position = expr.getEnd() - 1;
324
+ toInsert = ` ${metadataField}: [${symbolName}]\n`;
325
+ }
326
+ else {
327
+ node = expr.properties[expr.properties.length - 1];
328
+ position = node.getEnd();
329
+ // Get the indentation of the last element, if any.
330
+ const text = node.getFullText(source);
331
+ if (text.match('^\r?\r?\n')) {
332
+ toInsert = `,${text.match(/^\r?\n\s+/)[0]}${metadataField}: [${symbolName}]`;
333
+ }
334
+ else {
335
+ toInsert = `, ${metadataField}: [${symbolName}]`;
336
+ }
337
+ }
338
+ }
339
+ else if (node.kind == ts.SyntaxKind.ArrayLiteralExpression) {
340
+ // We found the field but it's empty. Insert it just before the `]`.
341
+ position--;
342
+ toInsert = `${symbolName}`;
343
+ }
344
+ else {
345
+ // Get the indentation of the last element, if any.
346
+ const text = node.getFullText(source);
347
+ if (text.match(/^\r?\n/)) {
348
+ toInsert = `,${text.match(/^\r?\n(\r?)\s+/)[0]}${symbolName}`;
349
+ }
350
+ else {
351
+ toInsert = `, ${symbolName}`;
352
+ }
353
+ }
354
+ if (importPath !== null) {
355
+ return [
356
+ new change_1.InsertChange(ngModulePath, position, toInsert),
357
+ (0, route_utils_1.insertImport)(source, ngModulePath, symbolName.replace(/\..*$/, ''), importPath),
358
+ ];
359
+ }
360
+ return [new change_1.InsertChange(ngModulePath, position, toInsert)];
361
+ }
362
+ exports.addSymbolToNgModuleMetadata = addSymbolToNgModuleMetadata;
363
+ /**
364
+ * Custom function to insert a declaration (component, pipe, directive)
365
+ * into NgModule declarations. It also imports the component.
366
+ */
367
+ function addDeclarationToModule(source, modulePath, classifiedName, importPath) {
368
+ return addSymbolToNgModuleMetadata(source, modulePath, 'declarations', classifiedName, importPath);
369
+ }
370
+ exports.addDeclarationToModule = addDeclarationToModule;
371
+ /**
372
+ * Custom function to insert an NgModule into NgModule imports. It also imports the module.
373
+ */
374
+ function addImportToModule(source, modulePath, classifiedName, importPath) {
375
+ return addSymbolToNgModuleMetadata(source, modulePath, 'imports', classifiedName, importPath);
376
+ }
377
+ exports.addImportToModule = addImportToModule;
378
+ /**
379
+ * Custom function to insert a provider into NgModule. It also imports it.
380
+ */
381
+ function addProviderToModule(source, modulePath, classifiedName, importPath) {
382
+ return addSymbolToNgModuleMetadata(source, modulePath, 'providers', classifiedName, importPath);
383
+ }
384
+ exports.addProviderToModule = addProviderToModule;
385
+ /**
386
+ * Custom function to insert an export into NgModule. It also imports it.
387
+ */
388
+ function addExportToModule(source, modulePath, classifiedName, importPath) {
389
+ return addSymbolToNgModuleMetadata(source, modulePath, 'exports', classifiedName, importPath);
390
+ }
391
+ exports.addExportToModule = addExportToModule;
392
+ /**
393
+ * Custom function to insert an export into NgModule. It also imports it.
394
+ */
395
+ function addBootstrapToModule(source, modulePath, classifiedName, importPath) {
396
+ return addSymbolToNgModuleMetadata(source, modulePath, 'bootstrap', classifiedName, importPath);
397
+ }
398
+ exports.addBootstrapToModule = addBootstrapToModule;
399
+ /**
400
+ * Determine if an import already exists.
401
+ */
402
+ function isImported(source, classifiedName, importPath) {
403
+ const allNodes = getSourceNodes(source);
404
+ const matchingNodes = allNodes
405
+ .filter(node => node.kind === ts.SyntaxKind.ImportDeclaration)
406
+ .filter((imp) => imp.moduleSpecifier.kind === ts.SyntaxKind.StringLiteral)
407
+ .filter((imp) => {
408
+ return imp.moduleSpecifier.text === importPath;
409
+ })
410
+ .filter((imp) => {
411
+ if (!imp.importClause) {
412
+ return false;
413
+ }
414
+ const nodes = findNodes(imp.importClause, ts.SyntaxKind.ImportSpecifier)
415
+ .filter(n => n.getText() === classifiedName);
416
+ return nodes.length > 0;
417
+ });
418
+ return matchingNodes.length > 0;
419
+ }
420
+ exports.isImported = isImported;
421
421
  //# sourceMappingURL=ast-utils_old.js.map