index.js 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. var optionsManager = require("./managers/options");
  4. var taskManager = require("./managers/tasks");
  5. var reader_async_1 = require("./providers/reader-async");
  6. var reader_stream_1 = require("./providers/reader-stream");
  7. var reader_sync_1 = require("./providers/reader-sync");
  8. var arrayUtils = require("./utils/array");
  9. var streamUtils = require("./utils/stream");
  10. /**
  11. * Synchronous API.
  12. */
  13. function sync(source, opts) {
  14. assertPatternsInput(source);
  15. var works = getWorks(source, reader_sync_1.default, opts);
  16. return arrayUtils.flatten(works);
  17. }
  18. exports.sync = sync;
  19. /**
  20. * Asynchronous API.
  21. */
  22. function async(source, opts) {
  23. try {
  24. assertPatternsInput(source);
  25. }
  26. catch (error) {
  27. return Promise.reject(error);
  28. }
  29. var works = getWorks(source, reader_async_1.default, opts);
  30. return Promise.all(works).then(arrayUtils.flatten);
  31. }
  32. exports.async = async;
  33. /**
  34. * Stream API.
  35. */
  36. function stream(source, opts) {
  37. assertPatternsInput(source);
  38. var works = getWorks(source, reader_stream_1.default, opts);
  39. return streamUtils.merge(works);
  40. }
  41. exports.stream = stream;
  42. /**
  43. * Return a set of tasks based on provided patterns.
  44. */
  45. function generateTasks(source, opts) {
  46. assertPatternsInput(source);
  47. var patterns = [].concat(source);
  48. var options = optionsManager.prepare(opts);
  49. return taskManager.generate(patterns, options);
  50. }
  51. exports.generateTasks = generateTasks;
  52. /**
  53. * Returns a set of works based on provided tasks and class of the reader.
  54. */
  55. function getWorks(source, _Reader, opts) {
  56. var patterns = [].concat(source);
  57. var options = optionsManager.prepare(opts);
  58. var tasks = taskManager.generate(patterns, options);
  59. var reader = new _Reader(options);
  60. return tasks.map(reader.read, reader);
  61. }
  62. function assertPatternsInput(source) {
  63. if ([].concat(source).every(isString)) {
  64. return;
  65. }
  66. throw new TypeError('Patterns must be a string or an array of strings');
  67. }
  68. function isString(source) {
  69. /* tslint:disable-next-line strict-type-predicates */
  70. return typeof source === 'string';
  71. }