testing-architect-host.js 4.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. const src_1 = require("../src");
  4. class TestingArchitectHost {
  5. /**
  6. * Can provide a backend host, in case of integration tests.
  7. * @param workspaceRoot The workspace root to use.
  8. * @param currentDirectory The current directory to use.
  9. * @param _backendHost A host to defer calls that aren't resolved here.
  10. */
  11. constructor(workspaceRoot = '', currentDirectory = workspaceRoot, _backendHost = null) {
  12. this.workspaceRoot = workspaceRoot;
  13. this.currentDirectory = currentDirectory;
  14. this._backendHost = _backendHost;
  15. this._builderImportMap = new Map();
  16. this._builderMap = new Map();
  17. this._targetMap = new Map();
  18. }
  19. addBuilder(builderName, builder, description = 'Testing only builder.', optionSchema = { type: 'object' }) {
  20. this._builderImportMap.set(builderName, builder);
  21. this._builderMap.set(builderName, { builderName, description, optionSchema });
  22. }
  23. async addBuilderFromPackage(packageName) {
  24. const packageJson = await Promise.resolve().then(() => require(packageName + '/package.json'));
  25. if (!('builders' in packageJson)) {
  26. throw new Error('Invalid package.json, builders key not found.');
  27. }
  28. if (!packageJson.name) {
  29. throw new Error('Invalid package name');
  30. }
  31. const builderJsonPath = packageName + '/' + packageJson['builders'];
  32. const builderJson = await Promise.resolve().then(() => require(builderJsonPath));
  33. const builders = builderJson['builders'];
  34. if (!builders) {
  35. throw new Error('Invalid builders.json, builders key not found.');
  36. }
  37. for (const builderName of Object.keys(builders)) {
  38. const b = builders[builderName];
  39. // TODO: remove this check as v1 is not supported anymore.
  40. if (!b.implementation) {
  41. continue;
  42. }
  43. const handler = (await Promise.resolve().then(() => require(builderJsonPath + '/../' + b.implementation))).default;
  44. const optionsSchema = await Promise.resolve().then(() => require(builderJsonPath + '/../' + b.schema));
  45. this.addBuilder(`${packageJson.name}:${builderName}`, handler, b.description, optionsSchema);
  46. }
  47. }
  48. addTarget(target, builderName, options = {}) {
  49. this._targetMap.set(src_1.targetStringFromTarget(target), { builderName, options });
  50. }
  51. async getBuilderNameForTarget(target) {
  52. const name = src_1.targetStringFromTarget(target);
  53. const maybeTarget = this._targetMap.get(name);
  54. if (!maybeTarget) {
  55. return this._backendHost && this._backendHost.getBuilderNameForTarget(target);
  56. }
  57. return maybeTarget.builderName;
  58. }
  59. /**
  60. * Resolve a builder. This needs to return a string which will be used in a dynamic `import()`
  61. * clause. This should throw if no builder can be found. The dynamic import will throw if
  62. * it is unsupported.
  63. * @param builderName The name of the builder to be used.
  64. * @returns All the info needed for the builder itself.
  65. */
  66. async resolveBuilder(builderName) {
  67. return this._builderMap.get(builderName)
  68. || (this._backendHost && this._backendHost.resolveBuilder(builderName));
  69. }
  70. async getCurrentDirectory() {
  71. return this.currentDirectory;
  72. }
  73. async getWorkspaceRoot() {
  74. return this.workspaceRoot;
  75. }
  76. async getOptionsForTarget(target) {
  77. const name = src_1.targetStringFromTarget(target);
  78. const maybeTarget = this._targetMap.get(name);
  79. if (!maybeTarget) {
  80. return this._backendHost && this._backendHost.getOptionsForTarget(target);
  81. }
  82. return maybeTarget.options;
  83. }
  84. async loadBuilder(info) {
  85. return this._builderImportMap.get(info.builderName)
  86. || (this._backendHost && this._backendHost.loadBuilder(info));
  87. }
  88. }
  89. exports.TestingArchitectHost = TestingArchitectHost;