node-module-engine-host.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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 path_1 = require("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. const relativeBase = requester ? (0, path_1.dirname)(requester) : process.cwd();
  43. let collectionPath = undefined;
  44. if (name.startsWith('.')) {
  45. name = (0, path_1.resolve)(relativeBase, name);
  46. }
  47. const resolveOptions = {
  48. paths: requester ? [(0, path_1.dirname)(requester), ...(this.paths || [])] : this.paths,
  49. };
  50. // Try to resolve as a package
  51. try {
  52. const packageJsonPath = require.resolve((0, path_1.join)(name, 'package.json'), resolveOptions);
  53. const { schematics } = require(packageJsonPath);
  54. if (!schematics || typeof schematics !== 'string') {
  55. throw new NodePackageDoesNotSupportSchematics(name);
  56. }
  57. // If this is a relative path to the collection, then create the collection
  58. // path in relation to the package path
  59. if (schematics.startsWith('.')) {
  60. const packageDirectory = (0, path_1.dirname)(packageJsonPath);
  61. collectionPath = (0, path_1.resolve)(packageDirectory, schematics);
  62. }
  63. // Otherwise treat this as a package, and recurse to find the collection path
  64. else {
  65. collectionPath = this.resolve(schematics, packageJsonPath, references);
  66. }
  67. }
  68. catch (e) {
  69. if (e.code !== 'MODULE_NOT_FOUND') {
  70. throw e;
  71. }
  72. }
  73. // If not a package, try to resolve as a file
  74. if (!collectionPath) {
  75. try {
  76. collectionPath = require.resolve(name, resolveOptions);
  77. }
  78. catch (e) {
  79. if (e.code !== 'MODULE_NOT_FOUND') {
  80. throw e;
  81. }
  82. }
  83. }
  84. // If not a package or a file, error
  85. if (!collectionPath) {
  86. throw new file_system_engine_host_base_1.CollectionCannotBeResolvedException(name);
  87. }
  88. return collectionPath;
  89. }
  90. _resolveCollectionPath(name, requester) {
  91. const collectionPath = this.resolve(name, requester);
  92. (0, file_system_utility_1.readJsonFile)(collectionPath);
  93. return collectionPath;
  94. }
  95. _resolveReferenceString(refString, parentPath, collectionDescription) {
  96. const ref = new export_ref_1.ExportStringRef(refString, parentPath);
  97. if (!ref.ref) {
  98. return null;
  99. }
  100. return { ref: ref.ref, path: ref.module };
  101. }
  102. _transformCollectionDescription(name, desc) {
  103. if (!desc.schematics || typeof desc.schematics != 'object') {
  104. throw new file_system_engine_host_base_1.CollectionMissingSchematicsMapException(name);
  105. }
  106. return {
  107. ...desc,
  108. name,
  109. };
  110. }
  111. _transformSchematicDescription(name, _collection, desc) {
  112. if (!desc.factoryFn || !desc.path || !desc.description) {
  113. throw new file_system_engine_host_base_1.SchematicMissingFieldsException(name);
  114. }
  115. return desc;
  116. }
  117. }
  118. exports.NodeModulesEngineHost = NodeModulesEngineHost;