node-modules-architect-host.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. const node_1 = require("@angular-devkit/core/node");
  4. const path = require("path");
  5. const internal_1 = require("../src/internal");
  6. // TODO: create a base class for all workspace related hosts.
  7. class WorkspaceNodeModulesArchitectHost {
  8. constructor(_workspace, _root) {
  9. this._workspace = _workspace;
  10. this._root = _root;
  11. }
  12. async getBuilderNameForTarget(target) {
  13. const targetDefinition = this.findProjectTarget(target);
  14. if (!targetDefinition) {
  15. throw new Error('Project target does not exist.');
  16. }
  17. return targetDefinition.builder;
  18. }
  19. /**
  20. * Resolve a builder. This needs to be a string which will be used in a dynamic `import()`
  21. * clause. This should throw if no builder can be found. The dynamic import will throw if
  22. * it is unsupported.
  23. * @param builderStr The name of the builder to be used.
  24. * @returns All the info needed for the builder itself.
  25. */
  26. resolveBuilder(builderStr) {
  27. const [packageName, builderName] = builderStr.split(':', 2);
  28. if (!builderName) {
  29. throw new Error('No builder name specified.');
  30. }
  31. const packageJsonPath = node_1.resolve(packageName, {
  32. basedir: this._root,
  33. checkLocal: true,
  34. checkGlobal: true,
  35. resolvePackageJson: true,
  36. });
  37. const packageJson = require(packageJsonPath);
  38. if (!packageJson['builders']) {
  39. throw new Error(`Package ${JSON.stringify(packageName)} has no builders defined.`);
  40. }
  41. const builderJsonPath = path.resolve(path.dirname(packageJsonPath), packageJson['builders']);
  42. const builderJson = require(builderJsonPath);
  43. const builder = builderJson.builders && builderJson.builders[builderName];
  44. if (!builder) {
  45. throw new Error(`Cannot find builder ${JSON.stringify(builderStr)}.`);
  46. }
  47. const importPath = builder.implementation;
  48. if (!importPath) {
  49. throw new Error('Could not find the implementation for builder ' + builderStr);
  50. }
  51. return Promise.resolve({
  52. name: builderStr,
  53. builderName,
  54. description: builder['description'],
  55. optionSchema: require(path.resolve(path.dirname(builderJsonPath), builder.schema)),
  56. import: path.resolve(path.dirname(builderJsonPath), importPath),
  57. });
  58. }
  59. async getCurrentDirectory() {
  60. return process.cwd();
  61. }
  62. async getWorkspaceRoot() {
  63. return this._root;
  64. }
  65. async getOptionsForTarget(target) {
  66. const targetSpec = this.findProjectTarget(target);
  67. if (targetSpec === undefined) {
  68. return null;
  69. }
  70. if (target.configuration
  71. && !(targetSpec['configurations'] && targetSpec['configurations'][target.configuration])) {
  72. throw new Error(`Configuration '${target.configuration}' is not set in the workspace.`);
  73. }
  74. return {
  75. ...targetSpec['options'],
  76. ...(target.configuration ? targetSpec['configurations'][target.configuration] : 0),
  77. };
  78. }
  79. async loadBuilder(info) {
  80. const builder = (await Promise.resolve().then(() => require(info.import))).default;
  81. if (builder[internal_1.BuilderSymbol]) {
  82. return builder;
  83. }
  84. throw new Error('Builder is not a builder');
  85. }
  86. findProjectTarget(target) {
  87. // NOTE: Remove conditional when deprecated support for experimental workspace API is removed.
  88. if ('getProjectTargets' in this._workspace) {
  89. return this._workspace.getProjectTargets(target.project)[target.target];
  90. }
  91. else {
  92. const projectDefinition = this._workspace.projects.get(target.project);
  93. if (!projectDefinition) {
  94. throw new Error('Project does not exist.');
  95. }
  96. return projectDefinition.targets.get(target.target);
  97. }
  98. }
  99. }
  100. exports.WorkspaceNodeModulesArchitectHost = WorkspaceNodeModulesArchitectHost;