export type CommandResult = { exitCode: 0 } | { exitCode: number; message: string };

export function success(): CommandResult {
  return { exitCode: 0 };
}

export function failure(message: string, exitCode = 1): CommandResult {
  return { exitCode, message };
}

export function silentFailure(exitCode = 1): CommandResult {
  return { exitCode, message: "" };
}

// Adds what politty can't express: custom exit codes and silent failures.
// Throws are left to politty, which prints the error and exits 1.
export async function executeCommand(
  fn: () => CommandResult | Promise<CommandResult>,
): Promise<never> {
  const result = await fn();
  if (result.exitCode !== 0 && "message" in result && result.message) {
    console.error(result.message);
  }
  process.exit(result.exitCode);
}
