tasks.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.convertPatternGroupToTask = exports.convertPatternGroupsToTasks = exports.groupPatternsByBaseDirectory = exports.getNegativePatternsAsPositive = exports.getPositivePatterns = exports.convertPatternsToTasks = exports.generate = void 0;
  4. const utils = require("../utils");
  5. function generate(input, settings) {
  6. const patterns = processPatterns(input, settings);
  7. const ignore = processPatterns(settings.ignore, settings);
  8. const positivePatterns = getPositivePatterns(patterns);
  9. const negativePatterns = getNegativePatternsAsPositive(patterns, ignore);
  10. const staticPatterns = positivePatterns.filter((pattern) => utils.pattern.isStaticPattern(pattern, settings));
  11. const dynamicPatterns = positivePatterns.filter((pattern) => utils.pattern.isDynamicPattern(pattern, settings));
  12. const staticTasks = convertPatternsToTasks(staticPatterns, negativePatterns, /* dynamic */ false);
  13. const dynamicTasks = convertPatternsToTasks(dynamicPatterns, negativePatterns, /* dynamic */ true);
  14. return staticTasks.concat(dynamicTasks);
  15. }
  16. exports.generate = generate;
  17. function processPatterns(input, settings) {
  18. let patterns = input;
  19. /**
  20. * The original pattern like `{,*,**,a/*}` can lead to problems checking the depth when matching entry
  21. * and some problems with the micromatch package (see fast-glob issues: #365, #394).
  22. *
  23. * To solve this problem, we expand all patterns containing brace expansion. This can lead to a slight slowdown
  24. * in matching in the case of a large set of patterns after expansion.
  25. */
  26. if (settings.braceExpansion) {
  27. patterns = utils.pattern.expandPatternsWithBraceExpansion(patterns);
  28. }
  29. /**
  30. * If the `baseNameMatch` option is enabled, we must add globstar to patterns, so that they can be used
  31. * at any nesting level.
  32. *
  33. * We do this here, because otherwise we have to complicate the filtering logic. For example, we need to change
  34. * the pattern in the filter before creating a regular expression. There is no need to change the patterns
  35. * in the application. Only on the input.
  36. */
  37. if (settings.baseNameMatch) {
  38. patterns = patterns.map((pattern) => pattern.includes('/') ? pattern : `**/${pattern}`);
  39. }
  40. /**
  41. * This method also removes duplicate slashes that may have been in the pattern or formed as a result of expansion.
  42. */
  43. return patterns.map((pattern) => utils.pattern.removeDuplicateSlashes(pattern));
  44. }
  45. /**
  46. * Returns tasks grouped by basic pattern directories.
  47. *
  48. * Patterns that can be found inside (`./`) and outside (`../`) the current directory are handled separately.
  49. * This is necessary because directory traversal starts at the base directory and goes deeper.
  50. */
  51. function convertPatternsToTasks(positive, negative, dynamic) {
  52. const tasks = [];
  53. const patternsOutsideCurrentDirectory = utils.pattern.getPatternsOutsideCurrentDirectory(positive);
  54. const patternsInsideCurrentDirectory = utils.pattern.getPatternsInsideCurrentDirectory(positive);
  55. const outsideCurrentDirectoryGroup = groupPatternsByBaseDirectory(patternsOutsideCurrentDirectory);
  56. const insideCurrentDirectoryGroup = groupPatternsByBaseDirectory(patternsInsideCurrentDirectory);
  57. tasks.push(...convertPatternGroupsToTasks(outsideCurrentDirectoryGroup, negative, dynamic));
  58. /*
  59. * For the sake of reducing future accesses to the file system, we merge all tasks within the current directory
  60. * into a global task, if at least one pattern refers to the root (`.`). In this case, the global task covers the rest.
  61. */
  62. if ('.' in insideCurrentDirectoryGroup) {
  63. tasks.push(convertPatternGroupToTask('.', patternsInsideCurrentDirectory, negative, dynamic));
  64. }
  65. else {
  66. tasks.push(...convertPatternGroupsToTasks(insideCurrentDirectoryGroup, negative, dynamic));
  67. }
  68. return tasks;
  69. }
  70. exports.convertPatternsToTasks = convertPatternsToTasks;
  71. function getPositivePatterns(patterns) {
  72. return utils.pattern.getPositivePatterns(patterns);
  73. }
  74. exports.getPositivePatterns = getPositivePatterns;
  75. function getNegativePatternsAsPositive(patterns, ignore) {
  76. const negative = utils.pattern.getNegativePatterns(patterns).concat(ignore);
  77. const positive = negative.map(utils.pattern.convertToPositivePattern);
  78. return positive;
  79. }
  80. exports.getNegativePatternsAsPositive = getNegativePatternsAsPositive;
  81. function groupPatternsByBaseDirectory(patterns) {
  82. const group = {};
  83. return patterns.reduce((collection, pattern) => {
  84. const base = utils.pattern.getBaseDirectory(pattern);
  85. if (base in collection) {
  86. collection[base].push(pattern);
  87. }
  88. else {
  89. collection[base] = [pattern];
  90. }
  91. return collection;
  92. }, group);
  93. }
  94. exports.groupPatternsByBaseDirectory = groupPatternsByBaseDirectory;
  95. function convertPatternGroupsToTasks(positive, negative, dynamic) {
  96. return Object.keys(positive).map((base) => {
  97. return convertPatternGroupToTask(base, positive[base], negative, dynamic);
  98. });
  99. }
  100. exports.convertPatternGroupsToTasks = convertPatternGroupsToTasks;
  101. function convertPatternGroupToTask(base, positive, negative, dynamic) {
  102. return {
  103. dynamic,
  104. positive,
  105. negative,
  106. base,
  107. patterns: [].concat(positive, negative.map(utils.pattern.convertToNegativePattern))
  108. };
  109. }
  110. exports.convertPatternGroupToTask = convertPatternGroupToTask;