fs/lib/AbstractReader.js

  1. const randomInt = require("random-int");
  2. const Trace = require("./tracing/Trace");
  3. /**
  4. * Abstract resource locator
  5. *
  6. * @public
  7. * @abstract
  8. * @memberof module:@ui5/fs
  9. */
  10. class AbstractReader {
  11. /**
  12. * The constructor.
  13. *
  14. * @public
  15. */
  16. constructor() {
  17. if (new.target === AbstractReader) {
  18. throw new TypeError("Class 'AbstractReader' is abstract");
  19. }
  20. }
  21. /**
  22. * Locates resources by matching glob patterns.
  23. *
  24. * @example
  25. * byGlob("**‏/*.{html,htm}");
  26. * byGlob("**‏/.library");
  27. * byGlob("/pony/*");
  28. *
  29. * @public
  30. * @param {string|string[]} virPattern glob pattern as string or array of glob patterns for
  31. * virtual directory structure
  32. * @param {object} [options] glob options
  33. * @param {boolean} [options.nodir=true] Do not match directories
  34. * @returns {Promise<module:@ui5/fs.Resource[]>} Promise resolving to list of resources
  35. */
  36. byGlob(virPattern, options = {nodir: true}) {
  37. const trace = new Trace(virPattern);
  38. return this._byGlob(virPattern, options, trace).then(function(result) {
  39. trace.printReport();
  40. return result;
  41. }).then((resources) => {
  42. if (resources.length > 1) {
  43. // Pseudo randomize result order to prevent consumers from relying on it:
  44. // Swap the first object with a randomly chosen one
  45. const x = 0;
  46. const y = randomInt(0, resources.length - 1);
  47. // Swap object at index "x" with "y"
  48. resources[x] = [resources[y], resources[y]=resources[x]][0];
  49. }
  50. return resources;
  51. });
  52. }
  53. /**
  54. * Locates resources by matching a given path.
  55. *
  56. * @public
  57. * @param {string} virPath Virtual path
  58. * @param {object} [options] Options
  59. * @param {boolean} [options.nodir=true] Do not match directories
  60. * @returns {Promise<module:@ui5/fs.Resource>} Promise resolving to a single resource
  61. */
  62. byPath(virPath, options = {nodir: true}) {
  63. const trace = new Trace(virPath);
  64. return this._byPath(virPath, options, trace).then(function(resource) {
  65. trace.printReport();
  66. return resource;
  67. });
  68. }
  69. /**
  70. * Locates resources by one or more glob patterns.
  71. *
  72. * @abstract
  73. * @protected
  74. * @param {string|string[]} virPattern glob pattern as string or an array of
  75. * glob patterns for virtual directory structure
  76. * @param {object} options glob options
  77. * @param {module:@ui5/fs.tracing.Trace} trace Trace instance
  78. * @returns {Promise<module:@ui5/fs.Resource[]>} Promise resolving to list of resources
  79. */
  80. _byGlob(virPattern, options, trace) {
  81. throw new Error("Not implemented");
  82. }
  83. /**
  84. * Locate resources by matching a single glob pattern.
  85. *
  86. * @abstract
  87. * @protected
  88. * @param {string} pattern glob pattern
  89. * @param {object} options glob options
  90. * @param {module:@ui5/fs.tracing.Trace} trace Trace instance
  91. * @returns {Promise<module:@ui5/fs.Resource[]>} Promise resolving to list of resources
  92. */
  93. _runGlob(pattern, options, trace) {
  94. throw new Error("Not implemented");
  95. }
  96. /**
  97. * Locates resources by path.
  98. *
  99. * @abstract
  100. * @protected
  101. * @param {string} virPath Virtual path
  102. * @param {object} options Options
  103. * @param {module:@ui5/fs.tracing.Trace} trace Trace instance
  104. * @returns {Promise<module:@ui5/fs.Resource>} Promise resolving to a single resource
  105. */
  106. _byPath(virPath, options, trace) {
  107. throw new Error("Not implemented");
  108. }
  109. }
  110. module.exports = AbstractReader;