file-system-engine-host.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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.FileSystemEngineHost = void 0;
  44. const node_fs_1 = require("node:fs");
  45. const node_path_1 = require("node:path");
  46. const rxjs_1 = require("rxjs");
  47. const src_1 = require("../src");
  48. const export_ref_1 = require("./export-ref");
  49. const file_system_engine_host_base_1 = require("./file-system-engine-host-base");
  50. /**
  51. * A simple EngineHost that uses a root with one directory per collection inside of it. The
  52. * collection declaration follows the same rules as the regular FileSystemEngineHostBase.
  53. */
  54. class FileSystemEngineHost extends file_system_engine_host_base_1.FileSystemEngineHostBase {
  55. _root;
  56. constructor(_root) {
  57. super();
  58. this._root = _root;
  59. }
  60. _resolveCollectionPath(name) {
  61. try {
  62. // Allow `${_root}/${name}.json` as a collection.
  63. const maybePath = require.resolve((0, node_path_1.join)(this._root, name + '.json'));
  64. if ((0, node_fs_1.existsSync)(maybePath)) {
  65. return maybePath;
  66. }
  67. }
  68. catch (error) { }
  69. try {
  70. // Allow `${_root}/${name}/collection.json.
  71. const maybePath = require.resolve((0, node_path_1.join)(this._root, name, 'collection.json'));
  72. if ((0, node_fs_1.existsSync)(maybePath)) {
  73. return maybePath;
  74. }
  75. }
  76. catch (error) { }
  77. throw new file_system_engine_host_base_1.CollectionCannotBeResolvedException(name);
  78. }
  79. _resolveReferenceString(refString, parentPath) {
  80. // Use the same kind of export strings as NodeModule.
  81. const ref = new export_ref_1.ExportStringRef(refString, parentPath);
  82. if (!ref.ref) {
  83. return null;
  84. }
  85. return { ref: ref.ref, path: ref.module };
  86. }
  87. _transformCollectionDescription(name, desc) {
  88. if (!desc.schematics || typeof desc.schematics != 'object') {
  89. throw new file_system_engine_host_base_1.CollectionMissingSchematicsMapException(name);
  90. }
  91. return {
  92. ...desc,
  93. name,
  94. };
  95. }
  96. _transformSchematicDescription(name, _collection, desc) {
  97. if (!desc.factoryFn || !desc.path || !desc.description) {
  98. throw new file_system_engine_host_base_1.SchematicMissingFieldsException(name);
  99. }
  100. return desc;
  101. }
  102. hasTaskExecutor(name) {
  103. if (super.hasTaskExecutor(name)) {
  104. return true;
  105. }
  106. try {
  107. const maybePath = require.resolve((0, node_path_1.join)(this._root, name));
  108. if ((0, node_fs_1.existsSync)(maybePath)) {
  109. return true;
  110. }
  111. }
  112. catch { }
  113. return false;
  114. }
  115. createTaskExecutor(name) {
  116. if (!super.hasTaskExecutor(name)) {
  117. try {
  118. const path = require.resolve((0, node_path_1.join)(this._root, name));
  119. // Default handling code is for old tasks that incorrectly export `default` with non-ESM module
  120. return (0, rxjs_1.from)(Promise.resolve(`${path}`).then(s => __importStar(require(s))).then((mod) => (mod.default?.default || mod.default)())).pipe((0, rxjs_1.catchError)(() => (0, rxjs_1.throwError)(() => new src_1.UnregisteredTaskException(name))));
  121. }
  122. catch { }
  123. }
  124. return super.createTaskExecutor(name);
  125. }
  126. }
  127. exports.FileSystemEngineHost = FileSystemEngineHost;