tasks.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. var patternUtils = require("../utils/pattern");
  4. /**
  5. * Generate tasks based on parent directory of each pattern.
  6. */
  7. function generate(patterns, options) {
  8. var unixPatterns = patterns.map(patternUtils.unixifyPattern);
  9. var unixIgnore = options.ignore.map(patternUtils.unixifyPattern);
  10. var positivePatterns = getPositivePatterns(unixPatterns);
  11. var negativePatterns = getNegativePatternsAsPositive(unixPatterns, unixIgnore);
  12. /**
  13. * When the `case` option is disabled, all patterns must be marked as dynamic, because we cannot check filepath
  14. * directly (without read directory).
  15. */
  16. var staticPatterns = !options.case ? [] : positivePatterns.filter(patternUtils.isStaticPattern);
  17. var dynamicPatterns = !options.case ? positivePatterns : positivePatterns.filter(patternUtils.isDynamicPattern);
  18. var staticTasks = convertPatternsToTasks(staticPatterns, negativePatterns, /* dynamic */ false);
  19. var dynamicTasks = convertPatternsToTasks(dynamicPatterns, negativePatterns, /* dynamic */ true);
  20. return staticTasks.concat(dynamicTasks);
  21. }
  22. exports.generate = generate;
  23. /**
  24. * Convert patterns to tasks based on parent directory of each pattern.
  25. */
  26. function convertPatternsToTasks(positive, negative, dynamic) {
  27. var positivePatternsGroup = groupPatternsByBaseDirectory(positive);
  28. // When we have a global group – there is no reason to divide the patterns into independent tasks.
  29. // In this case, the global task covers the rest.
  30. if ('.' in positivePatternsGroup) {
  31. var task = convertPatternGroupToTask('.', positive, negative, dynamic);
  32. return [task];
  33. }
  34. return convertPatternGroupsToTasks(positivePatternsGroup, negative, dynamic);
  35. }
  36. exports.convertPatternsToTasks = convertPatternsToTasks;
  37. /**
  38. * Return only positive patterns.
  39. */
  40. function getPositivePatterns(patterns) {
  41. return patternUtils.getPositivePatterns(patterns);
  42. }
  43. exports.getPositivePatterns = getPositivePatterns;
  44. /**
  45. * Return only negative patterns.
  46. */
  47. function getNegativePatternsAsPositive(patterns, ignore) {
  48. var negative = patternUtils.getNegativePatterns(patterns).concat(ignore);
  49. var positive = negative.map(patternUtils.convertToPositivePattern);
  50. return positive;
  51. }
  52. exports.getNegativePatternsAsPositive = getNegativePatternsAsPositive;
  53. /**
  54. * Group patterns by base directory of each pattern.
  55. */
  56. function groupPatternsByBaseDirectory(patterns) {
  57. return patterns.reduce(function (collection, pattern) {
  58. var base = patternUtils.getBaseDirectory(pattern);
  59. if (base in collection) {
  60. collection[base].push(pattern);
  61. }
  62. else {
  63. collection[base] = [pattern];
  64. }
  65. return collection;
  66. }, {});
  67. }
  68. exports.groupPatternsByBaseDirectory = groupPatternsByBaseDirectory;
  69. /**
  70. * Convert group of patterns to tasks.
  71. */
  72. function convertPatternGroupsToTasks(positive, negative, dynamic) {
  73. return Object.keys(positive).map(function (base) {
  74. return convertPatternGroupToTask(base, positive[base], negative, dynamic);
  75. });
  76. }
  77. exports.convertPatternGroupsToTasks = convertPatternGroupsToTasks;
  78. /**
  79. * Create a task for positive and negative patterns.
  80. */
  81. function convertPatternGroupToTask(base, positive, negative, dynamic) {
  82. return {
  83. base: base,
  84. dynamic: dynamic,
  85. positive: positive,
  86. negative: negative,
  87. patterns: [].concat(positive, negative.map(patternUtils.convertToNegativePattern))
  88. };
  89. }
  90. exports.convertPatternGroupToTask = convertPatternGroupToTask;