-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoptions.ts
More file actions
68 lines (64 loc) · 2.3 KB
/
options.ts
File metadata and controls
68 lines (64 loc) · 2.3 KB
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/**
* @file Constructor-options parsing for the `Logger` class. Pulls the
* options-shape inspection (null-prototype clone, original-stdout capture,
* theme-name-or-object resolution) out of `./node` so the class constructor
* stays a thin shell over `parseLoggerOptions`. Returns a normalized
* `ParsedLoggerOptions`; the constructor copies the fields onto its private
* slots.
*/
import { THEMES } from '../themes/themes'
import type { Theme } from '../themes/types'
/**
* The normalized result of inspecting the first `Logger` constructor argument.
*/
export interface ParsedLoggerOptions {
/**
* Null-prototype clone of the options object (empty when no options were
* passed). Stored for future extensibility.
*/
options: Record<string, unknown>
/**
* The caller-supplied stdout stream, used by `write()` to bypass Console
* formatting. `undefined` when not provided.
*/
originalStdout: NodeJS.WritableStream | undefined
/**
* The resolved instance theme: a `THEMES` entry when a theme name was given,
* the object itself when a `Theme` was given, or `undefined` otherwise.
*/
theme: Theme | undefined
}
/**
* Parse the first `Logger` constructor argument into normalized option slots.
*
* A `theme` string is resolved against `THEMES` (unknown names yield no theme);
* a `theme` object is used directly. When the first argument is not an object,
* every slot defaults (empty null-prototype options, no stdout, no theme).
*
* @param args - The raw `Logger` constructor arguments.
*/
export function parseLoggerOptions(args: unknown[]): ParsedLoggerOptions {
const options = args[0]
if (typeof options !== 'object' || options === null) {
return {
options: { __proto__: null } as Record<string, unknown>,
originalStdout: undefined,
theme: undefined,
}
}
const originalStdout = (
options as { stdout?: NodeJS.WritableStream | undefined }
).stdout
const themeOption = (options as { theme?: unknown | undefined }).theme
let theme: Theme | undefined
if (typeof themeOption === 'string') {
theme = THEMES[themeOption as keyof typeof THEMES] ?? undefined
} else if (themeOption) {
theme = themeOption as Theme
}
return {
options: { __proto__: null, ...options } as Record<string, unknown>,
originalStdout,
theme,
}
}