UNPKG

3.52 kBTypeScriptView Raw
1import { Color, Modifiers } from "chalk";
2import { EventEmitter } from "events";
3import { WriteStream } from "tty";
4import table = require("text-table");
5import { format } from "util";
6
7declare namespace createLogger {
8 /**
9 * Provides a set of colors.
10 */
11 type ColorMap<TKeys extends string | number | symbol> = {
12 /**
13 * Gets the color for the specified method-name.
14 */
15 [P in TKeys]: typeof Color | typeof Modifiers;
16 };
17
18 /**
19 * Provides default color-categories.
20 */
21 type DefaultCategories =
22 | "skip"
23 | "force"
24 | "create"
25 | "invoke"
26 | "conflict"
27 | "identical"
28 | "info";
29
30 /**
31 * Provides options for creating a logger.
32 */
33 interface LoggerOptions<TCategories extends string | number | symbol = DefaultCategories> {
34 /**
35 * A set of categories and assigned `chalk`-formats.
36 */
37 colors?: ColorMap<TCategories> | undefined;
38
39 /**
40 * The console to write log-messages to.
41 */
42 console?: Console | undefined;
43
44 /**
45 * The stream to write other messages to.
46 */
47 stderr?: WriteStream | undefined;
48
49 /**
50 * The stream to write other messages to.
51 */
52 stdout?: WriteStream | undefined;
53 }
54
55 /**
56 * Provides the functionality to log messages.
57 */
58 type Logger<TCategories extends string | number | symbol = DefaultCategories> =
59 & EventEmitter
60 & {
61 /**
62 * Logs a message of the specified category.
63 */
64 [P in TCategories]: (...args: Parameters<typeof format>) => Logger<TCategories>;
65 }
66 & {
67 /**
68 * Writes a log-message.
69 *
70 * @param format
71 * The format of the log-messages.
72 * See <https://github.com/mikeal/logref> for more info.
73 *
74 * @param params
75 * The parameters to replace variables with.
76 */
77 (format?: string, params?: Record<string, any>): Logger<TCategories>;
78
79 /**
80 * Writes a log-message.
81 */
82 (...args: Parameters<Console["error"]>): Logger<TCategories>;
83
84 /**
85 * Writes a log-message.
86 */
87 write(...args: Parameters<typeof format>): Logger<TCategories>;
88
89 /**
90 * Writes a log-message with an appended newline character.
91 */
92 writeln(...args: Parameters<typeof format>): Logger<TCategories>;
93
94 /**
95 * Writes a success status with a check mark `✔`.
96 */
97 ok(...args: Parameters<typeof format>): Logger<TCategories>;
98
99 /**
100 * Writes an error-message with a prepended cross mark.
101 */
102 error(...args: Parameters<typeof format>): Logger<TCategories>;
103
104 /**
105 * Writes a table to the console.
106 */
107 table: typeof table;
108 };
109}
110
111/**
112 * Creates a new `Logger` instance with the specified `options`.
113 *
114 * @param options
115 * The options for creating the new logger.
116 */
117declare function createLogger<TCategories extends string | number | symbol = createLogger.DefaultCategories>(
118 options: createLogger.LoggerOptions<TCategories>,
119): createLogger.Logger<TCategories>;
120
121/**
122 * Creates a logger with the specified `options`.
123 */
124export = createLogger;