All files / src/utils output.ts

100% Statements 14/14
100% Branches 8/8
100% Functions 4/4
100% Lines 14/14

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32                          1x 11x 10x 10x   1x 3x 3x   1x 5x 5x     1x 2x 2x 2x  
export interface SuccessResponse<T = unknown> {
  ok: true;
  data?: T;
}
 
export interface ErrorResponse {
  ok: false;
  error: string;
  message: string;
}
 
export type CLIResponse<T = unknown> = SuccessResponse<T> | ErrorResponse;
 
export function success<T>(data?: T): SuccessResponse<T> {
  if (data === undefined) return { ok: true } as SuccessResponse<T>;
  return { ok: true, data };
}
 
export function error(type: string, message: string): ErrorResponse {
  return { ok: false, error: type, message };
}
 
export function formatOutput(response: CLIResponse): string {
  return JSON.stringify(response, null, 2);
}
 
/** Print response to stdout and exit with appropriate code */
export function output(response: CLIResponse): void {
  console.log(formatOutput(response));
  process.exitCode = response.ok ? 0 : 1;
}