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.

68 lines
2.9 KiB

2 years ago
  1. "use strict";
  2. const taskManager = require("./managers/tasks");
  3. const patternManager = require("./managers/patterns");
  4. const async_1 = require("./providers/async");
  5. const stream_1 = require("./providers/stream");
  6. const sync_1 = require("./providers/sync");
  7. const settings_1 = require("./settings");
  8. const utils = require("./utils");
  9. async function FastGlob(source, options) {
  10. assertPatternsInput(source);
  11. const works = getWorks(source, async_1.default, options);
  12. const result = await Promise.all(works);
  13. return utils.array.flatten(result);
  14. }
  15. // https://github.com/typescript-eslint/typescript-eslint/issues/60
  16. // eslint-disable-next-line no-redeclare
  17. (function (FastGlob) {
  18. function sync(source, options) {
  19. assertPatternsInput(source);
  20. const works = getWorks(source, sync_1.default, options);
  21. return utils.array.flatten(works);
  22. }
  23. FastGlob.sync = sync;
  24. function stream(source, options) {
  25. assertPatternsInput(source);
  26. const works = getWorks(source, stream_1.default, options);
  27. /**
  28. * The stream returned by the provider cannot work with an asynchronous iterator.
  29. * To support asynchronous iterators, regardless of the number of tasks, we always multiplex streams.
  30. * This affects performance (+25%). I don't see best solution right now.
  31. */
  32. return utils.stream.merge(works);
  33. }
  34. FastGlob.stream = stream;
  35. function generateTasks(source, options) {
  36. assertPatternsInput(source);
  37. const patterns = patternManager.transform([].concat(source));
  38. const settings = new settings_1.default(options);
  39. return taskManager.generate(patterns, settings);
  40. }
  41. FastGlob.generateTasks = generateTasks;
  42. function isDynamicPattern(source, options) {
  43. assertPatternsInput(source);
  44. const settings = new settings_1.default(options);
  45. return utils.pattern.isDynamicPattern(source, settings);
  46. }
  47. FastGlob.isDynamicPattern = isDynamicPattern;
  48. function escapePath(source) {
  49. assertPatternsInput(source);
  50. return utils.path.escape(source);
  51. }
  52. FastGlob.escapePath = escapePath;
  53. })(FastGlob || (FastGlob = {}));
  54. function getWorks(source, _Provider, options) {
  55. const patterns = patternManager.transform([].concat(source));
  56. const settings = new settings_1.default(options);
  57. const tasks = taskManager.generate(patterns, settings);
  58. const provider = new _Provider(settings);
  59. return tasks.map(provider.read, provider);
  60. }
  61. function assertPatternsInput(input) {
  62. const source = [].concat(input);
  63. const isValidSource = source.every((item) => utils.string.isString(item) && !utils.string.isEmpty(item));
  64. if (!isValidSource) {
  65. throw new TypeError('Patterns must be a string (non empty) or an array of strings');
  66. }
  67. }
  68. module.exports = FastGlob;