reader-async.js 2.7 KB

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