optimizer.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. "use strict";
  2. var __importDefault = (this && this.__importDefault) || function (mod) {
  3. return (mod && mod.__esModule) ? mod : { "default": mod };
  4. };
  5. Object.defineProperty(exports, "__esModule", { value: true });
  6. exports.findMaxDepth = exports.findDirectoryPatterns = exports.findCommonRoots = void 0;
  7. // Glob Optimizations:
  8. // 1. Find common roots and only iterate on them
  9. // For example:
  10. // 1. "node_modules/**/*.ts" only requires us to search in node_modules
  11. // folder.
  12. // 2. Similarly, multiple glob patterns can have common deterministic roots
  13. // The optimizer's job is to find these roots and only crawl them.
  14. // 3. If any of the glob patterns have a globstar i.e. **/ in them, we
  15. // should bail out.
  16. // 2. Find out if glob is requesting only directories
  17. // 3. Find maximum depth requested
  18. // 4. If glob contains a root that doesn't exist, bail out
  19. const braces_1 = require("braces");
  20. const glob_parent_1 = __importDefault(require("glob-parent"));
  21. function findCommonRoots(patterns) {
  22. const allRoots = new Set();
  23. patterns = patterns.map((p) => (p.includes("{") ? (0, braces_1.expand)(p) : p)).flat();
  24. for (const pattern of patterns) {
  25. const parent = (0, glob_parent_1.default)(pattern);
  26. if (parent === ".")
  27. return [];
  28. allRoots.add(parent);
  29. }
  30. return Array.from(allRoots.values()).filter((root) => {
  31. for (const r of allRoots) {
  32. if (r === root)
  33. continue;
  34. if (root.startsWith(r))
  35. return false;
  36. }
  37. return true;
  38. });
  39. }
  40. exports.findCommonRoots = findCommonRoots;
  41. function findDirectoryPatterns(patterns) {
  42. return patterns.filter((p) => p.endsWith("/"));
  43. }
  44. exports.findDirectoryPatterns = findDirectoryPatterns;
  45. function findMaxDepth(patterns) {
  46. const isGlobstar = patterns.some((p) => p.includes("**/") || p.includes("/**") || p === "**");
  47. if (isGlobstar)
  48. return false;
  49. const maxDepth = patterns.reduce((depth, p) => {
  50. return Math.max(depth, p.split("/").filter(Boolean).length);
  51. }, 0);
  52. return maxDepth;
  53. }
  54. exports.findMaxDepth = findMaxDepth;