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