/**
 * Pre-flight environment checks
 *
 * Run at the start of init, start, and join to surface missing dependencies
 * as clear, actionable messages before any command logic executes.
 */
import { execFileSync } from 'child_process';
import { log } from '@clack/prompts';
/** Minimum supported Node.js major version */
export const MIN_NODE_MAJOR = 20;
/**
 * Check that git is installed and on PATH.
 */
export function checkGit() {
    try {
        const output = execFileSync('git', ['--version'], { stdio: 'pipe' }).toString().trim();
        const match = output.match(/git version (\S+)/);
        const version = match ? match[1] : output;
        return { passed: true, message: `git ${version} found` };
    }
    catch {
        return {
            passed: false,
            message: 'git not found',
            hint: 'Install git from https://git-scm.com',
        };
    }
}
/**
 * Check that the running Node.js meets the minimum version requirement.
 */
export function checkNode() {
    const version = process.versions.node;
    const major = parseInt(version.split('.')[0], 10);
    if (major >= MIN_NODE_MAJOR) {
        return { passed: true, message: `Node.js v${version} found` };
    }
    return {
        passed: false,
        message: `Node.js v${version} is too old (requires v${MIN_NODE_MAJOR}+)`,
        hint: `Install Node.js v${MIN_NODE_MAJOR}+ from https://nodejs.org`,
    };
}
/**
 * Run pre-flight checks and print results using clack log symbols.
 *
 * Required checks (git, Node.js) print as success or error.
 *
 * Returns false if any required check fails — caller should exit(1).
 */
export function runPreflight(_options = {}) {
    const gitResult = checkGit();
    const nodeResult = checkNode();
    if (gitResult.passed) {
        log.success(gitResult.message);
    }
    else {
        log.error(`${gitResult.message}${gitResult.hint ? ` — ${gitResult.hint}` : ''}`);
    }
    if (nodeResult.passed) {
        log.success(nodeResult.message);
    }
    else {
        log.error(`${nodeResult.message}${nodeResult.hint ? ` — ${nodeResult.hint}` : ''}`);
    }
    return gitResult.passed && nodeResult.passed;
}
//# sourceMappingURL=preflight.js.map