tasks.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. "use strict";
  2. var __read = (this && this.__read) || function (o, n) {
  3. var m = typeof Symbol === "function" && o[Symbol.iterator];
  4. if (!m) return o;
  5. var i = m.call(o), r, ar = [], e;
  6. try {
  7. while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
  8. }
  9. catch (error) { e = { error: error }; }
  10. finally {
  11. try {
  12. if (r && !r.done && (m = i["return"])) m.call(i);
  13. }
  14. finally { if (e) throw e.error; }
  15. }
  16. return ar;
  17. };
  18. var __spread = (this && this.__spread) || function () {
  19. for (var ar = [], i = 0; i < arguments.length; i++) ar = ar.concat(__read(arguments[i]));
  20. return ar;
  21. };
  22. Object.defineProperty(exports, "__esModule", { value: true });
  23. var patternUtils = require("../utils/pattern");
  24. /**
  25. * Generate tasks based on parent directory of each pattern.
  26. */
  27. function generate(patterns, options) {
  28. var unixPatterns = patterns.map(patternUtils.unixifyPattern);
  29. var unixIgnore = options.ignore.map(patternUtils.unixifyPattern);
  30. var positivePatterns = getPositivePatterns(unixPatterns);
  31. var negativePatterns = getNegativePatternsAsPositive(unixPatterns, unixIgnore);
  32. var staticPatterns = positivePatterns.filter(patternUtils.isStaticPattern);
  33. var dynamicPatterns = positivePatterns.filter(patternUtils.isDynamicPattern);
  34. var staticTasks = convertPatternsToTasks(staticPatterns, negativePatterns, /* dynamic */ false);
  35. var dynamicTasks = convertPatternsToTasks(dynamicPatterns, negativePatterns, /* dynamic */ true);
  36. return staticTasks.concat(dynamicTasks);
  37. }
  38. exports.generate = generate;
  39. /**
  40. * Convert patterns to tasks based on parent directory of each pattern.
  41. */
  42. function convertPatternsToTasks(positive, negative, dynamic) {
  43. var positivePatternsGroup = groupPatternsByBaseDirectory(positive);
  44. var negativePatternsGroup = groupPatternsByBaseDirectory(negative);
  45. // When we have a global group – there is no reason to divide the patterns into independent tasks.
  46. // In this case, the global task covers the rest.
  47. if ('.' in positivePatternsGroup) {
  48. var task = convertPatternGroupToTask('.', positive, negative, dynamic);
  49. return [task];
  50. }
  51. return convertPatternGroupsToTasks(positivePatternsGroup, negativePatternsGroup, dynamic);
  52. }
  53. exports.convertPatternsToTasks = convertPatternsToTasks;
  54. /**
  55. * Return only positive patterns.
  56. */
  57. function getPositivePatterns(patterns) {
  58. return patternUtils.getPositivePatterns(patterns);
  59. }
  60. exports.getPositivePatterns = getPositivePatterns;
  61. /**
  62. * Return only negative patterns.
  63. */
  64. function getNegativePatternsAsPositive(patterns, ignore) {
  65. var negative = patternUtils.getNegativePatterns(patterns).concat(ignore);
  66. var positive = negative.map(patternUtils.convertToPositivePattern);
  67. return positive;
  68. }
  69. exports.getNegativePatternsAsPositive = getNegativePatternsAsPositive;
  70. /**
  71. * Group patterns by base directory of each pattern.
  72. */
  73. function groupPatternsByBaseDirectory(patterns) {
  74. return patterns.reduce(function (collection, pattern) {
  75. var base = patternUtils.getBaseDirectory(pattern);
  76. if (base in collection) {
  77. collection[base].push(pattern);
  78. }
  79. else {
  80. collection[base] = [pattern];
  81. }
  82. return collection;
  83. }, {});
  84. }
  85. exports.groupPatternsByBaseDirectory = groupPatternsByBaseDirectory;
  86. /**
  87. * Convert group of patterns to tasks.
  88. */
  89. function convertPatternGroupsToTasks(positive, negative, dynamic) {
  90. var globalNegative = '.' in negative ? negative['.'] : [];
  91. return Object.keys(positive).map(function (base) {
  92. var localNegative = findLocalNegativePatterns(base, negative);
  93. var fullNegative = localNegative.concat(globalNegative);
  94. return convertPatternGroupToTask(base, positive[base], fullNegative, dynamic);
  95. });
  96. }
  97. exports.convertPatternGroupsToTasks = convertPatternGroupsToTasks;
  98. /**
  99. * Returns those negative patterns whose base paths includes positive base path.
  100. */
  101. function findLocalNegativePatterns(positiveBase, negative) {
  102. return Object.keys(negative).reduce(function (collection, base) {
  103. if (base.startsWith(positiveBase)) {
  104. collection.push.apply(collection, __spread(negative[base]));
  105. }
  106. return collection;
  107. }, []);
  108. }
  109. exports.findLocalNegativePatterns = findLocalNegativePatterns;
  110. /**
  111. * Create a task for positive and negative patterns.
  112. */
  113. function convertPatternGroupToTask(base, positive, negative, dynamic) {
  114. return {
  115. base: base,
  116. dynamic: dynamic,
  117. patterns: [].concat(positive, negative.map(patternUtils.convertToNegativePattern)),
  118. positive: positive,
  119. negative: negative
  120. };
  121. }
  122. exports.convertPatternGroupToTask = convertPatternGroupToTask;