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.

index.js 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. "use strict";
  2. const taskManager = require("./managers/tasks");
  3. const async_1 = require("./providers/async");
  4. const stream_1 = require("./providers/stream");
  5. const sync_1 = require("./providers/sync");
  6. const settings_1 = require("./settings");
  7. const utils = require("./utils");
  8. async function FastGlob(source, options) {
  9. assertPatternsInput(source);
  10. const works = getWorks(source, async_1.default, options);
  11. const result = await Promise.all(works);
  12. return utils.array.flatten(result);
  13. }
  14. // https://github.com/typescript-eslint/typescript-eslint/issues/60
  15. // eslint-disable-next-line no-redeclare
  16. (function (FastGlob) {
  17. FastGlob.glob = FastGlob;
  18. FastGlob.globSync = sync;
  19. FastGlob.globStream = stream;
  20. FastGlob.async = FastGlob;
  21. function sync(source, options) {
  22. assertPatternsInput(source);
  23. const works = getWorks(source, sync_1.default, options);
  24. return utils.array.flatten(works);
  25. }
  26. FastGlob.sync = sync;
  27. function stream(source, options) {
  28. assertPatternsInput(source);
  29. const works = getWorks(source, stream_1.default, options);
  30. /**
  31. * The stream returned by the provider cannot work with an asynchronous iterator.
  32. * To support asynchronous iterators, regardless of the number of tasks, we always multiplex streams.
  33. * This affects performance (+25%). I don't see best solution right now.
  34. */
  35. return utils.stream.merge(works);
  36. }
  37. FastGlob.stream = stream;
  38. function generateTasks(source, options) {
  39. assertPatternsInput(source);
  40. const patterns = [].concat(source);
  41. const settings = new settings_1.default(options);
  42. return taskManager.generate(patterns, settings);
  43. }
  44. FastGlob.generateTasks = generateTasks;
  45. function isDynamicPattern(source, options) {
  46. assertPatternsInput(source);
  47. const settings = new settings_1.default(options);
  48. return utils.pattern.isDynamicPattern(source, settings);
  49. }
  50. FastGlob.isDynamicPattern = isDynamicPattern;
  51. function escapePath(source) {
  52. assertPatternsInput(source);
  53. return utils.path.escape(source);
  54. }
  55. FastGlob.escapePath = escapePath;
  56. function convertPathToPattern(source) {
  57. assertPatternsInput(source);
  58. return utils.path.convertPathToPattern(source);
  59. }
  60. FastGlob.convertPathToPattern = convertPathToPattern;
  61. let posix;
  62. (function (posix) {
  63. function escapePath(source) {
  64. assertPatternsInput(source);
  65. return utils.path.escapePosixPath(source);
  66. }
  67. posix.escapePath = escapePath;
  68. function convertPathToPattern(source) {
  69. assertPatternsInput(source);
  70. return utils.path.convertPosixPathToPattern(source);
  71. }
  72. posix.convertPathToPattern = convertPathToPattern;
  73. })(posix = FastGlob.posix || (FastGlob.posix = {}));
  74. let win32;
  75. (function (win32) {
  76. function escapePath(source) {
  77. assertPatternsInput(source);
  78. return utils.path.escapeWindowsPath(source);
  79. }
  80. win32.escapePath = escapePath;
  81. function convertPathToPattern(source) {
  82. assertPatternsInput(source);
  83. return utils.path.convertWindowsPathToPattern(source);
  84. }
  85. win32.convertPathToPattern = convertPathToPattern;
  86. })(win32 = FastGlob.win32 || (FastGlob.win32 = {}));
  87. })(FastGlob || (FastGlob = {}));
  88. function getWorks(source, _Provider, options) {
  89. const patterns = [].concat(source);
  90. const settings = new settings_1.default(options);
  91. const tasks = taskManager.generate(patterns, settings);
  92. const provider = new _Provider(settings);
  93. return tasks.map(provider.read, provider);
  94. }
  95. function assertPatternsInput(input) {
  96. const source = [].concat(input);
  97. const isValidSource = source.every((item) => utils.string.isString(item) && !utils.string.isEmpty(item));
  98. if (!isValidSource) {
  99. throw new TypeError('Patterns must be a string (non empty) or an array of strings');
  100. }
  101. }
  102. module.exports = FastGlob;