@magic-xpa/cli 4.1100.0-dev4110.19 → 4.1100.0-dev4110.190

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