reader-async.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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 readdir = require("@mrmlnc/readdir-enhanced");
  14. var reader_1 = require("./reader");
  15. var fs_stream_1 = require("../adapters/fs-stream");
  16. var ReaderAsync = /** @class */ (function (_super) {
  17. __extends(ReaderAsync, _super);
  18. function ReaderAsync() {
  19. return _super !== null && _super.apply(this, arguments) || this;
  20. }
  21. Object.defineProperty(ReaderAsync.prototype, "fsAdapter", {
  22. /**
  23. * Returns FileSystem adapter.
  24. */
  25. get: function () {
  26. return new fs_stream_1.default(this.options);
  27. },
  28. enumerable: true,
  29. configurable: true
  30. });
  31. /**
  32. * Use async API to read entries for Task.
  33. */
  34. ReaderAsync.prototype.read = function (task) {
  35. var _this = this;
  36. var root = this.getRootDirectory(task);
  37. var options = this.getReaderOptions(task);
  38. var entries = [];
  39. return new Promise(function (resolve, reject) {
  40. var stream = _this.api(root, task, options);
  41. stream.on('error', function (err) {
  42. _this.isEnoentCodeError(err) ? resolve([]) : reject(err);
  43. stream.pause();
  44. });
  45. stream.on('data', function (entry) { return entries.push(_this.transform(entry)); });
  46. stream.on('end', function () { return resolve(entries); });
  47. });
  48. };
  49. /**
  50. * Returns founded paths.
  51. */
  52. ReaderAsync.prototype.api = function (root, task, options) {
  53. if (task.dynamic) {
  54. return this.dynamicApi(root, options);
  55. }
  56. return this.staticApi(task, options);
  57. };
  58. /**
  59. * Api for dynamic tasks.
  60. */
  61. ReaderAsync.prototype.dynamicApi = function (root, options) {
  62. return readdir.readdirStreamStat(root, options);
  63. };
  64. /**
  65. * Api for static tasks.
  66. */
  67. ReaderAsync.prototype.staticApi = function (task, options) {
  68. return this.fsAdapter.read(task.patterns, options.filter);
  69. };
  70. return ReaderAsync;
  71. }(reader_1.default));
  72. exports.default = ReaderAsync;