// engine/detectors/nomad-detector.ts — HashiCorp Nomad workload orchestrator detection (SPEC-033 TD-P14-02)
// Detects Nomad via .nomad/.hcl job files, nomad.hcl config, and nomad/ directory.
import { readFile } from 'node:fs/promises';
import { join } from 'node:path';
import { glob } from 'glob';
// === Detection signals ===
/** Explicit top-level files that indicate Nomad is configured. */
const NOMAD_EXPLICIT_PATHS = ['nomad.hcl', 'nomad.json'];
/** Directories that indicate Nomad is in use. */
const NOMAD_DIRS = ['nomad/', '.nomad/'];
/** Content patterns that identify a Nomad job specification inside HCL/nomad files. */
const NOMAD_JOB_PATTERNS = ['job "', "job '", 'job{'];
/** Content pattern inside .nomad files to verify they are Nomad job specs. */
const NOMAD_TASK_PATTERN = 'task "';
// === Public API ===
/**
 * Detects whether a project uses HashiCorp Nomad as its workload orchestrator.
 *
 * Checks:
 * 1. Explicit nomad.hcl / nomad.json config at project root
 * 2. nomad/ or .nomad/ directories
 * 3. *.nomad files anywhere in the project (must exist)
 * 4. *.hcl files containing `job "` or `task "` patterns (Nomad job specs)
 */
export async function detectNomad(projectPath) {
    // Check explicit top-level Nomad config files
    for (const p of NOMAD_EXPLICIT_PATHS) {
        if (await fileExists(join(projectPath, p))) {
            return true;
        }
    }
    // Check Nomad directories
    for (const dir of NOMAD_DIRS) {
        if (await fileExists(join(projectPath, dir))) {
            return true;
        }
    }
    // Check .nomad files (presence alone is sufficient — they are Nomad-specific)
    const nomadFiles = await globMatch(projectPath, '**/*.nomad');
    if (nomadFiles.length > 0) {
        return true;
    }
    // Check .hcl files for Nomad job spec patterns
    const hclFound = await detectNomadInHclFiles(projectPath);
    if (hclFound) {
        return true;
    }
    return false;
}
// === Internal helpers ===
async function detectNomadInHclFiles(projectPath) {
    try {
        const matches = await glob('**/*.hcl', {
            cwd: projectPath,
            nodir: true,
            ignore: ['node_modules/**', '.git/**', 'vendor/**'],
        });
        for (const match of matches) {
            const hasNomadPattern = await fileContainsAny(join(projectPath, match), NOMAD_JOB_PATTERNS, NOMAD_TASK_PATTERN);
            if (hasNomadPattern) {
                return true;
            }
        }
    }
    catch {
        // ignore glob errors
    }
    return false;
}
async function globMatch(projectPath, pattern) {
    try {
        return await glob(pattern, {
            cwd: projectPath,
            nodir: true,
            ignore: ['node_modules/**', '.git/**', 'vendor/**'],
        });
    }
    catch {
        return [];
    }
}
async function fileExists(filePath) {
    try {
        await readFile(filePath, 'utf-8');
        return true;
    }
    catch {
        return false;
    }
}
async function fileContainsAny(filePath, jobPatterns, taskPattern) {
    try {
        const content = await readFile(filePath, 'utf-8');
        const hasJob = jobPatterns.some((p) => content.includes(p));
        const hasTask = content.includes(taskPattern);
        return hasJob || hasTask;
    }
    catch {
        return false;
    }
}
//# sourceMappingURL=nomad-detector.js.map