fallback-engine-host.js 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. /**
  4. * @license
  5. * Copyright Google Inc. All Rights Reserved.
  6. *
  7. * Use of this source code is governed by an MIT-style license that can be
  8. * found in the LICENSE file at https://angular.io/license
  9. */
  10. const rxjs_1 = require("rxjs");
  11. const operators_1 = require("rxjs/operators");
  12. const src_1 = require("../src");
  13. /**
  14. * An EngineHost that support multiple hosts in a fallback configuration. If a host does not
  15. * have a collection/schematics, use the following host before giving up.
  16. */
  17. class FallbackEngineHost {
  18. constructor() {
  19. this._hosts = [];
  20. }
  21. addHost(host) {
  22. this._hosts.push(host);
  23. }
  24. createCollectionDescription(name) {
  25. for (const host of this._hosts) {
  26. try {
  27. const description = host.createCollectionDescription(name);
  28. return { name, host, description };
  29. }
  30. catch (_) {
  31. }
  32. }
  33. throw new src_1.UnknownCollectionException(name);
  34. }
  35. createSchematicDescription(name, collection) {
  36. const description = collection.host.createSchematicDescription(name, collection.description);
  37. if (!description) {
  38. return null;
  39. }
  40. return { name, collection, description };
  41. }
  42. getSchematicRuleFactory(schematic, collection) {
  43. return collection.host.getSchematicRuleFactory(schematic.description, collection.description);
  44. }
  45. createSourceFromUrl(url, context) {
  46. return context.schematic.collection.description.host.createSourceFromUrl(url, context);
  47. }
  48. transformOptions(schematic, options, context) {
  49. // tslint:disable-next-line:no-any https://github.com/ReactiveX/rxjs/issues/3989
  50. return (rxjs_1.of(options)
  51. .pipe(...this._hosts
  52. .map(host => operators_1.mergeMap((opt) => host.transformOptions(schematic, opt, context)))));
  53. }
  54. transformContext(context) {
  55. let result = context;
  56. this._hosts.forEach(host => {
  57. result = (host.transformContext(result) || result);
  58. });
  59. return result;
  60. }
  61. /**
  62. * @deprecated Use `listSchematicNames`.
  63. */
  64. listSchematics(collection) {
  65. return this.listSchematicNames(collection.description);
  66. }
  67. listSchematicNames(collection) {
  68. const allNames = new Set();
  69. this._hosts.forEach(host => {
  70. try {
  71. host.listSchematicNames(collection.description).forEach(name => allNames.add(name));
  72. }
  73. catch (_) { }
  74. });
  75. return [...allNames];
  76. }
  77. createTaskExecutor(name) {
  78. for (const host of this._hosts) {
  79. if (host.hasTaskExecutor(name)) {
  80. return host.createTaskExecutor(name);
  81. }
  82. }
  83. return rxjs_1.throwError(new src_1.UnregisteredTaskException(name));
  84. }
  85. hasTaskExecutor(name) {
  86. for (const host of this._hosts) {
  87. if (host.hasTaskExecutor(name)) {
  88. return true;
  89. }
  90. }
  91. return false;
  92. }
  93. }
  94. exports.FallbackEngineHost = FallbackEngineHost;