fs-stream.js 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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 fsStat = require("@nodelib/fs.stat");
  15. var fs_1 = require("./fs");
  16. var FileSystemStream = /** @class */ (function (_super) {
  17. __extends(FileSystemStream, _super);
  18. function FileSystemStream() {
  19. return _super !== null && _super.apply(this, arguments) || this;
  20. }
  21. /**
  22. * Use stream API to read entries for Task.
  23. */
  24. FileSystemStream.prototype.read = function (patterns, filter) {
  25. var _this = this;
  26. var filepaths = patterns.map(this.getFullEntryPath, this);
  27. var transform = new stream.Transform({ objectMode: true });
  28. transform._transform = function (index, _enc, done) {
  29. return _this.getEntry(filepaths[index], patterns[index]).then(function (entry) {
  30. if (entry !== null && filter(entry)) {
  31. transform.push(entry);
  32. }
  33. if (index === filepaths.length - 1) {
  34. transform.end();
  35. }
  36. done();
  37. });
  38. };
  39. for (var i = 0; i < filepaths.length; i++) {
  40. transform.write(i);
  41. }
  42. return transform;
  43. };
  44. /**
  45. * Return entry for the provided path.
  46. */
  47. FileSystemStream.prototype.getEntry = function (filepath, pattern) {
  48. var _this = this;
  49. return this.getStat(filepath)
  50. .then(function (stat) { return _this.makeEntry(stat, pattern); })
  51. .catch(function () { return null; });
  52. };
  53. /**
  54. * Return fs.Stats for the provided path.
  55. */
  56. FileSystemStream.prototype.getStat = function (filepath) {
  57. return fsStat.stat(filepath, { throwErrorOnBrokenSymlinks: false });
  58. };
  59. return FileSystemStream;
  60. }(fs_1.default));
  61. exports.default = FileSystemStream;