@dugararchit/cdk 0.0.0-dugararchit

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 (206) hide show
  1. package/_index.scss +8 -0
  2. package/a11y/_index.import.scss +2 -0
  3. package/a11y/_index.scss +102 -0
  4. package/a11y/a11y-prebuilt.scss +3 -0
  5. package/fesm2022/stagefright5-cdk-a11y.mjs +2381 -0
  6. package/fesm2022/stagefright5-cdk-a11y.mjs.map +1 -0
  7. package/fesm2022/stagefright5-cdk-accordion.mjs +267 -0
  8. package/fesm2022/stagefright5-cdk-accordion.mjs.map +1 -0
  9. package/fesm2022/stagefright5-cdk-bidi.mjs +185 -0
  10. package/fesm2022/stagefright5-cdk-bidi.mjs.map +1 -0
  11. package/fesm2022/stagefright5-cdk-clipboard.mjs +250 -0
  12. package/fesm2022/stagefright5-cdk-clipboard.mjs.map +1 -0
  13. package/fesm2022/stagefright5-cdk-coercion.mjs +128 -0
  14. package/fesm2022/stagefright5-cdk-coercion.mjs.map +1 -0
  15. package/fesm2022/stagefright5-cdk-collections.mjs +472 -0
  16. package/fesm2022/stagefright5-cdk-collections.mjs.map +1 -0
  17. package/fesm2022/stagefright5-cdk-drag-drop.mjs +3690 -0
  18. package/fesm2022/stagefright5-cdk-drag-drop.mjs.map +1 -0
  19. package/fesm2022/stagefright5-cdk-keycodes.mjs +159 -0
  20. package/fesm2022/stagefright5-cdk-keycodes.mjs.map +1 -0
  21. package/fesm2022/stagefright5-cdk-layout.mjs +246 -0
  22. package/fesm2022/stagefright5-cdk-layout.mjs.map +1 -0
  23. package/fesm2022/stagefright5-cdk-observers.mjs +203 -0
  24. package/fesm2022/stagefright5-cdk-observers.mjs.map +1 -0
  25. package/fesm2022/stagefright5-cdk-overlay.mjs +3033 -0
  26. package/fesm2022/stagefright5-cdk-overlay.mjs.map +1 -0
  27. package/fesm2022/stagefright5-cdk-platform.mjs +375 -0
  28. package/fesm2022/stagefright5-cdk-platform.mjs.map +1 -0
  29. package/fesm2022/stagefright5-cdk-portal.mjs +672 -0
  30. package/fesm2022/stagefright5-cdk-portal.mjs.map +1 -0
  31. package/fesm2022/stagefright5-cdk-scrolling.mjs +1399 -0
  32. package/fesm2022/stagefright5-cdk-scrolling.mjs.map +1 -0
  33. package/fesm2022/stagefright5-cdk-stepper.mjs +624 -0
  34. package/fesm2022/stagefright5-cdk-stepper.mjs.map +1 -0
  35. package/fesm2022/stagefright5-cdk-table.mjs +2346 -0
  36. package/fesm2022/stagefright5-cdk-table.mjs.map +1 -0
  37. package/fesm2022/stagefright5-cdk-testing-protractor.mjs +324 -0
  38. package/fesm2022/stagefright5-cdk-testing-protractor.mjs.map +1 -0
  39. package/fesm2022/stagefright5-cdk-testing-selenium-webdriver.mjs +364 -0
  40. package/fesm2022/stagefright5-cdk-testing-selenium-webdriver.mjs.map +1 -0
  41. package/fesm2022/stagefright5-cdk-testing-testbed.mjs +806 -0
  42. package/fesm2022/stagefright5-cdk-testing-testbed.mjs.map +1 -0
  43. package/fesm2022/stagefright5-cdk-testing.mjs +740 -0
  44. package/fesm2022/stagefright5-cdk-testing.mjs.map +1 -0
  45. package/fesm2022/stagefright5-cdk-text-field.mjs +453 -0
  46. package/fesm2022/stagefright5-cdk-text-field.mjs.map +1 -0
  47. package/fesm2022/stagefright5-cdk-tree.mjs +945 -0
  48. package/fesm2022/stagefright5-cdk-tree.mjs.map +1 -0
  49. package/fesm2022/stagefright5-cdk.mjs +26 -0
  50. package/fesm2022/stagefright5-cdk.mjs.map +1 -0
  51. package/overlay/_index-deprecated.scss +13 -0
  52. package/overlay/_index.import.scss +13 -0
  53. package/overlay/_index.scss +148 -0
  54. package/overlay/overlay-prebuilt.scss +3 -0
  55. package/package.json +159 -0
  56. package/schematics/ng-update/test-cases/misc/global-stylesheets-test.scss +3 -0
  57. package/schematics/schematics/BUILD.bazel +104 -0
  58. package/schematics/schematics/README.md +4 -0
  59. package/schematics/schematics/collection.json +18 -0
  60. package/schematics/schematics/index.ts +22 -0
  61. package/schematics/schematics/migration.json +50 -0
  62. package/schematics/schematics/ng-add/index.spec.ts +50 -0
  63. package/schematics/schematics/ng-add/index.ts +39 -0
  64. package/schematics/schematics/ng-add/package-config.ts +62 -0
  65. package/schematics/schematics/ng-add/schema.json +16 -0
  66. package/schematics/schematics/ng-add/schema.ts +12 -0
  67. package/schematics/schematics/ng-generate/drag-drop/files/__path__/__name@dasherize@if-flat__/__name@dasherize__.component.__style__.template +53 -0
  68. package/schematics/schematics/ng-generate/drag-drop/files/__path__/__name@dasherize@if-flat__/__name@dasherize__.component.html.template +17 -0
  69. package/schematics/schematics/ng-generate/drag-drop/files/__path__/__name@dasherize@if-flat__/__name@dasherize__.component.spec.ts.template +29 -0
  70. package/schematics/schematics/ng-generate/drag-drop/files/__path__/__name@dasherize@if-flat__/__name@dasherize__.component.ts.template +43 -0
  71. package/schematics/schematics/ng-generate/drag-drop/index.spec.ts +205 -0
  72. package/schematics/schematics/ng-generate/drag-drop/index.ts +35 -0
  73. package/schematics/schematics/ng-generate/drag-drop/schema.json +93 -0
  74. package/schematics/schematics/ng-generate/drag-drop/schema.ts +11 -0
  75. package/schematics/schematics/ng-update/data/attribute-selectors.ts +29 -0
  76. package/schematics/schematics/ng-update/data/class-names.ts +50 -0
  77. package/schematics/schematics/ng-update/data/constructor-checks.ts +75 -0
  78. package/schematics/schematics/ng-update/data/css-selectors.ts +33 -0
  79. package/schematics/schematics/ng-update/data/element-selectors.ts +21 -0
  80. package/schematics/schematics/ng-update/data/index.ts +18 -0
  81. package/schematics/schematics/ng-update/data/input-names.ts +118 -0
  82. package/schematics/schematics/ng-update/data/method-call-checks.ts +63 -0
  83. package/schematics/schematics/ng-update/data/output-names.ts +42 -0
  84. package/schematics/schematics/ng-update/data/property-names.ts +151 -0
  85. package/schematics/schematics/ng-update/data/symbol-removal.ts +22 -0
  86. package/schematics/schematics/ng-update/devkit-file-system.ts +87 -0
  87. package/schematics/schematics/ng-update/devkit-migration-rule.ts +185 -0
  88. package/schematics/schematics/ng-update/devkit-migration.ts +47 -0
  89. package/schematics/schematics/ng-update/find-stylesheets.ts +43 -0
  90. package/schematics/schematics/ng-update/html-parsing/angular.ts +49 -0
  91. package/schematics/schematics/ng-update/html-parsing/elements.ts +66 -0
  92. package/schematics/schematics/ng-update/index.ts +113 -0
  93. package/schematics/schematics/ng-update/migrations/attribute-selectors.ts +83 -0
  94. package/schematics/schematics/ng-update/migrations/class-inheritance.ts +63 -0
  95. package/schematics/schematics/ng-update/migrations/class-names.ts +107 -0
  96. package/schematics/schematics/ng-update/migrations/constructor-signature.ts +170 -0
  97. package/schematics/schematics/ng-update/migrations/css-selectors.ts +83 -0
  98. package/schematics/schematics/ng-update/migrations/element-selectors.ts +75 -0
  99. package/schematics/schematics/ng-update/migrations/input-names.ts +83 -0
  100. package/schematics/schematics/ng-update/migrations/method-call-arguments.ts +70 -0
  101. package/schematics/schematics/ng-update/migrations/misc-template.ts +36 -0
  102. package/schematics/schematics/ng-update/migrations/output-names.ts +61 -0
  103. package/schematics/schematics/ng-update/migrations/property-names.ts +61 -0
  104. package/schematics/schematics/ng-update/migrations/symbol-removal.ts +51 -0
  105. package/schematics/schematics/ng-update/migrations/tilde-import-v13/tilde-import-migration.ts +40 -0
  106. package/schematics/schematics/ng-update/public-api.ts +18 -0
  107. package/schematics/schematics/ng-update/test-cases/index.spec.ts +18 -0
  108. package/schematics/schematics/ng-update/test-cases/misc/external-resource-resolution.spec.ts +32 -0
  109. package/schematics/schematics/ng-update/test-cases/misc/external-resource-resolution_input.ts +22 -0
  110. package/schematics/schematics/ng-update/test-cases/misc/global-stylesheets-test.scss +3 -0
  111. package/schematics/schematics/ng-update/test-cases/misc/global-stylesheets.spec.ts +50 -0
  112. package/schematics/schematics/ng-update/test-cases/misc/global-stylesheets_input.ts +8 -0
  113. package/schematics/schematics/ng-update/test-cases/misc/method-call-checks.spec.ts +20 -0
  114. package/schematics/schematics/ng-update/test-cases/misc/method-call-checks_input.ts +18 -0
  115. package/schematics/schematics/ng-update/test-cases/misc/module-resolution.spec.ts +27 -0
  116. package/schematics/schematics/ng-update/test-cases/v13/misc/tilde-import-v13.spec.ts +149 -0
  117. package/schematics/schematics/ng-update/test-cases/v6/attribute-selectors_expected_output.ts +36 -0
  118. package/schematics/schematics/ng-update/test-cases/v6/attribute-selectors_input.ts +36 -0
  119. package/schematics/schematics/ng-update/test-cases/v6/class-names_expected_output.ts +8 -0
  120. package/schematics/schematics/ng-update/test-cases/v6/class-names_input.ts +8 -0
  121. package/schematics/schematics/ng-update/test-cases/v6/input-names_expected_output.ts +22 -0
  122. package/schematics/schematics/ng-update/test-cases/v6/input-names_input.ts +22 -0
  123. package/schematics/schematics/ng-update/test-cases/v6/property-names_expected_output.ts +64 -0
  124. package/schematics/schematics/ng-update/test-cases/v6/property-names_input.ts +64 -0
  125. package/schematics/schematics/ng-update/test-cases/v7/property-names_expected_output.ts +28 -0
  126. package/schematics/schematics/ng-update/test-cases/v7/property-names_input.ts +28 -0
  127. package/schematics/schematics/ng-update/typescript/base-types.ts +22 -0
  128. package/schematics/schematics/ng-update/typescript/imports.ts +54 -0
  129. package/schematics/schematics/ng-update/typescript/literal.ts +32 -0
  130. package/schematics/schematics/ng-update/typescript/module-specifiers.ts +39 -0
  131. package/schematics/schematics/ng-update/update-schematic.md +209 -0
  132. package/schematics/schematics/ng-update/upgrade-data.ts +79 -0
  133. package/schematics/schematics/paths.ts +15 -0
  134. package/schematics/schematics/testing/BUILD.bazel +27 -0
  135. package/schematics/schematics/testing/file-content.ts +20 -0
  136. package/schematics/schematics/testing/index.ts +14 -0
  137. package/schematics/schematics/testing/post-scheduled-tasks.ts +45 -0
  138. package/schematics/schematics/testing/resolve-bazel-path.ts +46 -0
  139. package/schematics/schematics/testing/test-app.ts +21 -0
  140. package/schematics/schematics/testing/test-case-setup.ts +236 -0
  141. package/schematics/schematics/testing/test-library.ts +21 -0
  142. package/schematics/schematics/testing/test-project.ts +40 -0
  143. package/schematics/schematics/testing/tsconfig.json +7 -0
  144. package/schematics/schematics/tsconfig.json +28 -0
  145. package/schematics/schematics/update-tool/BUILD.bazel +19 -0
  146. package/schematics/schematics/update-tool/component-resource-collector.ts +195 -0
  147. package/schematics/schematics/update-tool/file-system.ts +78 -0
  148. package/schematics/schematics/update-tool/index.ts +191 -0
  149. package/schematics/schematics/update-tool/logger.ts +23 -0
  150. package/schematics/schematics/update-tool/migration.ts +88 -0
  151. package/schematics/schematics/update-tool/public-api.ts +16 -0
  152. package/schematics/schematics/update-tool/target-version.ts +31 -0
  153. package/schematics/schematics/update-tool/tsconfig.json +7 -0
  154. package/schematics/schematics/update-tool/update-recorder.ts +14 -0
  155. package/schematics/schematics/update-tool/utils/decorators.ts +54 -0
  156. package/schematics/schematics/update-tool/utils/functions.ts +18 -0
  157. package/schematics/schematics/update-tool/utils/imports.ts +128 -0
  158. package/schematics/schematics/update-tool/utils/line-mappings.ts +84 -0
  159. package/schematics/schematics/update-tool/utils/parse-tsconfig.ts +25 -0
  160. package/schematics/schematics/update-tool/utils/property-name.ts +28 -0
  161. package/schematics/schematics/update-tool/utils/virtual-host.ts +116 -0
  162. package/schematics/schematics/update-tool/version-changes.ts +49 -0
  163. package/schematics/schematics/utils/ast/ng-module-imports.spec.ts +43 -0
  164. package/schematics/schematics/utils/ast/ng-module-imports.ts +102 -0
  165. package/schematics/schematics/utils/ast.ts +88 -0
  166. package/schematics/schematics/utils/build-component.ts +252 -0
  167. package/schematics/schematics/utils/get-project.ts +27 -0
  168. package/schematics/schematics/utils/html-manipulation.ts +105 -0
  169. package/schematics/schematics/utils/index.ts +19 -0
  170. package/schematics/schematics/utils/parse5-element.ts +34 -0
  171. package/schematics/schematics/utils/project-index-file.ts +21 -0
  172. package/schematics/schematics/utils/project-main-file.ts +26 -0
  173. package/schematics/schematics/utils/project-style-file.ts +53 -0
  174. package/schematics/schematics/utils/project-targets.ts +43 -0
  175. package/schematics/schematics/utils/project-tsconfig-paths.spec.ts +91 -0
  176. package/schematics/schematics/utils/project-tsconfig-paths.ts +49 -0
  177. package/schematics/schematics/utils/schematic-options.ts +65 -0
  178. package/schematics/schematics/utils/vendored-ast-utils/index.ts +599 -0
  179. package/scrolling/virtual-scroll-viewport.scss +87 -0
  180. package/table/table.scss +3 -0
  181. package/text-field/_index.import.scss +2 -0
  182. package/text-field/_index.scss +89 -0
  183. package/text-field/text-field-prebuilt.scss +4 -0
  184. package/types/stagefright5-cdk-a11y.d.ts +1160 -0
  185. package/types/stagefright5-cdk-accordion.d.ts +95 -0
  186. package/types/stagefright5-cdk-bidi.d.ts +95 -0
  187. package/types/stagefright5-cdk-clipboard.d.ts +116 -0
  188. package/types/stagefright5-cdk-coercion.d.ts +101 -0
  189. package/types/stagefright5-cdk-collections.d.ts +405 -0
  190. package/types/stagefright5-cdk-drag-drop.d.ts +1471 -0
  191. package/types/stagefright5-cdk-keycodes.d.ts +142 -0
  192. package/types/stagefright5-cdk-layout.d.ts +96 -0
  193. package/types/stagefright5-cdk-observers.d.ts +91 -0
  194. package/types/stagefright5-cdk-overlay.d.ts +1258 -0
  195. package/types/stagefright5-cdk-platform.d.ts +130 -0
  196. package/types/stagefright5-cdk-portal.d.ts +327 -0
  197. package/types/stagefright5-cdk-scrolling.d.ts +631 -0
  198. package/types/stagefright5-cdk-stepper.d.ts +302 -0
  199. package/types/stagefright5-cdk-table.d.ts +1128 -0
  200. package/types/stagefright5-cdk-testing-protractor.d.ts +146 -0
  201. package/types/stagefright5-cdk-testing-selenium-webdriver.d.ts +161 -0
  202. package/types/stagefright5-cdk-testing-testbed.d.ts +168 -0
  203. package/types/stagefright5-cdk-testing.d.ts +735 -0
  204. package/types/stagefright5-cdk-text-field.d.ts +171 -0
  205. package/types/stagefright5-cdk-tree.d.ts +522 -0
  206. package/types/stagefright5-cdk.d.ts +14 -0
