node-module-engine-host.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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.NodeModulesEngineHost = exports.NodePackageDoesNotSupportSchematics = void 0;
  11. const core_1 = require("@angular-devkit/core");
  12. const node_path_1 = require("node:path");
  13. const export_ref_1 = require("./export-ref");
  14. const file_system_engine_host_base_1 = require("./file-system-engine-host-base");
  15. const file_system_utility_1 = require("./file-system-utility");
  16. class NodePackageDoesNotSupportSchematics extends core_1.BaseException {
  17. constructor(name) {
  18. super(`Package ${JSON.stringify(name)} was found but does not support schematics.`);
  19. }
  20. }
  21. exports.NodePackageDoesNotSupportSchematics = NodePackageDoesNotSupportSchematics;
  22. /**
  23. * A simple EngineHost that uses NodeModules to resolve collections.
  24. */
  25. class NodeModulesEngineHost extends file_system_engine_host_base_1.FileSystemEngineHostBase {
  26. paths;
  27. constructor(paths) {
  28. super();
  29. this.paths = paths;
  30. }
  31. resolve(name, requester, references = new Set()) {
  32. // Keep track of the package requesting the schematic, in order to avoid infinite recursion
  33. if (requester) {
  34. if (references.has(requester)) {
  35. references.add(requester);
  36. throw new Error('Circular schematic reference detected: ' + JSON.stringify(Array.from(references)));
  37. }
  38. else {
  39. references.add(requester);
  40. }
  41. }
  42. let collectionPath = undefined;
  43. const resolveOptions = {
  44. paths: requester ? [(0, node_path_1.dirname)(requester), ...(this.paths || [])] : this.paths,
  45. };
  46. // Try to resolve as a package
  47. try {
  48. const packageJsonPath = require.resolve(`${name}/package.json`, resolveOptions);
  49. const { schematics } = require(packageJsonPath);
  50. if (!schematics || typeof schematics !== 'string') {
  51. throw new NodePackageDoesNotSupportSchematics(name);
  52. }
  53. // If this is a relative path to the collection, then create the collection
  54. // path in relation to the package path
  55. if (schematics.startsWith('.')) {
  56. const packageDirectory = (0, node_path_1.dirname)(packageJsonPath);
  57. collectionPath = (0, node_path_1.resolve)(packageDirectory, schematics);
  58. }
  59. // Otherwise treat this as a package, and recurse to find the collection path
  60. else {
  61. collectionPath = this.resolve(schematics, packageJsonPath, references);
  62. }
  63. }
  64. catch (e) {
  65. if (e.code !== 'MODULE_NOT_FOUND') {
  66. throw e;
  67. }
  68. }
  69. // If not a package, try to resolve as a file
  70. if (!collectionPath) {
  71. try {
  72. collectionPath = require.resolve(name, resolveOptions);
  73. }
  74. catch (e) {
  75. if (e.code !== 'MODULE_NOT_FOUND') {
  76. throw e;
  77. }
  78. }
  79. }
  80. // If not a package or a file, error
  81. if (!collectionPath) {
  82. throw new file_system_engine_host_base_1.CollectionCannotBeResolvedException(name);
  83. }
  84. return collectionPath;
  85. }
  86. _resolveCollectionPath(name, requester) {
  87. const collectionPath = this.resolve(name, requester);
  88. (0, file_system_utility_1.readJsonFile)(collectionPath);
  89. return collectionPath;
  90. }
  91. _resolveReferenceString(refString, parentPath, collectionDescription) {
  92. const ref = new export_ref_1.ExportStringRef(refString, parentPath);
  93. if (!ref.ref) {
  94. return null;
  95. }
  96. return { ref: ref.ref, path: ref.module };
  97. }
  98. _transformCollectionDescription(name, desc) {
  99. if (!desc.schematics || typeof desc.schematics != 'object') {
  100. throw new file_system_engine_host_base_1.CollectionMissingSchematicsMapException(name);
  101. }
  102. return {
  103. ...desc,
  104. name,
  105. };
  106. }
  107. _transformSchematicDescription(name, _collection, desc) {
  108. if (!desc.factoryFn || !desc.path || !desc.description) {
  109. throw new file_system_engine_host_base_1.SchematicMissingFieldsException(name);
  110. }
  111. return desc;
  112. }
  113. }
  114. exports.NodeModulesEngineHost = NodeModulesEngineHost;