index.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. "use strict";
  2. /**
  3. * @license
  4. * Copyright Google LLC All Rights Reserved.
  5. *
  6. * Use of this source code is governed by an MIT-style license that can be
  7. * found in the LICENSE file at https://angular.dev/license
  8. */
  9. Object.defineProperty(exports, "__esModule", { value: true });
  10. exports.default = default_1;
  11. const tasks_1 = require("@angular-devkit/schematics/tasks");
  12. const package_config_1 = require("./package-config");
  13. /**
  14. * Version range that will be used for the Angular CDK and Angular Material if this
  15. * schematic has been run outside of the CLI `ng add` command. In those cases, there
  16. * can be no dependency on `@angular/material` in the `package.json` file, and we need
  17. * to manually insert the dependency based on the build version placeholder.
  18. *
  19. * Note that the fallback version range does not use caret, but tilde because that is
  20. * the default for Angular framework dependencies in CLI projects.
  21. */
  22. const fallbackMaterialVersionRange = `~19.2.17`;
  23. /**
  24. * Schematic factory entry-point for the `ng-add` schematic. The ng-add schematic will be
  25. * automatically executed if developers run `ng add @angular/material`.
  26. *
  27. * Since the Angular Material schematics depend on the schematic utility functions from the CDK,
  28. * we need to install the CDK before loading the schematic files that import from the CDK.
  29. */
  30. function default_1(options) {
  31. return (host, context) => {
  32. // Version tag of the `@angular/core` dependency that has been loaded from the `package.json`
  33. // of the CLI project. This tag should be preferred because all Angular dependencies should
  34. // have the same version tag if possible.
  35. const ngCoreVersionTag = (0, package_config_1.getPackageVersionFromPackageJson)(host, '@angular/core');
  36. const materialVersionRange = (0, package_config_1.getPackageVersionFromPackageJson)(host, '@angular/material');
  37. const angularDependencyVersion = ngCoreVersionTag || `^19.0.0 || ^20.0.0`;
  38. // The CLI inserts `@angular/material` into the `package.json` before this schematic runs.
  39. // This means that we do not need to insert Angular Material into `package.json` files again.
  40. // In some cases though, it could happen that this schematic runs outside of the CLI `ng add`
  41. // command, or Material is only listed a dev dependency. If that is the case, we insert a
  42. // version based on the current build version (substituted version placeholder).
  43. if (materialVersionRange === null) {
  44. (0, package_config_1.addPackageToPackageJson)(host, '@angular/material', fallbackMaterialVersionRange);
  45. }
  46. (0, package_config_1.addPackageToPackageJson)(host, '@angular/cdk', materialVersionRange || fallbackMaterialVersionRange);
  47. (0, package_config_1.addPackageToPackageJson)(host, '@angular/forms', angularDependencyVersion);
  48. // Since the Angular Material schematics depend on the schematic utility functions from the
  49. // CDK, we need to install the CDK before loading the schematic files that import from the CDK.
  50. const installTaskId = context.addTask(new tasks_1.NodePackageInstallTask());
  51. context.addTask(new tasks_1.RunSchematicTask('ng-add-setup-project', options), [installTaskId]);
  52. };
  53. }
  54. //# sourceMappingURL=index.js.map