"use strict";
// adapted from https://github.com/blitz-js/blitz/blob/59c839c65fb96f21535491f4c167998fb2c6707c/packages/server/src/parse-chokidar-rules-from-gitignore.ts
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.parseChokidarRulesFromGitignore = exports.chokidarRulesFromGitignore = exports.getAllGitIgnores = exports.isControlledByUser = void 0;
const cross_spawn_1 = __importDefault(require("cross-spawn"));
const expand_tilde_1 = __importDefault(require("expand-tilde"));
const fast_glob_1 = __importDefault(require("fast-glob"));
const fs_1 = __importDefault(require("fs"));
const parse_gitignore_1 = __importDefault(require("parse-gitignore"));
function partition(arr, predicate) {
    const truthy = [];
    const falsy = [];
    for (const v of arr) {
        if (predicate(v)) {
            truthy.push(v);
        }
        else {
            falsy.push(v);
        }
    }
    return [truthy, falsy];
}
const { GIT_DIR = ".git" } = process.env;
function globalGitIgnore() {
    const versionResult = cross_spawn_1.default.sync("git", ["version"]);
    if (!(versionResult.status === 0)) {
        console.log("Git doesn't seem to be installed. Get it here: https://git-scm.com/downloads.");
        return null;
    }
    const configResult = cross_spawn_1.default.sync("git", ["config", "--get", "core.excludesfile"], {
        stdio: "pipe",
    });
    if (!(configResult.status === 0)) {
        // Git config core.excludesFile is unset. Inferring .gitignore file locations.
        return null;
    }
    const output = String(configResult.stdout).trim();
    return process.platform === "win32" ? output : (0, expand_tilde_1.default)(output);
}
function isControlledByUser(file) {
    if (file.startsWith("node_modules")) {
        return false;
    }
    return true;
}
exports.isControlledByUser = isControlledByUser;
function getAllGitIgnores(rootFolder) {
    const globalIgnore = globalGitIgnore();
    const localRepoIgnore = `${GIT_DIR}/info/exclude`;
    const files = fast_glob_1.default.sync([localRepoIgnore, `**/${localRepoIgnore}`, "**/.gitignore"], {
        cwd: rootFolder,
    });
    if (globalIgnore && fs_1.default.existsSync(globalIgnore))
        files.push(globalIgnore);
    return files.filter(isControlledByUser).map((file) => {
        let prefix = "";
        if (file.match(localRepoIgnore))
            prefix = file.split(localRepoIgnore)[0];
        else if (globalIgnore && file.match(globalIgnore))
            prefix = "";
        else
            prefix = file.split(".gitignore")[0];
        return {
            gitIgnore: fs_1.default.readFileSync(file, { encoding: "utf8" }),
            prefix,
        };
    });
}
exports.getAllGitIgnores = getAllGitIgnores;
function chokidarRulesFromGitignore({ gitIgnore, prefix, }) {
    const rules = (0, parse_gitignore_1.default)(gitIgnore);
    const isInclusionRule = (rule) => rule.startsWith("!");
    const [includePaths, ignoredPaths] = partition(rules, isInclusionRule);
    const trimExclamationMark = (rule) => rule.substring(1);
    const prefixPath = (rule) => {
        if (rule.startsWith("/")) {
            rule = rule.substring(1);
        }
        if (rule.endsWith("/")) {
            rule = rule.substring(0, rule.length - 1);
        }
        if (!prefix) {
            return rule;
        }
        else {
            return prefix + rule;
        }
    };
    return {
        includePaths: includePaths.map(trimExclamationMark).map(prefixPath),
        ignoredPaths: ignoredPaths.map(prefixPath),
    };
}
exports.chokidarRulesFromGitignore = chokidarRulesFromGitignore;
function parseChokidarRulesFromGitignore(rootFolder) {
    const result = {
        includePaths: [],
        ignoredPaths: [],
    };
    getAllGitIgnores(rootFolder)
        .map(chokidarRulesFromGitignore)
        .forEach(({ ignoredPaths, includePaths }) => {
        result.includePaths.push(...includePaths);
        result.ignoredPaths.push(...ignoredPaths);
    });
    return result;
}
exports.parseChokidarRulesFromGitignore = parseChokidarRulesFromGitignore;
//# sourceMappingURL=parse-gitignore.js.map