core.d.ts 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. type SelectOption = {
  2. label: string;
  3. value: string;
  4. hint?: string;
  5. };
  6. type TextOptions = {
  7. type?: "text";
  8. default?: string;
  9. placeholder?: string;
  10. initial?: string;
  11. };
  12. type ConfirmOptions = {
  13. type: "confirm";
  14. initial?: boolean;
  15. };
  16. type SelectOptions = {
  17. type: "select";
  18. initial?: string;
  19. options: (string | SelectOption)[];
  20. };
  21. type MultiSelectOptions = {
  22. type: "multiselect";
  23. initial?: string;
  24. options: string[] | SelectOption[];
  25. required?: boolean;
  26. };
  27. type PromptOptions = TextOptions | ConfirmOptions | SelectOptions | MultiSelectOptions;
  28. type inferPromptReturnType<T extends PromptOptions> = T extends TextOptions ? string : T extends ConfirmOptions ? boolean : T extends SelectOptions ? T["options"][number] : T extends MultiSelectOptions ? T["options"] : unknown;
  29. declare function prompt<_ = any, __ = any, T extends PromptOptions = TextOptions>(message: string, opts?: PromptOptions): Promise<inferPromptReturnType<T>>;
  30. type LogLevel = 0 | 1 | 2 | 3 | 4 | 5 | (number & {});
  31. declare const LogLevels: Record<LogType, number>;
  32. type LogType = "silent" | "fatal" | "error" | "warn" | "log" | "info" | "success" | "fail" | "ready" | "start" | "box" | "debug" | "trace" | "verbose";
  33. declare const LogTypes: Record<LogType, Partial<LogObject>>;
  34. interface ConsolaOptions {
  35. reporters: ConsolaReporter[];
  36. types: Record<LogType, InputLogObject>;
  37. level: LogLevel;
  38. defaults: InputLogObject;
  39. throttle: number;
  40. throttleMin: number;
  41. stdout?: NodeJS.WriteStream;
  42. stderr?: NodeJS.WriteStream;
  43. mockFn?: (type: LogType, defaults: InputLogObject) => (...args: any) => void;
  44. prompt?: typeof prompt | undefined;
  45. formatOptions: FormatOptions;
  46. }
  47. /**
  48. * @see https://nodejs.org/api/util.html#util_util_inspect_object_showhidden_depth_colors
  49. */
  50. interface FormatOptions {
  51. columns?: number;
  52. date?: boolean;
  53. colors?: boolean;
  54. compact?: boolean | number;
  55. [key: string]: unknown;
  56. }
  57. interface InputLogObject {
  58. level?: LogLevel;
  59. tag?: string;
  60. type?: LogType;
  61. message?: string;
  62. additional?: string | string[];
  63. args?: any[];
  64. date?: Date;
  65. }
  66. interface LogObject extends InputLogObject {
  67. level: LogLevel;
  68. type: LogType;
  69. tag: string;
  70. args: any[];
  71. date: Date;
  72. [key: string]: unknown;
  73. }
  74. interface ConsolaReporter {
  75. log: (logObj: LogObject, ctx: {
  76. options: ConsolaOptions;
  77. }) => void;
  78. }
  79. declare class Consola {
  80. options: ConsolaOptions;
  81. _lastLog: {
  82. serialized?: string;
  83. object?: LogObject;
  84. count?: number;
  85. time?: Date;
  86. timeout?: ReturnType<typeof setTimeout>;
  87. };
  88. _mockFn?: ConsolaOptions["mockFn"];
  89. constructor(options?: Partial<ConsolaOptions>);
  90. get level(): LogLevel;
  91. set level(level: LogLevel);
  92. prompt<T extends PromptOptions>(message: string, opts?: T): Promise<T extends TextOptions ? string : T extends ConfirmOptions ? boolean : T extends SelectOptions ? T["options"][number] : T extends MultiSelectOptions ? T["options"] : unknown>;
  93. create(options: Partial<ConsolaOptions>): ConsolaInstance;
  94. withDefaults(defaults: InputLogObject): ConsolaInstance;
  95. withTag(tag: string): ConsolaInstance;
  96. addReporter(reporter: ConsolaReporter): this;
  97. removeReporter(reporter: ConsolaReporter): ConsolaReporter[] | this;
  98. setReporters(reporters: ConsolaReporter[]): this;
  99. wrapAll(): void;
  100. restoreAll(): void;
  101. wrapConsole(): void;
  102. restoreConsole(): void;
  103. wrapStd(): void;
  104. _wrapStream(stream: NodeJS.WriteStream | undefined, type: LogType): void;
  105. restoreStd(): void;
  106. _restoreStream(stream?: NodeJS.WriteStream): void;
  107. pauseLogs(): void;
  108. resumeLogs(): void;
  109. mockTypes(mockFn?: ConsolaOptions["mockFn"]): void;
  110. _wrapLogFn(defaults: InputLogObject, isRaw?: boolean): (...args: any[]) => false | undefined;
  111. _logFn(defaults: InputLogObject, args: any[], isRaw?: boolean): false | undefined;
  112. _log(logObj: LogObject): void;
  113. }
  114. interface LogFn {
  115. (message: InputLogObject | any, ...args: any[]): void;
  116. raw: (...args: any[]) => void;
  117. }
  118. type ConsolaInstance = Consola & Record<LogType, LogFn>;
  119. declare function createConsola(options?: Partial<ConsolaOptions>): ConsolaInstance;
  120. export { Consola, ConsolaInstance, ConsolaOptions, ConsolaReporter, FormatOptions, InputLogObject, LogLevel, LogLevels, LogObject, LogType, LogTypes, createConsola };