@@ -0,0 +1,49 @@
1
+ /**
2
+ * @license
3
+ * Copyright Google LLC All Rights Reserved.
4
+ *
5
+ * Use of this source code is governed by an MIT-style license that can be
6
+ * found in the LICENSE file at https://angular.io/license
7
+ */
8
+
9
+ import {normalize} from '@angular-devkit/core';
10
+ import {
11
+ ProjectDefinition,
12
+ WorkspaceDefinition,
13
+ WorkspaceHost,
14
+ } from '@angular-devkit/core/src/workspace';
15
+ import {readJsonWorkspace} from '@angular-devkit/core/src/workspace/json/reader';
16
+ import {Tree} from '@angular-devkit/schematics';
17
+ import {WorkspacePath} from '../update-tool/file-system';
18
+
19
+ /** Name of the default Angular CLI workspace configuration files. */
20
+ const defaultWorkspaceConfigPaths = ['/angular.json', '/.angular.json'];
21
+
22
+ /** Gets the tsconfig path from the given target within the specified project. */
23
+ export function getTargetTsconfigPath(
24
+ project: ProjectDefinition,
25
+ targetName: string,
26
+ ): WorkspacePath | null {
27
+ const tsconfig = project.targets?.get(targetName)?.options?.tsConfig;
28
+ return tsconfig ? normalize(tsconfig as string) : null;
29
+ }
30
+
31
+ /** Resolve the workspace configuration of the specified tree gracefully. */
32
+ export async function getWorkspaceConfigGracefully(
33
+ tree: Tree,
34
+ ): Promise<WorkspaceDefinition | null> {
35
+ const path = defaultWorkspaceConfigPaths.find(filePath => tree.exists(filePath));
36
+ const configBuffer = tree.read(path!);
37
+
38
+ if (!path || !configBuffer) {
39
+ return null;
40
+ }
41
+
42
+ try {
43
+ return await readJsonWorkspace(path, {
44
+ readFile: async filePath => tree.read(filePath)!.toString(),
45
+ } as WorkspaceHost);
46
+ } catch {
47
+ return null;
48
+ }
49
+ }
@@ -0,0 +1,65 @@
1
+ /**
2
+ * @license
3
+ * Copyright Google LLC All Rights Reserved.
4
+ *
5
+ * Use of this source code is governed by an MIT-style license that can be
6
+ * found in the LICENSE file at https://angular.io/license
7
+ */
8
+
9
+ import {ProjectDefinition} from '@angular-devkit/core/src/workspace';
10
+ import {isJsonObject, JsonObject} from '@angular-devkit/core';
11
+ import {Schema, Style} from '@schematics/angular/component/schema';
12
+
13
+ /**
14
+ * Returns the default options for the `@schematics/angular:component` schematic which would
15
+ * have been specified at project initialization (ng new or ng init).
16
+ *
17
+ * This is necessary because the Angular CLI only exposes the default values for the "--style",
18
+ * "--inlineStyle", "--skipTests" and "--inlineTemplate" options to the "component" schematic.
19
+ */
20
+ export function getDefaultComponentOptions(project: ProjectDefinition): Partial<Schema> {
21
+ // Note: Not all options which are available when running "ng new" will be stored in the
22
+ // workspace config. List of options which will be available in the configuration:
23
+ // angular/angular-cli/blob/main/packages/schematics/angular/application/index.ts#L109-L131
24
+ let skipTests = getDefaultComponentOption<boolean | null>(project, ['skipTests'], null);
25
+
26
+ // In case "skipTests" is not set explicitly, also look for the "spec" option. The "spec"
27
+ // option has been deprecated but can be still used in older Angular CLI projects.
28
+ // See: https://github.com/angular/angular-cli/commit/a12a4e02a4689b5bdbc6e740c0d9865afb55671a
29
+ if (skipTests === null) {
30
+ skipTests = !getDefaultComponentOption(project, ['spec'], true);
31
+ }
32
+
33
+ return {
34
+ style: getDefaultComponentOption<Style>(project, ['style', 'styleext'], Style.Css),
35
+ inlineStyle: getDefaultComponentOption(project, ['inlineStyle'], false),
36
+ inlineTemplate: getDefaultComponentOption(project, ['inlineTemplate'], false),
37
+ skipTests: skipTests,
38
+ };
39
+ }
40
+
41
+ /**
42
+ * Gets the default value for the specified option. The default options will be determined
43
+ * by looking at the stored schematic options for `@schematics/angular:component` in the
44
+ * CLI workspace configuration.
45
+ */
46
+ function getDefaultComponentOption<T>(
47
+ project: ProjectDefinition,
48
+ optionNames: string[],
49
+ fallbackValue: T,
50
+ ): T {
51
+ const schematicOptions = isJsonObject(project.extensions.schematics || null)
52
+ ? (project.extensions.schematics as JsonObject)
53
+ : null;
54
+ const defaultSchematic = schematicOptions
55
+ ? (schematicOptions['@schematics/angular:component'] as JsonObject | null)
56
+ : null;
57
+
58
+ for (const optionName of optionNames) {
59
+ if (defaultSchematic && defaultSchematic[optionName] != null) {
60
+ return defaultSchematic[optionName] as unknown as T;
61
+ }
62
+ }
63
+
64
+ return fallbackValue;
65
+ }
@@ -0,0 +1,599 @@
1
+ /**
2
+ * @license
3
+ * Copyright Google LLC All Rights Reserved.
4
+ *
5
+ * Use of this source code is governed by an MIT-style license that can be
6
+ * found in the LICENSE file at https://angular.io/license
7
+ */
8
+
9
+ // tslint:disable
10
+
11
+ /*
12
+ * Note: This file contains vendored TypeScript AST utils from "@schematics/angular".
13
+ * Since there is no canonical place for common utils, and we don't want to use the AST
14
+ * utils directly from "@schematics/angular" to avoid TypeScript version mismatches, we
15
+ * copy the needed AST utils until there is a general place for such utility functions.
16
+ *
17
+ * Taken from:
18
+ * (1) https://github.com/angular/angular-cli/blob/30df1470a0f18989db336d50b55a79021ab64c85/packages/schematics/angular/utility/ng-ast-utils.ts
19
+ * (2) https://github.com/angular/angular-cli/blob/30df1470a0f18989db336d50b55a79021ab64c85/packages/schematics/angular/utility/ast-utils.ts
20
+ */
21
+
22
+ import {normalize} from '@angular-devkit/core';
23
+ import {SchematicsException, Tree} from '@angular-devkit/schematics';
24
+ import {Change, InsertChange, NoopChange} from '@schematics/angular/utility/change';
25
+ import {dirname} from 'path';
26
+
27
+ import * as ts from 'typescript';
28
+
29
+ /**
30
+ * Add Import `import { symbolName } from fileName` if the import doesn't exit
31
+ * already. Assumes fileToEdit can be resolved and accessed.
32
+ * @param fileToEdit (file we want to add import to)
33
+ * @param symbolName (item to import)
34
+ * @param fileName (path to the file)
35
+ * @param isDefault (if true, import follows style for importing default exports)
36
+ * @return Change
37
+ */
38
+ export function insertImport(
39
+ source: ts.SourceFile,
40
+ fileToEdit: string,
41
+ symbolName: string,
42
+ fileName: string,
43
+ isDefault = false,
44
+ ): Change {
45
+ const rootNode = source;
46
+ const allImports = findNodes(rootNode, ts.SyntaxKind.ImportDeclaration);
47
+
48
+ // get nodes that map to import statements from the file fileName
49
+ const relevantImports = allImports.filter(node => {
50
+ // StringLiteral of the ImportDeclaration is the import file (fileName in this case).
51
+ const importFiles = node
52
+ .getChildren()
53
+ .filter(child => child.kind === ts.SyntaxKind.StringLiteral)
54
+ .map(n => (n as ts.StringLiteral).text);
55
+
56
+ return importFiles.filter(file => file === fileName).length === 1;
57
+ });
58
+
59
+ if (relevantImports.length > 0) {
60
+ let importsAsterisk = false;
61
+ // imports from import file
62
+ const imports: ts.Node[] = [];
63
+ relevantImports.forEach(n => {
64
+ Array.prototype.push.apply(imports, findNodes(n, ts.SyntaxKind.Identifier));
65
+ if (findNodes(n, ts.SyntaxKind.AsteriskToken).length > 0) {
66
+ importsAsterisk = true;
67
+ }
68
+ });
69
+
70
+ // if imports * from fileName, don't add symbolName
71
+ if (importsAsterisk) {
72
+ return new NoopChange();
73
+ }
74
+
75
+ const importTextNodes = imports.filter(n => (n as ts.Identifier).text === symbolName);
76
+
77
+ // insert import if it's not there
78
+ if (importTextNodes.length === 0) {
79
+ const fallbackPos =
80
+ findNodes(relevantImports[0], ts.SyntaxKind.CloseBraceToken)[0].getStart() ||
81
+ findNodes(relevantImports[0], ts.SyntaxKind.FromKeyword)[0].getStart();
82
+
83
+ return insertAfterLastOccurrence(imports, `, ${symbolName}`, fileToEdit, fallbackPos);
84
+ }
85
+
86
+ return new NoopChange();
87
+ }
88
+
89
+ // no such import declaration exists
90
+ const useStrict = findNodes(rootNode, ts.SyntaxKind.StringLiteral).filter(
91
+ n => (n as ts.StringLiteral).text === 'use strict',
92
+ );
93
+ let fallbackPos = 0;
94
+ if (useStrict.length > 0) {
95
+ fallbackPos = useStrict[0].end;
96
+ }
97
+ const open = isDefault ? '' : '{ ';
98
+ const close = isDefault ? '' : ' }';
99
+ // if there are no imports or 'use strict' statement, insert import at beginning of file
100
+ const insertAtBeginning = allImports.length === 0 && useStrict.length === 0;
101
+ const separator = insertAtBeginning ? '' : ';\n';
102
+ const toInsert =
103
+ `${separator}import ${open}${symbolName}${close}` +
104
+ ` from '${fileName}'${insertAtBeginning ? ';\n' : ''}`;
105
+
106
+ return insertAfterLastOccurrence(
107
+ allImports,
108
+ toInsert,
109
+ fileToEdit,
110
+ fallbackPos,
111
+ ts.SyntaxKind.StringLiteral,
112
+ );
113
+ }
114
+
115
+ /**
116
+ * Find all nodes from the AST in the subtree of node of SyntaxKind kind.
117
+ * @param node
118
+ * @param kind
119
+ * @param max The maximum number of items to return.
120
+ * @param recursive Continue looking for nodes of kind recursive until end
121
+ * the last child even when node of kind has been found.
122
+ * @return all nodes of kind, or [] if none is found
123
+ */
124
+ export function findNodes(
125
+ node: ts.Node,
126
+ kind: ts.SyntaxKind,
127
+ max = Infinity,
128
+ recursive = false,
129
+ ): ts.Node[] {
130
+ if (!node || max == 0) {
131
+ return [];
132
+ }
133
+
134
+ const arr: ts.Node[] = [];
135
+ if (node.kind === kind) {
136
+ arr.push(node);
137
+ max--;
138
+ }
139
+ if (max > 0 && (recursive || node.kind !== kind)) {
140
+ for (const child of node.getChildren()) {
141
+ findNodes(child, kind, max).forEach(node => {
142
+ if (max > 0) {
143
+ arr.push(node);
144
+ }
145
+ max--;
146
+ });
147
+
148
+ if (max <= 0) {
149
+ break;
150
+ }
151
+ }
152
+ }
153
+
154
+ return arr;
155
+ }
156
+
157
+ /**
158
+ * Get all the nodes from a source.
159
+ * @param sourceFile The source file object.
160
+ * @returns {Observable<ts.Node>} An observable of all the nodes in the source.
161
+ */
162
+ export function getSourceNodes(sourceFile: ts.SourceFile): ts.Node[] {
163
+ const nodes: ts.Node[] = [sourceFile];
164
+ const result: ts.Node[] = [];
165
+
166
+ while (nodes.length > 0) {
167
+ const node = nodes.shift();
168
+
169
+ if (node) {
170
+ result.push(node);
171
+ if (node.getChildCount(sourceFile) >= 0) {
172
+ nodes.unshift(...node.getChildren());
173
+ }
174
+ }
175
+ }
176
+
177
+ return result;
178
+ }
179
+
180
+ export function findNode(node: ts.Node, kind: ts.SyntaxKind, text: string): ts.Node | null {
181
+ if (node.kind === kind && node.getText() === text) {
182
+ // throw new Error(node.getText());
183
+ return node;
184
+ }
185
+
186
+ let foundNode: ts.Node | null = null;
187
+ ts.forEachChild(node, childNode => {
188
+ foundNode = foundNode || findNode(childNode, kind, text);
189
+ });
190
+
191
+ return foundNode;
192
+ }
193
+
194
+ /**
195
+ * Helper for sorting nodes.
196
+ * @return function to sort nodes in increasing order of position in sourceFile
197
+ */
198
+ function nodesByPosition(first: ts.Node, second: ts.Node): number {
199
+ return first.getStart() - second.getStart();
200
+ }
201
+
202
+ /**
203
+ * Insert `toInsert` after the last occurence of `ts.SyntaxKind[nodes[i].kind]`
204
+ * or after the last of occurence of `syntaxKind` if the last occurence is a sub child
205
+ * of ts.SyntaxKind[nodes[i].kind] and save the changes in file.
206
+ *
207
+ * @param nodes insert after the last occurence of nodes
208
+ * @param toInsert string to insert
209
+ * @param file file to insert changes into
210
+ * @param fallbackPos position to insert if toInsert happens to be the first occurence
211
+ * @param syntaxKind the ts.SyntaxKind of the subchildren to insert after
212
+ * @return Change instance
213
+ * @throw Error if toInsert is first occurence but fall back is not set
214
+ */
215
+ export function insertAfterLastOccurrence(
216
+ nodes: ts.Node[],
217
+ toInsert: string,
218
+ file: string,
219
+ fallbackPos: number,
220
+ syntaxKind?: ts.SyntaxKind,
221
+ ): Change {
222
+ let lastItem: ts.Node | undefined;
223
+ for (const node of nodes) {
224
+ if (!lastItem || lastItem.getStart() < node.getStart()) {
225
+ lastItem = node;
226
+ }
227
+ }
228
+ if (syntaxKind && lastItem) {
229
+ lastItem = findNodes(lastItem, syntaxKind).sort(nodesByPosition).pop();
230
+ }
231
+ if (!lastItem && fallbackPos == undefined) {
232
+ throw new Error(`tried to insert ${toInsert} as first occurence with no fallback position`);
233
+ }
234
+ const lastItemPosition: number = lastItem ? lastItem.getEnd() : fallbackPos;
235
+
236
+ return new InsertChange(file, lastItemPosition, toInsert);
237
+ }
238
+
239
+ function _angularImportsFromNode(
240
+ node: ts.ImportDeclaration,
241
+ _sourceFile: ts.SourceFile,
242
+ ): {[name: string]: string} {
243
+ const ms = node.moduleSpecifier;
244
+ let modulePath: string;
245
+ switch (ms.kind) {
246
+ case ts.SyntaxKind.StringLiteral:
247
+ modulePath = (ms as ts.StringLiteral).text;
248
+ break;
249
+ default:
250
+ return {};
251
+ }
252
+
253
+ if (!modulePath.startsWith('@angular/')) {
254
+ return {};
255
+ }
256
+
257
+ if (node.importClause) {
258
+ if (node.importClause.name) {
259
+ // This is of the form `import Name from 'path'`. Ignore.
260
+ return {};
261
+ } else if (node.importClause.namedBindings) {
262
+ const nb = node.importClause.namedBindings;
263
+ if (nb.kind == ts.SyntaxKind.NamespaceImport) {
264
+ // This is of the form `import * as name from 'path'`. Return `name.`.
265
+ return {
266
+ [(nb as ts.NamespaceImport).name.text + '.']: modulePath,
267
+ };
268
+ } else {
269
+ // This is of the form `import {a,b,c} from 'path'`
270
+ const namedImports = nb as ts.NamedImports;
271
+
272
+ return namedImports.elements
273
+ .map((is: ts.ImportSpecifier) => (is.propertyName ? is.propertyName.text : is.name.text))
274
+ .reduce((acc: {[name: string]: string}, curr: string) => {
275
+ acc[curr] = modulePath;
276
+
277
+ return acc;
278
+ }, {});
279
+ }
280
+ }
281
+
282
+ return {};
283
+ } else {
284
+ // This is of the form `import 'path';`. Nothing to do.
285
+ return {};
286
+ }
287
+ }
288
+
289
+ export function getDecoratorMetadata(
290
+ source: ts.SourceFile,
291
+ identifier: string,
292
+ module: string,
293
+ ): ts.Node[] {
294
+ const angularImports: {[name: string]: string} = findNodes(
295
+ source,
296
+ ts.SyntaxKind.ImportDeclaration,
297
+ )
298
+ .map(node => _angularImportsFromNode(node as ts.ImportDeclaration, source))
299
+ .reduce((acc: {[name: string]: string}, current: {[name: string]: string}) => {
300
+ for (const key of Object.keys(current)) {
301
+ acc[key] = current[key];
302
+ }
303
+
304
+ return acc;
305
+ }, {});
306
+
307
+ return getSourceNodes(source)
308
+ .filter(node => {
309
+ return (
310
+ node.kind == ts.SyntaxKind.Decorator &&
311
+ (node as ts.Decorator).expression.kind == ts.SyntaxKind.CallExpression
312
+ );
313
+ })
314
+ .map(node => (node as ts.Decorator).expression as ts.CallExpression)
315
+ .filter(expr => {
316
+ if (expr.expression.kind == ts.SyntaxKind.Identifier) {
317
+ const id = expr.expression as ts.Identifier;
318
+
319
+ return id.text == identifier && angularImports[id.text] === module;
320
+ } else if (expr.expression.kind == ts.SyntaxKind.PropertyAccessExpression) {
321
+ // This covers foo.NgModule when importing * as foo.
322
+ const paExpr = expr.expression as ts.PropertyAccessExpression;
323
+ // If the left expression is not an identifier, just give up at that point.
324
+ if (paExpr.expression.kind !== ts.SyntaxKind.Identifier) {
325
+ return false;
326
+ }
327
+
328
+ const id = paExpr.name.text;
329
+ const moduleId = (paExpr.expression as ts.Identifier).text;
330
+
331
+ return id === identifier && angularImports[moduleId + '.'] === module;
332
+ }
333
+
334
+ return false;
335
+ })
336
+ .filter(
337
+ expr => expr.arguments[0] && expr.arguments[0].kind == ts.SyntaxKind.ObjectLiteralExpression,
338
+ )
339
+ .map(expr => expr.arguments[0] as ts.ObjectLiteralExpression);
340
+ }
341
+
342
+ export function getMetadataField(
343
+ node: ts.ObjectLiteralExpression,
344
+ metadataField: string,
345
+ ): ts.ObjectLiteralElement[] {
346
+ return (
347
+ node.properties
348
+ .filter(prop => ts.isPropertyAssignment(prop))
349
+ // Filter out every fields that's not "metadataField". Also handles string literals
350
+ // (but not expressions).
351
+ .filter(node => {
352
+ const name = (node as ts.PropertyAssignment).name;
353
+ return (
354
+ (ts.isIdentifier(name) || ts.isStringLiteral(name)) && name.getText() === metadataField
355
+ );
356
+ })
357
+ );
358
+ }
359
+
360
+ export function addSymbolToNgModuleMetadata(
361
+ source: ts.SourceFile,
362
+ ngModulePath: string,
363
+ metadataField: string,
364
+ symbolName: string,
365
+ importPath: string | null = null,
366
+ ): Change[] {
367
+ const nodes = getDecoratorMetadata(source, 'NgModule', '@angular/core');
368
+ let node: any = nodes[0]; // tslint:disable-line:no-any
369
+
370
+ // Find the decorator declaration.
371
+ if (!node) {
372
+ return [];
373
+ }
374
+
375
+ // Get all the children property assignment of object literals.
376
+ const matchingProperties = getMetadataField(node as ts.ObjectLiteralExpression, metadataField);
377
+
378
+ // Get the last node of the array literal.
379
+ if (!matchingProperties) {
380
+ return [];
381
+ }
382
+ if (matchingProperties.length == 0) {
383
+ // We haven't found the field in the metadata declaration. Insert a new field.
384
+ const expr = node as ts.ObjectLiteralExpression;
385
+ let position: number;
386
+ let toInsert: string;
387
+ if (expr.properties.length == 0) {
388
+ position = expr.getEnd() - 1;
389
+ toInsert = ` ${metadataField}: [${symbolName}]\n`;
390
+ } else {
391
+ node = expr.properties[expr.properties.length - 1];
392
+ position = node.getEnd();
393
+ // Get the indentation of the last element, if any.
394
+ const text = node.getFullText(source);
395
+ const matches = text.match(/^\r?\n\s*/);
396
+ if (matches && matches.length > 0) {
397
+ toInsert = `,${matches[0]}${metadataField}: [${symbolName}]`;
398
+ } else {
399
+ toInsert = `, ${metadataField}: [${symbolName}]`;
400
+ }
401
+ }
402
+ if (importPath !== null) {
403
+ return [
404
+ new InsertChange(ngModulePath, position, toInsert),
405
+ insertImport(source, ngModulePath, symbolName.replace(/\..*$/, ''), importPath),
406
+ ];
407
+ } else {
408
+ return [new InsertChange(ngModulePath, position, toInsert)];
409
+ }
410
+ }
411
+ const assignment = matchingProperties[0] as ts.PropertyAssignment;
412
+
413
+ // If it's not an array, nothing we can do really.
414
+ if (assignment.initializer.kind !== ts.SyntaxKind.ArrayLiteralExpression) {
415
+ return [];
416
+ }
417
+
418
+ const arrLiteral = assignment.initializer as ts.ArrayLiteralExpression;
419
+ if (arrLiteral.elements.length == 0) {
420
+ // Forward the property.
421
+ node = arrLiteral;
422
+ } else {
423
+ node = arrLiteral.elements;
424
+ }
425
+
426
+ if (!node) {
427
+ // tslint:disable-next-line: no-console
428
+ console.error('No app module found. Please add your new class to your component.');
429
+
430
+ return [];
431
+ }
432
+
433
+ if (Array.isArray(node)) {
434
+ const nodeArray = node as {} as Array<ts.Node>;
435
+ const symbolsArray = nodeArray.map(node => node.getText());
436
+ if (symbolsArray.includes(symbolName)) {
437
+ return [];
438
+ }
439
+
440
+ node = node[node.length - 1];
441
+ }
442
+
443
+ let toInsert: string;
444
+ let position = node.getEnd();
445
+ if (node.kind == ts.SyntaxKind.ObjectLiteralExpression) {
446
+ // We haven't found the field in the metadata declaration. Insert a new
447
+ // field.
448
+ const expr = node as ts.ObjectLiteralExpression;
449
+ if (expr.properties.length == 0) {
450
+ position = expr.getEnd() - 1;
451
+ toInsert = ` ${symbolName}\n`;
452
+ } else {
453
+ // Get the indentation of the last element, if any.
454
+ const text = node.getFullText(source);
455
+ if (text.match(/^\r?\r?\n/)) {
456
+ toInsert = `,${text.match(/^\r?\n\s*/)[0]}${symbolName}`;
457
+ } else {
458
+ toInsert = `, ${symbolName}`;
459
+ }
460
+ }
461
+ } else if (node.kind == ts.SyntaxKind.ArrayLiteralExpression) {
462
+ // We found the field but it's empty. Insert it just before the `]`.
463
+ position--;
464
+ toInsert = `${symbolName}`;
465
+ } else {
466
+ // Get the indentation of the last element, if any.
467
+ const text = node.getFullText(source);
468
+ if (text.match(/^\r?\n/)) {
469
+ toInsert = `,${text.match(/^\r?\n(\r?)\s*/)[0]}${symbolName}`;
470
+ } else {
471
+ toInsert = `, ${symbolName}`;
472
+ }
473
+ }
474
+ if (importPath !== null) {
475
+ return [
476
+ new InsertChange(ngModulePath, position, toInsert),
477
+ insertImport(source, ngModulePath, symbolName.replace(/\..*$/, ''), importPath),
478
+ ];
479
+ }
480
+
481
+ return [new InsertChange(ngModulePath, position, toInsert)];
482
+ }
483
+
484
+ /**
485
+ * Custom function to insert a declaration (component, pipe, directive)
486
+ * into NgModule declarations. It also imports the component.
487
+ */
488
+ export function addDeclarationToModule(
489
+ source: ts.SourceFile,
490
+ modulePath: string,
491
+ classifiedName: string,
492
+ importPath: string,
493
+ ): Change[] {
494
+ return addSymbolToNgModuleMetadata(
495
+ source,
496
+ modulePath,
497
+ 'declarations',
498
+ classifiedName,
499
+ importPath,
500
+ );
501
+ }
502
+
503
+ /**
504
+ * Custom function to insert an NgModule into NgModule imports. It also imports the module.
505
+ */
506
+ export function addImportToModule(
507
+ source: ts.SourceFile,
508
+ modulePath: string,
509
+ classifiedName: string,
510
+ importPath: string,
511
+ ): Change[] {
512
+ return addSymbolToNgModuleMetadata(source, modulePath, 'imports', classifiedName, importPath);
513
+ }
514
+
515
+ /**
516
+ * Custom function to insert an export into NgModule. It also imports it.
517
+ */
518
+ export function addExportToModule(
519
+ source: ts.SourceFile,
520
+ modulePath: string,
521
+ classifiedName: string,
522
+ importPath: string,
523
+ ): Change[] {
524
+ return addSymbolToNgModuleMetadata(source, modulePath, 'exports', classifiedName, importPath);
525
+ }
526
+
527
+ export function findBootstrapModuleCall(host: Tree, mainPath: string): ts.CallExpression | null {
528
+ const mainBuffer = host.read(mainPath);
529
+ if (!mainBuffer) {
530
+ throw new SchematicsException(`Main file (${mainPath}) not found`);
531
+ }
532
+ const mainText = mainBuffer.toString('utf-8');
533
+ const source = ts.createSourceFile(mainPath, mainText, ts.ScriptTarget.Latest, true);
534
+
535
+ const allNodes = getSourceNodes(source);
536
+
537
+ let bootstrapCall: ts.CallExpression | null = null;
538
+
539
+ for (const node of allNodes) {
540
+ let bootstrapCallNode: ts.Node | null = null;
541
+ bootstrapCallNode = findNode(node, ts.SyntaxKind.Identifier, 'bootstrapModule');
542
+
543
+ // Walk up the parent until CallExpression is found.
544
+ while (
545
+ bootstrapCallNode &&
546
+ bootstrapCallNode.parent &&
547
+ bootstrapCallNode.parent.kind !== ts.SyntaxKind.CallExpression
548
+ ) {
549
+ bootstrapCallNode = bootstrapCallNode.parent;
550
+ }
551
+
552
+ if (
553
+ bootstrapCallNode !== null &&
554
+ bootstrapCallNode.parent !== undefined &&
555
+ bootstrapCallNode.parent.kind === ts.SyntaxKind.CallExpression
556
+ ) {
557
+ bootstrapCall = bootstrapCallNode.parent as ts.CallExpression;
558
+ break;
559
+ }
560
+ }
561
+
562
+ return bootstrapCall;
563
+ }
564
+
565
+ export function findBootstrapModulePath(host: Tree, mainPath: string): string {
566
+ const bootstrapCall = findBootstrapModuleCall(host, mainPath);
567
+ if (!bootstrapCall) {
568
+ throw new SchematicsException('Bootstrap call not found');
569
+ }
570
+
571
+ const bootstrapModule = bootstrapCall.arguments[0];
572
+
573
+ const mainBuffer = host.read(mainPath);
574
+ if (!mainBuffer) {
575
+ throw new SchematicsException(`Client app main file (${mainPath}) not found`);
576
+ }
577
+ const mainText = mainBuffer.toString('utf-8');
578
+ const source = ts.createSourceFile(mainPath, mainText, ts.ScriptTarget.Latest, true);
579
+ const allNodes = getSourceNodes(source);
580
+ const bootstrapModuleRelativePath = allNodes
581
+ .filter(node => node.kind === ts.SyntaxKind.ImportDeclaration)
582
+ .filter(imp => {
583
+ return findNode(imp, ts.SyntaxKind.Identifier, bootstrapModule.getText());
584
+ })
585
+ .map(node => {
586
+ const modulePath = (node as ts.ImportDeclaration).moduleSpecifier as ts.StringLiteral;
587
+ return modulePath.text;
588
+ })[0];
589
+
590
+ return bootstrapModuleRelativePath;
591
+ }
592
+
593
+ export function getAppModulePath(host: Tree, mainPath: string): string {
594
+ const moduleRelativePath = findBootstrapModulePath(host, mainPath);
595
+ const mainDir = dirname(mainPath);
596
+ const modulePath = normalize(`/${mainDir}/${moduleRelativePath}.ts`);
597
+
598
+ return modulePath;
599
+ }