fs/lib/DuplexCollection.js

  1. import AbstractReaderWriter from "./AbstractReaderWriter.js";
  2. import ReaderCollectionPrioritized from "./ReaderCollectionPrioritized.js";
  3. /**
  4. * Wrapper to keep readers and writers together
  5. *
  6. * @public
  7. * @class
  8. * @alias @ui5/fs/DuplexCollection
  9. * @extends @ui5/fs/AbstractReaderWriter
  10. */
  11. class DuplexCollection extends AbstractReaderWriter {
  12. /**
  13. * The Constructor.
  14. *
  15. * @param {object} parameters
  16. * @param {@ui5/fs/AbstractReader} parameters.reader Single reader or collection of readers
  17. * @param {@ui5/fs/AbstractReaderWriter} parameters.writer
  18. * A ReaderWriter instance which is only used for writing files
  19. * @param {string} [parameters.name=""] The collection name
  20. */
  21. constructor({reader, writer, name = ""}) {
  22. super(name);
  23. if (!reader) {
  24. throw new Error(`Cannot create DuplexCollection ${this._name}: No reader provided`);
  25. }
  26. if (!writer) {
  27. throw new Error(`Cannot create DuplexCollection ${this._name}: No writer provided`);
  28. }
  29. this._reader = reader;
  30. this._writer = writer;
  31. this._combo = new ReaderCollectionPrioritized({
  32. name: `${name} - ReaderCollectionPrioritized`,
  33. readers: [
  34. writer,
  35. reader
  36. ]
  37. });
  38. }
  39. /**
  40. * Locates resources by glob.
  41. *
  42. * @private
  43. * @param {string|string[]} virPattern glob pattern as string or an array of
  44. * glob patterns for virtual directory structure
  45. * @param {object} options glob options
  46. * @param {@ui5/fs/tracing.Trace} trace Trace instance
  47. * @returns {Promise<@ui5/fs/Resource[]>} Promise resolving with a list of resources
  48. */
  49. _byGlob(virPattern, options, trace) {
  50. return this._combo._byGlob(virPattern, options, trace);
  51. }
  52. /**
  53. * Locates resources by glob from source reader only.
  54. * For found resources that are also available in the writer, the writer resource will be returned.
  55. *
  56. * @param {string} virPattern glob pattern for virtual directory structure
  57. * @param {object} [options] glob options
  58. * @param {boolean} [options.nodir=true] Do not match directories
  59. * @returns {Promise<@ui5/fs/Resource[]>} Promise resolving to list of resources
  60. */
  61. byGlobSource(virPattern, options = {nodir: true}) {
  62. return this._reader.byGlob(virPattern, options).then((resources) => {
  63. return Promise.all(resources.map((readerResource) => {
  64. return this._writer.byPath(readerResource.getPath()).then((writerResource) => {
  65. return writerResource || readerResource;
  66. });
  67. }));
  68. });
  69. }
  70. /**
  71. * Locates resources by path.
  72. *
  73. * @private
  74. * @param {string} virPath Virtual path
  75. * @param {object} options Options
  76. * @param {@ui5/fs/tracing.Trace} trace Trace instance
  77. * @returns {Promise<@ui5/fs/Resource|null>}
  78. * Promise resolving to a single resource or <code>null</code> if no resource is found
  79. */
  80. _byPath(virPath, options, trace) {
  81. return this._combo._byPath(virPath, options, trace);
  82. }
  83. /**
  84. * Writes the content of a resource to a path.
  85. *
  86. * @private
  87. * @param {@ui5/fs/Resource} resource The Resource to write
  88. * @returns {Promise<undefined>} Promise resolving once data has been written
  89. */
  90. _write(resource) {
  91. return this._writer.write(resource);
  92. }
  93. }
  94. export default DuplexCollection;