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 path.
  54. *
  55. * @private
  56. * @param {string} virPath Virtual path
  57. * @param {object} options Options
  58. * @param {@ui5/fs/tracing.Trace} trace Trace instance
  59. * @returns {Promise<@ui5/fs/Resource|null>}
  60. * Promise resolving to a single resource or <code>null</code> if no resource is found
  61. */
  62. _byPath(virPath, options, trace) {
  63. return this._combo._byPath(virPath, options, trace);
  64. }
  65. /**
  66. * Writes the content of a resource to a path.
  67. *
  68. * @private
  69. * @param {@ui5/fs/Resource} resource The Resource to write
  70. * @returns {Promise<undefined>} Promise resolving once data has been written
  71. */
  72. _write(resource) {
  73. return this._writer.write(resource);
  74. }
  75. }
  76. export default DuplexCollection;