// engine/detectors/monorepo-detector.ts — Monorepo tool detection
// SPEC-004 tech-debt: Nx, Turborepo, Bazel, Rush, Moon monorepo detection
import { access } from 'node:fs/promises';
import { join } from 'node:path';
/** File existence check — returns true if file is accessible */
async function fileExists(filePath) {
    try {
        await access(filePath);
        return true;
    }
    catch {
        return false;
    }
}
/**
 * Detect which monorepo tool (if any) is used in the given project directory.
 *
 * Detection order (first match wins):
 *   1. nx.json         → Nx
 *   2. turbo.json      → Turborepo
 *   3. WORKSPACE / WORKSPACE.bazel → Bazel
 *   4. rush.json       → Rush
 *   5. moon.yml / .moon/workspace.yml → Moon
 *   6. None found      → { tool: 'none', isMonorepo: false }
 */
export async function detectMonorepoTool(projectPath) {
    // 1. Nx
    if (await fileExists(join(projectPath, 'nx.json'))) {
        return { tool: 'nx', configFile: 'nx.json', isMonorepo: true };
    }
    // 2. Turborepo
    if (await fileExists(join(projectPath, 'turbo.json'))) {
        return { tool: 'turborepo', configFile: 'turbo.json', isMonorepo: true };
    }
    // 3. Bazel — try WORKSPACE first, then WORKSPACE.bazel
    if (await fileExists(join(projectPath, 'WORKSPACE'))) {
        return { tool: 'bazel', configFile: 'WORKSPACE', isMonorepo: true };
    }
    if (await fileExists(join(projectPath, 'WORKSPACE.bazel'))) {
        return { tool: 'bazel', configFile: 'WORKSPACE.bazel', isMonorepo: true };
    }
    // 4. Rush
    if (await fileExists(join(projectPath, 'rush.json'))) {
        return { tool: 'rush', configFile: 'rush.json', isMonorepo: true };
    }
    // 5. Moon — try moon.yml first, then .moon/workspace.yml
    if (await fileExists(join(projectPath, 'moon.yml'))) {
        return { tool: 'moon', configFile: 'moon.yml', isMonorepo: true };
    }
    if (await fileExists(join(projectPath, '.moon', 'workspace.yml'))) {
        return { tool: 'moon', configFile: '.moon/workspace.yml', isMonorepo: true };
    }
    // 6. No monorepo tool detected
    return { tool: 'none', configFile: null, isMonorepo: false };
}
//# sourceMappingURL=monorepo-detector.js.map