sync.js 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. const fsScandir = require("@nodelib/fs.scandir");
  4. const common = require("./common");
  5. const reader_1 = require("./reader");
  6. class SyncReader extends reader_1.default {
  7. constructor() {
  8. super(...arguments);
  9. this._scandir = fsScandir.scandirSync;
  10. this._storage = [];
  11. this._queue = new Set();
  12. }
  13. read() {
  14. this._pushToQueue(this._root, this._settings.basePath);
  15. this._handleQueue();
  16. return this._storage;
  17. }
  18. _pushToQueue(directory, base) {
  19. this._queue.add({ directory, base });
  20. }
  21. _handleQueue() {
  22. for (const item of this._queue.values()) {
  23. this._handleDirectory(item.directory, item.base);
  24. }
  25. }
  26. _handleDirectory(directory, base) {
  27. try {
  28. const entries = this._scandir(directory, this._settings.fsScandirSettings);
  29. for (const entry of entries) {
  30. this._handleEntry(entry, base);
  31. }
  32. }
  33. catch (error) {
  34. this._handleError(error);
  35. }
  36. }
  37. _handleError(error) {
  38. if (!common.isFatalError(this._settings, error)) {
  39. return;
  40. }
  41. throw error;
  42. }
  43. _handleEntry(entry, base) {
  44. const fullpath = entry.path;
  45. if (base !== undefined) {
  46. entry.path = common.joinPathSegments(base, entry.name, this._settings.pathSegmentSeparator);
  47. }
  48. if (common.isAppliedFilter(this._settings.entryFilter, entry)) {
  49. this._pushToStorage(entry);
  50. }
  51. if (entry.dirent.isDirectory() && common.isAppliedFilter(this._settings.deepFilter, entry)) {
  52. this._pushToQueue(fullpath, base === undefined ? undefined : entry.path);
  53. }
  54. }
  55. _pushToStorage(entry) {
  56. this._storage.push(entry);
  57. }
  58. }
  59. exports.default = SyncReader;