testing-architect-host.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
  10. if (k2 === undefined) k2 = k;
  11. var desc = Object.getOwnPropertyDescriptor(m, k);
  12. if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
  13. desc = { enumerable: true, get: function() { return m[k]; } };
  14. }
  15. Object.defineProperty(o, k2, desc);
  16. }) : (function(o, m, k, k2) {
  17. if (k2 === undefined) k2 = k;
  18. o[k2] = m[k];
  19. }));
  20. var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
  21. Object.defineProperty(o, "default", { enumerable: true, value: v });
  22. }) : function(o, v) {
  23. o["default"] = v;
  24. });
  25. var __importStar = (this && this.__importStar) || (function () {
  26. var ownKeys = function(o) {
  27. ownKeys = Object.getOwnPropertyNames || function (o) {
  28. var ar = [];
  29. for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
  30. return ar;
  31. };
  32. return ownKeys(o);
  33. };
  34. return function (mod) {
  35. if (mod && mod.__esModule) return mod;
  36. var result = {};
  37. if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
  38. __setModuleDefault(result, mod);
  39. return result;
  40. };
  41. })();
  42. Object.defineProperty(exports, "__esModule", { value: true });
  43. exports.TestingArchitectHost = void 0;
  44. const src_1 = require("../src");
  45. class TestingArchitectHost {
  46. workspaceRoot;
  47. currentDirectory;
  48. _backendHost;
  49. _builderImportMap = new Map();
  50. _builderMap = new Map();
  51. _targetMap = new Map();
  52. /**
  53. * Can provide a backend host, in case of integration tests.
  54. * @param workspaceRoot The workspace root to use.
  55. * @param currentDirectory The current directory to use.
  56. * @param _backendHost A host to defer calls that aren't resolved here.
  57. */
  58. constructor(workspaceRoot = '', currentDirectory = workspaceRoot, _backendHost = null) {
  59. this.workspaceRoot = workspaceRoot;
  60. this.currentDirectory = currentDirectory;
  61. this._backendHost = _backendHost;
  62. }
  63. addBuilder(builderName, builder, description = 'Testing only builder.', optionSchema = { type: 'object' }) {
  64. this._builderImportMap.set(builderName, builder);
  65. this._builderMap.set(builderName, { builderName, description, optionSchema });
  66. }
  67. async addBuilderFromPackage(packageName) {
  68. const packageJson = await Promise.resolve(`${packageName + '/package.json'}`).then(s => __importStar(require(s)));
  69. if (!('builders' in packageJson)) {
  70. throw new Error('Invalid package.json, builders key not found.');
  71. }
  72. if (!packageJson.name) {
  73. throw new Error('Invalid package name');
  74. }
  75. const builderJsonPath = packageName + '/' + packageJson['builders'];
  76. const builderJson = await Promise.resolve(`${builderJsonPath}`).then(s => __importStar(require(s)));
  77. const builders = builderJson['builders'];
  78. if (!builders) {
  79. throw new Error('Invalid builders.json, builders key not found.');
  80. }
  81. for (const builderName of Object.keys(builders)) {
  82. const b = builders[builderName];
  83. // TODO: remove this check as v1 is not supported anymore.
  84. if (!b.implementation) {
  85. continue;
  86. }
  87. const handler = (await Promise.resolve(`${builderJsonPath + '/../' + b.implementation}`).then(s => __importStar(require(s)))).default;
  88. const optionsSchema = await Promise.resolve(`${builderJsonPath + '/../' + b.schema}`).then(s => __importStar(require(s)));
  89. this.addBuilder(`${packageJson.name}:${builderName}`, handler, b.description, optionsSchema);
  90. }
  91. }
  92. addTarget(target, builderName, options = {}) {
  93. this._targetMap.set((0, src_1.targetStringFromTarget)(target), { builderName, options });
  94. }
  95. async getBuilderNameForTarget(target) {
  96. const name = (0, src_1.targetStringFromTarget)(target);
  97. const maybeTarget = this._targetMap.get(name);
  98. if (!maybeTarget) {
  99. return this._backendHost && this._backendHost.getBuilderNameForTarget(target);
  100. }
  101. return maybeTarget.builderName;
  102. }
  103. /**
  104. * Resolve a builder. This needs to return a string which will be used in a dynamic `import()`
  105. * clause. This should throw if no builder can be found. The dynamic import will throw if
  106. * it is unsupported.
  107. * @param builderName The name of the builder to be used.
  108. * @returns All the info needed for the builder itself.
  109. */
  110. async resolveBuilder(builderName) {
  111. return (this._builderMap.get(builderName) ||
  112. (this._backendHost && this._backendHost.resolveBuilder(builderName)));
  113. }
  114. async getCurrentDirectory() {
  115. return this.currentDirectory;
  116. }
  117. async getWorkspaceRoot() {
  118. return this.workspaceRoot;
  119. }
  120. async getOptionsForTarget(target) {
  121. const name = (0, src_1.targetStringFromTarget)(target);
  122. const maybeTarget = this._targetMap.get(name);
  123. if (!maybeTarget) {
  124. return this._backendHost && this._backendHost.getOptionsForTarget(target);
  125. }
  126. return maybeTarget.options;
  127. }
  128. async getProjectMetadata(target) {
  129. return this._backendHost && this._backendHost.getProjectMetadata(target);
  130. }
  131. async loadBuilder(info) {
  132. return (this._builderImportMap.get(info.builderName) ||
  133. (this._backendHost && this._backendHost.loadBuilder(info)));
  134. }
  135. }
  136. exports.TestingArchitectHost = TestingArchitectHost;