import { execFile } from 'node:child_process';
import { promisify } from 'node:util';
const execFileAsync = promisify(execFile);
export interface ClioResult {
success: boolean;
output: string;
error?: string;
}
/**
* Execute a CLIO command with the given arguments.
* Assumes 'clio' is available on PATH (installed via `dotnet tool install clio -g`).
*
* @param command - The CLIO command to run (e.g., 'ping', 'restart', 'push-pkg')
* @param args - Command arguments (e.g., ['-e', 'myenv'])
* @param timeoutMs - Timeout in milliseconds (default: 30000)
* @returns ClioResult with success status, output, and optional error message
*/
export async function executeClio(
command: string,
args: string[] = [],
timeoutMs: number = 30000
): Promise<ClioResult> {
try {
const { stdout, stderr } = await execFileAsync(
'clio',
[command, ...args],
{
timeout: timeoutMs,
maxBuffer: 1024 * 1024 * 10, // 10MB buffer
}
);
// CLIO may write informational messages to stderr even on success
const output = stdout.trim() || stderr.trim();
return {
success: true,
output: output || 'Command completed successfully',
};
} catch (error: any) {
// Handle execution errors (non-zero exit code, timeout, etc.)
const errorMessage = error.stderr?.trim() || error.stdout?.trim() || error.message;
return {
success: false,
output: error.stdout?.trim() || '',
error: errorMessage || 'Command execution failed',
};
}
}