
!function(){try{var e="undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof globalThis?globalThis:"undefined"!=typeof self?self:{},n=(new e.Error).stack;n&&(e._sentryDebugIds=e._sentryDebugIds||{},e._sentryDebugIds[n]="3a2ef1a0-10dc-5219-9f8b-d1d96308f4eb")}catch(e){}}();
import { readdir, readFile } from "node:fs/promises";
import { join, relative } from "node:path";
const NODE_MODULES_RELATIVE_PATH = "node_modules";
const TURBOPACK_SCAN_MAX_DIRECTORY_DEPTH = 3;
const TURBOPACK_SCRIPT_FLAG_PATTERN = /(^|\s)--(?:turbo|turbopack)(?=\s|$)/i;
const TURBOPACK_SCRIPT_ENV_PATTERN = /(^|\s)(?:TURBOPACK|NEXT_TURBOPACK)\s*=\s*(?:1|true|yes)(?=\s|$)/i;
const TURBOPACK_CONFIG_PATTERN = /\bturbopack\b/i;
const NEXT_SCRIPT_PATTERN = /\bnext\b/i;
const NEXT_CONFIG_FILENAMES = [
    "next.config.js",
    "next.config.mjs",
    "next.config.cjs",
    "next.config.ts",
    "next.config.mts",
    "next.config.cts",
];
const TURBOPACK_SCAN_DIRECTORY_SKIP = new Set([".git", ".next", "build", "coverage", "dist", "node_modules"]);
function toPlatformRelativePath(path) {
    return path
        .trim()
        .replaceAll("\\", "/")
        .replace(/\/+$/g, "")
        .split("/")
        .filter((segment) => segment.length > 0)
        .join("/");
}
function scriptUsesTurbopack(script) {
    return TURBOPACK_SCRIPT_FLAG_PATTERN.test(script) || TURBOPACK_SCRIPT_ENV_PATTERN.test(script);
}
function isObjectRecord(value) {
    return typeof value === "object" && value !== null;
}
function getStringScripts(packageJson) {
    const scripts = packageJson.scripts;
    if (!isObjectRecord(scripts)) {
        return [];
    }
    return Object.values(scripts).filter((script) => typeof script === "string");
}
function packageDependsOnNext(packageJson) {
    for (const dependencyGroup of [
        packageJson.dependencies,
        packageJson.devDependencies,
        packageJson.peerDependencies,
    ]) {
        if (!isObjectRecord(dependencyGroup)) {
            continue;
        }
        if ("next" in dependencyGroup) {
            return true;
        }
    }
    return false;
}
function packageLooksLikeNextApp(packageJson) {
    return (packageDependsOnNext(packageJson) ||
        getStringScripts(packageJson).some((script) => NEXT_SCRIPT_PATTERN.test(script)));
}
async function readPackageJson(packageDir) {
    try {
        const packageJsonContent = await readFile(join(packageDir, "package.json"), "utf8");
        return JSON.parse(packageJsonContent);
    }
    catch {
        return null;
    }
}
async function repoConfigMentionsTurbopack(repoPath) {
    for (const filename of NEXT_CONFIG_FILENAMES) {
        try {
            const content = await readFile(join(repoPath, filename), "utf8");
            if (TURBOPACK_CONFIG_PATTERN.test(content)) {
                return true;
            }
        }
        catch { }
    }
    return false;
}
async function packageDirectoryUsesTurbopack(packageDir) {
    const packageJson = await readPackageJson(packageDir);
    if (!packageJson) {
        return false;
    }
    const scripts = getStringScripts(packageJson);
    const hasTurbopackScript = scripts.some((script) => scriptUsesTurbopack(script));
    if (hasTurbopackScript) {
        return true;
    }
    const hasNextAppHints = packageLooksLikeNextApp(packageJson);
    if (!hasNextAppHints) {
        return false;
    }
    const hasTurbopackConfig = await repoConfigMentionsTurbopack(packageDir);
    return hasTurbopackConfig;
}
async function collectNestedPackageDirs(rootDir) {
    const packageDirs = [];
    async function visitDirectory(currentDir, depth) {
        if (depth >= TURBOPACK_SCAN_MAX_DIRECTORY_DEPTH) {
            return;
        }
        const entries = await readdir(currentDir, { withFileTypes: true }).catch(() => []);
        for (const entry of entries) {
            if (!entry.isDirectory() || TURBOPACK_SCAN_DIRECTORY_SKIP.has(entry.name)) {
                continue;
            }
            const childDir = join(currentDir, entry.name);
            if ((await readPackageJson(childDir)) !== null) {
                packageDirs.push(childDir);
            }
            await visitDirectory(childDir, depth + 1);
        }
    }
    await visitDirectory(rootDir, 0);
    return packageDirs;
}
function getNodeModulesPathForPackageDir(repoPath, packageDir) {
    const packageRelativePath = toPlatformRelativePath(relative(repoPath, packageDir));
    return packageRelativePath.length > 0
        ? `${packageRelativePath}/${NODE_MODULES_RELATIVE_PATH}`
        : NODE_MODULES_RELATIVE_PATH;
}
export async function listTurbopackNodeModulesSymlinkSkipPaths(repoPath) {
    const skipPaths = new Set();
    if (await packageDirectoryUsesTurbopack(repoPath)) {
        skipPaths.add(NODE_MODULES_RELATIVE_PATH);
    }
    const nestedPackageDirs = await collectNestedPackageDirs(repoPath);
    for (const packageDir of nestedPackageDirs) {
        if (await packageDirectoryUsesTurbopack(packageDir)) {
            skipPaths.add(getNodeModulesPathForPackageDir(repoPath, packageDir));
        }
    }
    return Array.from(skipPaths).sort();
}
//# sourceMappingURL=task-worktree-turbopack.js.map
//# debugId=3a2ef1a0-10dc-5219-9f8b-d1d96308f4eb
