You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

71 lines
2.2 KiB

4 years ago
  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. }