@magic-xpa/cli 4.1000.0 → 4.1100.0-dev000.0

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