file-system-engine-host.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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 (mod) {
  26. if (mod && mod.__esModule) return mod;
  27. var result = {};
  28. if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
  29. __setModuleDefault(result, mod);
  30. return result;
  31. };
  32. Object.defineProperty(exports, "__esModule", { value: true });
  33. exports.FileSystemEngineHost = void 0;
  34. const fs_1 = require("fs");
  35. const path_1 = require("path");
  36. const rxjs_1 = require("rxjs");
  37. const src_1 = require("../src");
  38. const export_ref_1 = require("./export-ref");
  39. const file_system_engine_host_base_1 = require("./file-system-engine-host-base");
  40. /**
  41. * A simple EngineHost that uses a root with one directory per collection inside of it. The
  42. * collection declaration follows the same rules as the regular FileSystemEngineHostBase.
  43. */
  44. class FileSystemEngineHost extends file_system_engine_host_base_1.FileSystemEngineHostBase {
  45. _root;
  46. constructor(_root) {
  47. super();
  48. this._root = _root;
  49. }
  50. _resolveCollectionPath(name) {
  51. try {
  52. // Allow `${_root}/${name}.json` as a collection.
  53. const maybePath = require.resolve((0, path_1.join)(this._root, name + '.json'));
  54. if ((0, fs_1.existsSync)(maybePath)) {
  55. return maybePath;
  56. }
  57. }
  58. catch (error) { }
  59. try {
  60. // Allow `${_root}/${name}/collection.json.
  61. const maybePath = require.resolve((0, path_1.join)(this._root, name, 'collection.json'));
  62. if ((0, fs_1.existsSync)(maybePath)) {
  63. return maybePath;
  64. }
  65. }
  66. catch (error) { }
  67. throw new file_system_engine_host_base_1.CollectionCannotBeResolvedException(name);
  68. }
  69. _resolveReferenceString(refString, parentPath) {
  70. // Use the same kind of export strings as NodeModule.
  71. const ref = new export_ref_1.ExportStringRef(refString, parentPath);
  72. if (!ref.ref) {
  73. return null;
  74. }
  75. return { ref: ref.ref, path: ref.module };
  76. }
  77. _transformCollectionDescription(name, desc) {
  78. if (!desc.schematics || typeof desc.schematics != 'object') {
  79. throw new file_system_engine_host_base_1.CollectionMissingSchematicsMapException(name);
  80. }
  81. return {
  82. ...desc,
  83. name,
  84. };
  85. }
  86. _transformSchematicDescription(name, _collection, desc) {
  87. if (!desc.factoryFn || !desc.path || !desc.description) {
  88. throw new file_system_engine_host_base_1.SchematicMissingFieldsException(name);
  89. }
  90. return desc;
  91. }
  92. hasTaskExecutor(name) {
  93. if (super.hasTaskExecutor(name)) {
  94. return true;
  95. }
  96. try {
  97. const maybePath = require.resolve((0, path_1.join)(this._root, name));
  98. if ((0, fs_1.existsSync)(maybePath)) {
  99. return true;
  100. }
  101. }
  102. catch { }
  103. return false;
  104. }
  105. createTaskExecutor(name) {
  106. if (!super.hasTaskExecutor(name)) {
  107. try {
  108. const path = require.resolve((0, path_1.join)(this._root, name));
  109. // Default handling code is for old tasks that incorrectly export `default` with non-ESM module
  110. 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))));
  111. }
  112. catch { }
  113. }
  114. return super.createTaskExecutor(name);
  115. }
  116. }
  117. exports.FileSystemEngineHost = FileSystemEngineHost;