// engine/config-health/ci-commands-collector.ts — SPEC-042
// Helpers to collect available commands/scripts in a project.
import { readFile } from 'node:fs/promises';
import { join } from 'node:path';
import { glob } from 'glob';
export async function readFileSafe(filePath) {
    try {
        return await readFile(filePath, 'utf-8');
    }
    catch {
        return '';
    }
}
export async function fileExists(filePath) {
    const c = await readFileSafe(filePath);
    return c !== '';
}
async function collectPackageScripts(projectPath) {
    const scripts = new Set();
    const pkgContent = await readFileSafe(join(projectPath, 'package.json'));
    if (!pkgContent) {
        return scripts;
    }
    try {
        const pkg = JSON.parse(pkgContent);
        for (const name of Object.keys(pkg.scripts ?? {})) {
            scripts.add(name);
        }
    }
    catch {
        // ignore parse errors
    }
    return scripts;
}
async function collectMakefileTargets(projectPath) {
    const targets = new Set();
    const makeContent = await readFileSafe(join(projectPath, 'Makefile'));
    if (!makeContent) {
        return targets;
    }
    const targetPattern = /^([\w-]+)\s*:/gm;
    let match;
    while ((match = targetPattern.exec(makeContent)) !== null) {
        /* v8 ignore start */
        if (match[1]) {
            targets.add(match[1]);
        }
        /* v8 ignore stop */
    }
    return targets;
}
async function collectShellScripts(projectPath) {
    const scripts = new Set();
    try {
        const found = await glob('scripts/**/*.sh', { cwd: projectPath, nodir: true });
        for (const s of found) {
            scripts.add(s);
            /* v8 ignore start */
            const base = s.split('/').pop() ?? '';
            if (base) {
                scripts.add(base);
            }
            /* v8 ignore stop */
        }
    }
    catch {
        // ignore
    }
    return scripts;
}
async function collectCargoAliases(projectPath) {
    const aliases = new Set();
    const content = await readFileSafe(join(projectPath, '.cargo', 'config.toml'));
    if (!content) {
        return aliases;
    }
    const aliasPattern = /^([\w-]+)\s*=/gm;
    let inAliasSection = false;
    for (const line of content.split('\n')) {
        const trimmed = line.trim();
        if (trimmed === '[alias]') {
            inAliasSection = true;
            continue;
        }
        if (inAliasSection && trimmed.startsWith('[')) {
            break;
        }
        if (inAliasSection) {
            let m;
            aliasPattern.lastIndex = 0;
            if ((m = aliasPattern.exec(trimmed)) !== null && m[1]) {
                aliases.add(m[1]);
            }
        }
    }
    return aliases;
}
async function collectPythonScripts(projectPath) {
    const scripts = new Set();
    const content = await readFileSafe(join(projectPath, 'pyproject.toml'));
    if (!content) {
        return scripts;
    }
    const scriptPattern = /^\s*([\w-]+)\s*=\s*"/gm;
    let inScripts = false;
    for (const line of content.split('\n')) {
        const trimmed = line.trim();
        if (trimmed === '[project.scripts]') {
            inScripts = true;
            continue;
        }
        if (inScripts && trimmed.startsWith('[')) {
            break;
        }
        if (inScripts) {
            let m;
            scriptPattern.lastIndex = 0;
            if ((m = scriptPattern.exec(trimmed)) !== null && m[1]) {
                scripts.add(m[1]);
            }
        }
    }
    return scripts;
}
export async function collectProjectCommands(projectPath) {
    const [packageScripts, makefileTargets, shellScripts, cargoAliases, pythonScripts] = await Promise.all([
        collectPackageScripts(projectPath),
        collectMakefileTargets(projectPath),
        collectShellScripts(projectPath),
        collectCargoAliases(projectPath),
        collectPythonScripts(projectPath),
    ]);
    return { packageScripts, makefileTargets, shellScripts, cargoAliases, pythonScripts };
}
//# sourceMappingURL=ci-commands-collector.js.map