reader-stream.js 2.8 KB

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