reader-stream.js 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. "use strict";
  2. var __extends = (this && this.__extends) || (function () {
  3. var extendStatics = function (d, b) {
  4. extendStatics = Object.setPrototypeOf ||
  5. ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
  6. function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
  7. return extendStatics(d, b);
  8. };
  9. return function (d, b) {
  10. extendStatics(d, b);
  11. function __() { this.constructor = d; }
  12. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  13. };
  14. })();
  15. Object.defineProperty(exports, "__esModule", { value: true });
  16. var stream = require("stream");
  17. var readdir = require("@mrmlnc/readdir-enhanced");
  18. var reader_1 = require("./reader");
  19. var fs_stream_1 = require("../adapters/fs-stream");
  20. var TransformStream = /** @class */ (function (_super) {
  21. __extends(TransformStream, _super);
  22. function TransformStream(reader) {
  23. var _this = _super.call(this, { objectMode: true }) || this;
  24. _this.reader = reader;
  25. return _this;
  26. }
  27. TransformStream.prototype._transform = function (entry, _encoding, callback) {
  28. callback(null, this.reader.transform(entry));
  29. };
  30. return TransformStream;
  31. }(stream.Transform));
  32. var ReaderStream = /** @class */ (function (_super) {
  33. __extends(ReaderStream, _super);
  34. function ReaderStream() {
  35. return _super !== null && _super.apply(this, arguments) || this;
  36. }
  37. Object.defineProperty(ReaderStream.prototype, "fsAdapter", {
  38. /**
  39. * Returns FileSystem adapter.
  40. */
  41. get: function () {
  42. return new fs_stream_1.default(this.options);
  43. },
  44. enumerable: true,
  45. configurable: true
  46. });
  47. /**
  48. * Use stream API to read entries for Task.
  49. */
  50. ReaderStream.prototype.read = function (task) {
  51. var _this = this;
  52. var root = this.getRootDirectory(task);
  53. var options = this.getReaderOptions(task);
  54. var transform = new TransformStream(this);
  55. var readable = this.api(root, task, options);
  56. return readable
  57. .on('error', function (err) { return _this.isEnoentCodeError(err) ? null : transform.emit('error', err); })
  58. .pipe(transform);
  59. };
  60. /**
  61. * Returns founded paths.
  62. */
  63. ReaderStream.prototype.api = function (root, task, options) {
  64. if (task.dynamic) {
  65. return this.dynamicApi(root, options);
  66. }
  67. return this.staticApi(task, options);
  68. };
  69. /**
  70. * Api for dynamic tasks.
  71. */
  72. ReaderStream.prototype.dynamicApi = function (root, options) {
  73. return readdir.readdirStreamStat(root, options);
  74. };
  75. /**
  76. * Api for static tasks.
  77. */
  78. ReaderStream.prototype.staticApi = function (task, options) {
  79. return this.fsAdapter.read(task.patterns, options.filter);
  80. };
  81. return ReaderStream;
  82. }(reader_1.default));
  83. exports.default = ReaderStream;