@likec4/log 1.18.0 → 1.19.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.cts CHANGED
@@ -3,82 +3,304 @@ type SelectOption = {
3
3
  value: string;
4
4
  hint?: string;
5
5
  };
6
- type TextOptions = {
6
+ declare const kCancel: unique symbol;
7
+ type PromptCommonOptions = {
8
+ /**
9
+ * Specify how to handle a cancelled prompt (e.g. by pressing Ctrl+C).
10
+ *
11
+ * Default strategy is `"default"`.
12
+ *
13
+ * - `"default"` - Resolve the promise with the `default` value or `initial` value.
14
+ * - `"undefined`" - Resolve the promise with `undefined`.
15
+ * - `"null"` - Resolve the promise with `null`.
16
+ * - `"symbol"` - Resolve the promise with a symbol `Symbol.for("cancel")`.
17
+ * - `"reject"` - Reject the promise with an error.
18
+ */
19
+ cancel?: "reject" | "default" | "undefined" | "null" | "symbol";
20
+ };
21
+ type TextPromptOptions = PromptCommonOptions & {
22
+ /**
23
+ * Specifies the prompt type as text.
24
+ * @optional
25
+ * @default "text"
26
+ */
7
27
  type?: "text";
28
+ /**
29
+ * The default text value.
30
+ * @optional
31
+ */
8
32
  default?: string;
33
+ /**
34
+ * A placeholder text displayed in the prompt.
35
+ * @optional
36
+ */
9
37
  placeholder?: string;
38
+ /**
39
+ * The initial text value.
40
+ * @optional
41
+ */
10
42
  initial?: string;
11
43
  };
12
- type ConfirmOptions = {
44
+ type ConfirmPromptOptions = PromptCommonOptions & {
45
+ /**
46
+ * Specifies the prompt type as confirm.
47
+ */
13
48
  type: "confirm";
49
+ /**
50
+ * The initial value for the confirm prompt.
51
+ * @optional
52
+ */
14
53
  initial?: boolean;
15
54
  };
16
- type SelectOptions = {
55
+ type SelectPromptOptions = PromptCommonOptions & {
56
+ /**
57
+ * Specifies the prompt type as select.
58
+ */
17
59
  type: "select";
60
+ /**
61
+ * The initial value for the select prompt.
62
+ * @optional
63
+ */
18
64
  initial?: string;
65
+ /**
66
+ * The options to select from. See {@link SelectOption}.
67
+ */
19
68
  options: (string | SelectOption)[];
20
69
  };
21
- type MultiSelectOptions = {
70
+ type MultiSelectOptions = PromptCommonOptions & {
71
+ /**
72
+ * Specifies the prompt type as multiselect.
73
+ */
22
74
  type: "multiselect";
23
- initial?: string;
24
- options: string[] | SelectOption[];
75
+ /**
76
+ * The options to select from. See {@link SelectOption}.
77
+ */
78
+ initial?: string[];
79
+ /**
80
+ * The options to select from. See {@link SelectOption}.
81
+ */
82
+ options: (string | SelectOption)[];
83
+ /**
84
+ * Whether the prompt requires at least one selection.
85
+ */
25
86
  required?: boolean;
26
87
  };
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>>;
88
+ /**
89
+ * Defines a combined type for all prompt options.
90
+ */
91
+ type PromptOptions = TextPromptOptions | ConfirmPromptOptions | SelectPromptOptions | MultiSelectOptions;
92
+ type inferPromptReturnType<T extends PromptOptions> = T extends TextPromptOptions ? string : T extends ConfirmPromptOptions ? boolean : T extends SelectPromptOptions ? T["options"][number] extends SelectOption ? T["options"][number]["value"] : T["options"][number] : T extends MultiSelectOptions ? T["options"] : unknown;
93
+ type inferPromptCancalReturnType<T extends PromptOptions> = T extends {
94
+ cancel: "reject";
95
+ } ? never : T extends {
96
+ cancel: "default";
97
+ } ? inferPromptReturnType<T> : T extends {
98
+ cancel: "undefined";
99
+ } ? undefined : T extends {
100
+ cancel: "null";
101
+ } ? null : T extends {
102
+ cancel: "symbol";
103
+ } ? typeof kCancel : inferPromptReturnType<T>;
104
+ /**
105
+ * Asynchronously prompts the user for input based on specified options.
106
+ * Supports text, confirm, select and multi-select prompts.
107
+ *
108
+ * @param {string} message - The message to display in the prompt.
109
+ * @param {PromptOptions} [opts={}] - The prompt options. See {@link PromptOptions}.
110
+ * @returns {Promise<inferPromptReturnType<T>>} - A promise that resolves with the user's response, the type of which is inferred from the options. See {@link inferPromptReturnType}.
111
+ */
112
+ declare function prompt<_ = any, __ = any, T extends PromptOptions = TextPromptOptions>(message: string, opts?: PromptOptions): Promise<inferPromptReturnType<T> | inferPromptCancalReturnType<T>>;
30
113
 
114
+ /**
115
+ * Defines the level of logs as specific numbers or special number types.
116
+ *
117
+ * @type {0 | 1 | 2 | 3 | 4 | 5 | (number & {})} LogLevel - Represents the log level.
118
+ * @default 0 - Represents the default log level.
119
+ */
31
120
  type LogLevel = 0 | 1 | 2 | 3 | 4 | 5 | (number & {});
121
+ /**
122
+ * A mapping of `LogType` to its corresponding numeric log level.
123
+ *
124
+ * @type {Record<LogType, number>} LogLevels - key-value pairs of log types to their numeric levels. See {@link LogType}.
125
+ */
32
126
  declare const LogLevels: Record<LogType, number>;
127
+ /**
128
+ * Lists the types of log messages supported by the system.
129
+ *
130
+ * @type {"silent" | "fatal" | "error" | "warn" | "log" | "info" | "success" | "fail" | "ready" | "start" | "box" | "debug" | "trace" | "verbose"} LogType - Represents the specific type of log message.
131
+ */
33
132
  type LogType = "silent" | "fatal" | "error" | "warn" | "log" | "info" | "success" | "fail" | "ready" | "start" | "box" | "debug" | "trace" | "verbose";
133
+ /**
134
+ * Maps `LogType` to a `Partial<LogObject>`, primarily defining the log level.
135
+ *
136
+ * @type {Record<LogType, Partial<LogObject>>} LogTypes - key-value pairs of log types to partial log objects, specifying log levels. See {@link LogType} and {@link LogObject}.
137
+ */
34
138
  declare const LogTypes: Record<LogType, Partial<LogObject>>;
35
139
 
36
140
  interface ConsolaOptions {
141
+ /**
142
+ * An array of ConsolaReporter instances used to handle and output log messages.
143
+ */
37
144
  reporters: ConsolaReporter[];
145
+ /**
146
+ * A record mapping LogType to InputLogObject, defining the log configuration for each log type.
147
+ * See {@link LogType} and {@link InputLogObject}.
148
+ */
38
149
  types: Record<LogType, InputLogObject>;
150
+ /**
151
+ * The minimum log level to output. See {@link LogLevel}.
152
+ */
39
153
  level: LogLevel;
154
+ /**
155
+ * Default properties applied to all log messages unless overridden. See {@link InputLogObject}.
156
+ */
40
157
  defaults: InputLogObject;
158
+ /**
159
+ * The maximum number of times a log message can be repeated within a given timeframe.
160
+ */
41
161
  throttle: number;
162
+ /**
163
+ * The minimum time in milliseconds that must elapse before a throttled log message can be logged again.
164
+ */
42
165
  throttleMin: number;
166
+ /**
167
+ * The Node.js writable stream for standard output. See {@link NodeJS.WriteStream}.
168
+ * @optional
169
+ */
43
170
  stdout?: NodeJS.WriteStream;
171
+ /**
172
+ * The Node.js writeable stream for standard error output. See {@link NodeJS.WriteStream}.
173
+ * @optional
174
+ */
44
175
  stderr?: NodeJS.WriteStream;
176
+ /**
177
+ * A function that allows you to mock log messages for testing purposes.
178
+ * @optional
179
+ */
45
180
  mockFn?: (type: LogType, defaults: InputLogObject) => (...args: any) => void;
181
+ /**
182
+ * Custom prompt function to use. It can be undefined.
183
+ * @optional
184
+ */
46
185
  prompt?: typeof prompt | undefined;
186
+ /**
187
+ * Configuration options for formatting log messages. See {@link FormatOptions}.
188
+ */
47
189
  formatOptions: FormatOptions;
48
190
  }
49
191
  /**
50
192
  * @see https://nodejs.org/api/util.html#util_util_inspect_object_showhidden_depth_colors
51
193
  */
52
194
  interface FormatOptions {
195
+ /**
196
+ * The maximum number of columns to output, affects formatting.
197
+ * @optional
198
+ */
53
199
  columns?: number;
200
+ /**
201
+ * Whether to include timestamp information in log messages.
202
+ * @optional
203
+ */
54
204
  date?: boolean;
205
+ /**
206
+ * Whether to use colors in the output.
207
+ * @optional
208
+ */
55
209
  colors?: boolean;
210
+ /**
211
+ * Specifies whether or not the output should be compact. Accepts a boolean or numeric level of compactness.
212
+ * @optional
213
+ */
56
214
  compact?: boolean | number;
215
+ /**
216
+ * Error cause level.
217
+ */
218
+ errorLevel?: number;
219
+ /**
220
+ * Allows additional custom formatting options.
221
+ */
57
222
  [key: string]: unknown;
58
223
  }
59
224
  interface InputLogObject {
225
+ /**
226
+ * The logging level of the message. See {@link LogLevel}.
227
+ * @optional
228
+ */
60
229
  level?: LogLevel;
230
+ /**
231
+ * A string tag to categorise or identify the log message.
232
+ * @optional
233
+ */
61
234
  tag?: string;
235
+ /**
236
+ * The type of log message, which affects how it's processed and displayed. See {@link LogType}.
237
+ * @optional
238
+ */
62
239
  type?: LogType;
240
+ /**
241
+ * The main log message text.
242
+ * @optional
243
+ */
63
244
  message?: string;
245
+ /**
246
+ * Additional text or texts to be logged with the message.
247
+ * @optional
248
+ */
64
249
  additional?: string | string[];
250
+ /**
251
+ * Additional arguments to be logged with the message.
252
+ * @optional
253
+ */
65
254
  args?: any[];
255
+ /**
256
+ * The date and time when the log message was created.
257
+ * @optional
258
+ */
66
259
  date?: Date;
67
260
  }
68
261
  interface LogObject extends InputLogObject {
262
+ /**
263
+ * The logging level of the message, overridden if required. See {@link LogLevel}.
264
+ */
69
265
  level: LogLevel;
266
+ /**
267
+ * The type of log message, overridden if required. See {@link LogType}.
268
+ */
70
269
  type: LogType;
270
+ /**
271
+ * A string tag to categorise or identify the log message, overridden if necessary.
272
+ */
71
273
  tag: string;
274
+ /**
275
+ * Additional arguments to be logged with the message, overridden if necessary.
276
+ */
72
277
  args: any[];
278
+ /**
279
+ * The date and time the log message was created, overridden if necessary.
280
+ */
73
281
  date: Date;
282
+ /**
283
+ * Allows additional custom properties to be set on the log object.
284
+ */
74
285
  [key: string]: unknown;
75
286
  }
76
287
  interface ConsolaReporter {
288
+ /**
289
+ * Defines how a log message is processed and displayed by this reporter.
290
+ * @param logObj The LogObject containing the log information to process. See {@link LogObject}.
291
+ * @param ctx An object containing context information such as options. See {@link ConsolaOptions}.
292
+ */
77
293
  log: (logObj: LogObject, ctx: {
78
294
  options: ConsolaOptions;
79
295
  }) => void;
80
296
  }
81
297
 
298
+ /**
299
+ * Consola class for logging management with support for pause/resume, mocking and customisable reporting.
300
+ * Provides flexible logging capabilities including level-based logging, custom reporters and integration options.
301
+ *
302
+ * @class Consola
303
+ */
82
304
  declare class Consola {
83
305
  options: ConsolaOptions;
84
306
  _lastLog: {
@@ -89,26 +311,133 @@ declare class Consola {
89
311
  timeout?: ReturnType<typeof setTimeout>;
90
312
  };
91
313
  _mockFn?: ConsolaOptions["mockFn"];
314
+ /**
315
+ * Creates an instance of Consola with specified options or defaults.
316
+ *
317
+ * @param {Partial<ConsolaOptions>} [options={}] - Configuration options for the Consola instance.
318
+ */
92
319
  constructor(options?: Partial<ConsolaOptions>);
320
+ /**
321
+ * Gets the current log level of the Consola instance.
322
+ *
323
+ * @returns {number} The current log level.
324
+ */
93
325
  get level(): LogLevel;
326
+ /**
327
+ * Sets the minimum log level that will be output by the instance.
328
+ *
329
+ * @param {number} level - The new log level to set.
330
+ */
94
331
  set level(level: LogLevel);
95
- 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>;
332
+ /**
333
+ * Displays a prompt to the user and returns the response.
334
+ * Throw an error if `prompt` is not supported by the current configuration.
335
+ *
336
+ * @template T
337
+ * @param {string} message - The message to display in the prompt.
338
+ * @param {T} [opts] - Optional options for the prompt. See {@link PromptOptions}.
339
+ * @returns {promise<T>} A promise that infer with the prompt options. See {@link PromptOptions}.
340
+ */
341
+ prompt<T extends PromptOptions>(message: string, opts?: T): Promise<(T extends TextPromptOptions ? string : T extends ConfirmPromptOptions ? boolean : T extends SelectPromptOptions ? T["options"][number] extends {
342
+ label: string;
343
+ value: string;
344
+ hint?: string;
345
+ } ? T["options"][number]["value"] : T["options"][number] : T extends MultiSelectOptions ? T["options"] : unknown) | (T extends {
346
+ cancel: "reject";
347
+ } ? never : T extends {
348
+ cancel: "default";
349
+ } ? T extends infer T_1 ? T_1 extends T ? T_1 extends TextPromptOptions ? string : T_1 extends ConfirmPromptOptions ? boolean : T_1 extends SelectPromptOptions ? T_1["options"][number] extends {
350
+ label: string;
351
+ value: string;
352
+ hint?: string;
353
+ } ? T_1["options"][number]["value"] : T_1["options"][number] : T_1 extends MultiSelectOptions ? T_1["options"] : unknown : never : never : T extends {
354
+ cancel: "undefined";
355
+ } ? undefined : T extends {
356
+ cancel: "null";
357
+ } ? null : T extends {
358
+ cancel: "symbol";
359
+ } ? typeof kCancel : T extends TextPromptOptions ? string : T extends ConfirmPromptOptions ? boolean : T extends SelectPromptOptions ? T["options"][number] extends {
360
+ label: string;
361
+ value: string;
362
+ hint?: string;
363
+ } ? T["options"][number]["value"] : T["options"][number] : T extends MultiSelectOptions ? T["options"] : unknown)>;
364
+ /**
365
+ * Creates a new instance of Consola, inheriting options from the current instance, with possible overrides.
366
+ *
367
+ * @param {Partial<ConsolaOptions>} options - Optional overrides for the new instance. See {@link ConsolaOptions}.
368
+ * @returns {ConsolaInstance} A new Consola instance. See {@link ConsolaInstance}.
369
+ */
96
370
  create(options: Partial<ConsolaOptions>): ConsolaInstance;
371
+ /**
372
+ * Creates a new Consola instance with the specified default log object properties.
373
+ *
374
+ * @param {InputLogObject} defaults - Default properties to include in any log from the new instance. See {@link InputLogObject}.
375
+ * @returns {ConsolaInstance} A new Consola instance. See {@link ConsolaInstance}.
376
+ */
97
377
  withDefaults(defaults: InputLogObject): ConsolaInstance;
378
+ /**
379
+ * Creates a new Consola instance with a specified tag, which will be included in every log.
380
+ *
381
+ * @param {string} tag - The tag to include in each log of the new instance.
382
+ * @returns {ConsolaInstance} A new Consola instance. See {@link ConsolaInstance}.
383
+ */
98
384
  withTag(tag: string): ConsolaInstance;
385
+ /**
386
+ * Adds a custom reporter to the Consola instance.
387
+ * Reporters will be called for each log message, depending on their implementation and log level.
388
+ *
389
+ * @param {ConsolaReporter} reporter - The reporter to add. See {@link ConsolaReporter}.
390
+ * @returns {Consola} The current Consola instance.
391
+ */
99
392
  addReporter(reporter: ConsolaReporter): this;
100
- removeReporter(reporter: ConsolaReporter): ConsolaReporter[] | this;
393
+ /**
394
+ * Removes a custom reporter from the Consola instance.
395
+ * If no reporter is specified, all reporters will be removed.
396
+ *
397
+ * @param {ConsolaReporter} reporter - The reporter to remove. See {@link ConsolaReporter}.
398
+ * @returns {Consola} The current Consola instance.
399
+ */
400
+ removeReporter(reporter: ConsolaReporter): this | ConsolaReporter[];
401
+ /**
402
+ * Replaces all reporters of the Consola instance with the specified array of reporters.
403
+ *
404
+ * @param {ConsolaReporter[]} reporters - The new reporters to set. See {@link ConsolaReporter}.
405
+ * @returns {Consola} The current Consola instance.
406
+ */
101
407
  setReporters(reporters: ConsolaReporter[]): this;
102
408
  wrapAll(): void;
103
409
  restoreAll(): void;
410
+ /**
411
+ * Overrides console methods with Consola logging methods for consistent logging.
412
+ */
104
413
  wrapConsole(): void;
414
+ /**
415
+ * Restores the original console methods, removing Consola overrides.
416
+ */
105
417
  restoreConsole(): void;
418
+ /**
419
+ * Overrides standard output and error streams to redirect them through Consola.
420
+ */
106
421
  wrapStd(): void;
107
422
  _wrapStream(stream: NodeJS.WriteStream | undefined, type: LogType): void;
423
+ /**
424
+ * Restores the original standard output and error streams, removing the Consola redirection.
425
+ */
108
426
  restoreStd(): void;
109
427
  _restoreStream(stream?: NodeJS.WriteStream): void;
428
+ /**
429
+ * Pauses logging, queues incoming logs until resumed.
430
+ */
110
431
  pauseLogs(): void;
432
+ /**
433
+ * Resumes logging, processing any queued logs.
434
+ */
111
435
  resumeLogs(): void;
436
+ /**
437
+ * Replaces logging methods with mocks if a mock function is provided.
438
+ *
439
+ * @param {ConsolaOptions["mockFn"]} mockFn - The function to use for mocking logging methods. See {@link ConsolaOptions["mockFn"]}.
440
+ */
112
441
  mockTypes(mockFn?: ConsolaOptions["mockFn"]): void;
113
442
  _wrapLogFn(defaults: InputLogObject, isRaw?: boolean): (...args: any[]) => false | undefined;
114
443
  _logFn(defaults: InputLogObject, args: any[], isRaw?: boolean): false | undefined;
@@ -119,8 +448,24 @@ interface LogFn {
119
448
  raw: (...args: any[]) => void;
120
449
  }
121
450
  type ConsolaInstance = Consola & Record<LogType, LogFn>;
451
+ /**
452
+ * Utility for creating a new Consola instance with optional configuration.
453
+ *
454
+ * @param {Partial<ConsolaOptions>} [options={}] - Optional configuration options for the new Consola instance. See {@link ConsolaOptions}.
455
+ * @returns {ConsolaInstance} A new instance of Consola. See {@link ConsolaInstance}.
456
+ */
122
457
  declare function createConsola(options?: Partial<ConsolaOptions>): ConsolaInstance;
123
458
 
459
+ type FormattedLogObject = {
460
+ message: string;
461
+ error?: {
462
+ message: string;
463
+ name: string;
464
+ stack?: string;
465
+ };
466
+ };
467
+
468
+ declare function formatLogObj(logObj: LogObject): FormattedLogObject;
124
469
  declare const consola: ConsolaInstance;
125
470
 
126
- export { Consola, type ConsolaInstance, type ConsolaOptions, type ConsolaReporter, type FormatOptions, type InputLogObject, type LogLevel, LogLevels, type LogObject, type LogType, LogTypes, consola, createConsola, consola as logger, consola as rootLogger };
471
+ export { type ConfirmPromptOptions, Consola, type ConsolaInstance, type ConsolaOptions, type ConsolaReporter, type FormatOptions, type FormattedLogObject, type InputLogObject, type LogLevel, LogLevels, type LogObject, type LogType, LogTypes, type MultiSelectOptions, type PromptOptions, type SelectPromptOptions, type TextPromptOptions, consola, createConsola, formatLogObj, consola as logger, consola as rootLogger };