fs/lib/DuplexCollection.js

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