file-system-engine-host.js 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. /**
  4. * @license
  5. * Copyright Google Inc. All Rights Reserved.
  6. *
  7. * Use of this source code is governed by an MIT-style license that can be
  8. * found in the LICENSE file at https://angular.io/license
  9. */
  10. const fs_1 = require("fs");
  11. const path_1 = require("path");
  12. const rxjs_1 = require("rxjs");
  13. const operators_1 = require("rxjs/operators");
  14. const src_1 = require("../src");
  15. const export_ref_1 = require("./export-ref");
  16. const file_system_engine_host_base_1 = require("./file-system-engine-host-base");
  17. /**
  18. * A simple EngineHost that uses a root with one directory per collection inside of it. The
  19. * collection declaration follows the same rules as the regular FileSystemEngineHostBase.
  20. */
  21. class FileSystemEngineHost extends file_system_engine_host_base_1.FileSystemEngineHostBase {
  22. constructor(_root) {
  23. super();
  24. this._root = _root;
  25. }
  26. _resolveCollectionPath(name) {
  27. try {
  28. // Allow `${_root}/${name}.json` as a collection.
  29. const maybePath = require.resolve(path_1.join(this._root, name + '.json'));
  30. if (fs_1.existsSync(maybePath)) {
  31. return maybePath;
  32. }
  33. }
  34. catch (error) { }
  35. try {
  36. // Allow `${_root}/${name}/collection.json.
  37. const maybePath = require.resolve(path_1.join(this._root, name, 'collection.json'));
  38. if (fs_1.existsSync(maybePath)) {
  39. return maybePath;
  40. }
  41. }
  42. catch (error) { }
  43. throw new file_system_engine_host_base_1.CollectionCannotBeResolvedException(name);
  44. }
  45. _resolveReferenceString(refString, parentPath) {
  46. // Use the same kind of export strings as NodeModule.
  47. const ref = new export_ref_1.ExportStringRef(refString, parentPath);
  48. if (!ref.ref) {
  49. return null;
  50. }
  51. return { ref: ref.ref, path: ref.module };
  52. }
  53. _transformCollectionDescription(name, desc) {
  54. if (!desc.schematics || typeof desc.schematics != 'object') {
  55. throw new file_system_engine_host_base_1.CollectionMissingSchematicsMapException(name);
  56. }
  57. return {
  58. ...desc,
  59. name,
  60. };
  61. }
  62. _transformSchematicDescription(name, _collection, desc) {
  63. if (!desc.factoryFn || !desc.path || !desc.description) {
  64. throw new file_system_engine_host_base_1.SchematicMissingFieldsException(name);
  65. }
  66. return desc;
  67. }
  68. hasTaskExecutor(name) {
  69. if (super.hasTaskExecutor(name)) {
  70. return true;
  71. }
  72. try {
  73. const maybePath = require.resolve(path_1.join(this._root, name));
  74. if (fs_1.existsSync(maybePath)) {
  75. return true;
  76. }
  77. }
  78. catch (_a) { }
  79. return false;
  80. }
  81. createTaskExecutor(name) {
  82. if (!super.hasTaskExecutor(name)) {
  83. try {
  84. const path = require.resolve(path_1.join(this._root, name));
  85. return rxjs_1.from(Promise.resolve().then(() => require(path)).then(mod => mod.default())).pipe(operators_1.catchError(() => rxjs_1.throwError(new src_1.UnregisteredTaskException(name))));
  86. }
  87. catch (_a) { }
  88. }
  89. return super.createTaskExecutor(name);
  90. }
  91. }
  92. exports.FileSystemEngineHost = FileSystemEngineHost;