/**
 * Shared utilities for install/uninstall scripts.
 *
 * Used by preflight.ts and uninstall.ts to avoid duplicating helpers
 * for logging, running commands, and running PowerShell scripts.
 */
import chalk from 'chalk';
import { spawnSync } from 'child_process';
// ── Logging ──────────────────────────────────────────────────────────────
export function ok(msg) {
    console.log(chalk.green('  ✓ ') + msg);
}
export function warn(msg) {
    console.log(chalk.yellow('  ⚠ ') + msg);
}
export function header(step) {
    console.log();
    console.log(chalk.bold.white(`  ${step}`));
    console.log(chalk.dim('  ' + '─'.repeat(40)));
}
// ── Command execution ────────────────────────────────────────────────────
/** Run a command synchronously and return { ok, stdout, stderr }. */
export function run(cmd, options) {
    try {
        const result = spawnSync(cmd, {
            encoding: 'utf-8',
            timeout: options?.timeout ?? 30_000,
            shell: true,
            stdio: ['ignore', 'pipe', 'pipe']
        });
        return {
            ok: result.status === 0,
            stdout: (result.stdout ?? '').trim(),
            stderr: (result.stderr ?? '').trim()
        };
    }
    catch {
        return { ok: false, stdout: '', stderr: 'command failed' };
    }
}
/** Run a PowerShell script via base64-encoded command. */
export function runPS(script, options) {
    const encoded = Buffer.from(script, 'utf16le').toString('base64');
    return run(`powershell -NoProfile -EncodedCommand ${encoded}`, options);
}
/**
 * Escape a string for safe interpolation inside a PowerShell double-quoted string.
 * Handles: backtick, dollar sign, double quote, and null byte.
 */
export function escapePSString(value) {
    return value.replace(/`/g, '``').replace(/\$/g, '`$').replace(/"/g, '`"');
}
//# sourceMappingURL=install-utils.js.map