reader-sync.js 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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_sync_1 = require("../adapters/fs-sync");
  16. var ReaderSync = /** @class */ (function (_super) {
  17. __extends(ReaderSync, _super);
  18. function ReaderSync() {
  19. return _super !== null && _super.apply(this, arguments) || this;
  20. }
  21. Object.defineProperty(ReaderSync.prototype, "fsAdapter", {
  22. /**
  23. * Returns FileSystem adapter.
  24. */
  25. get: function () {
  26. return new fs_sync_1.default(this.options);
  27. },
  28. enumerable: true,
  29. configurable: true
  30. });
  31. /**
  32. * Use sync API to read entries for Task.
  33. */
  34. ReaderSync.prototype.read = function (task) {
  35. var root = this.getRootDirectory(task);
  36. var options = this.getReaderOptions(task);
  37. try {
  38. var entries = this.api(root, task, options);
  39. return entries.map(this.transform, this);
  40. }
  41. catch (err) {
  42. if (this.isEnoentCodeError(err)) {
  43. return [];
  44. }
  45. throw err;
  46. }
  47. };
  48. /**
  49. * Returns founded paths.
  50. */
  51. ReaderSync.prototype.api = function (root, task, options) {
  52. if (task.dynamic) {
  53. return this.dynamicApi(root, options);
  54. }
  55. return this.staticApi(task, options);
  56. };
  57. /**
  58. * Api for dynamic tasks.
  59. */
  60. ReaderSync.prototype.dynamicApi = function (root, options) {
  61. return readdir.readdirSyncStat(root, options);
  62. };
  63. /**
  64. * Api for static tasks.
  65. */
  66. ReaderSync.prototype.staticApi = function (task, options) {
  67. return this.fsAdapter.read(task.patterns, options.filter);
  68. };
  69. return ReaderSync;
  70. }(reader_1.default));
  71. exports.default = ReaderSync;