|
1 |
| -import { stripVTControlCharacters } from "node:util"; |
| 1 | +import chalk from "chalk"; |
| 2 | +import { |
| 3 | + inspect, |
| 4 | + type InspectOptions, |
| 5 | + stripVTControlCharacters, |
| 6 | +} from "node:util"; |
| 7 | +import { allLogLevels } from "./levels.ts"; |
2 | 8 | import type {
|
3 | 9 | ColorSettings,
|
4 | 10 | DeepPartial,
|
5 | 11 | FormatHelper,
|
6 |
| - ReporterSettings, |
| 12 | + FormattingOptions, |
| 13 | + FormattingSettings, |
7 | 14 | } from "./types.ts";
|
8 | 15 |
|
9 | 16 | export function noChange<T>(arg: T) {
|
10 | 17 | return arg;
|
11 | 18 | }
|
12 | 19 |
|
13 |
| -function asObject<T extends object>(item: unknown): T | undefined { |
14 |
| - if (item && typeof item === "object" && !Array.isArray(item)) { |
15 |
| - return item as T; |
| 20 | +export type Formatting = FormattingSettings & { |
| 21 | + format: FormatHelper; |
| 22 | +}; |
| 23 | + |
| 24 | +const defaultFormatSettings: FormattingSettings = { |
| 25 | + inspectOptions: { |
| 26 | + colors: true, |
| 27 | + depth: 2, |
| 28 | + compact: true, |
| 29 | + }, |
| 30 | + colors: { |
| 31 | + error: { prefix: chalk.red.bold }, |
| 32 | + warn: { prefix: chalk.yellowBright.bold }, |
| 33 | + log: {}, |
| 34 | + verbose: { text: chalk.dim }, |
| 35 | + labels: chalk.bold, |
| 36 | + pkgName: chalk.bold.cyan, |
| 37 | + pkgScope: chalk.bold.blue, |
| 38 | + path: chalk.blue, |
| 39 | + duration: chalk.green, |
| 40 | + durationUnits: chalk.greenBright, |
| 41 | + }, |
| 42 | + prefixes: { |
| 43 | + error: "ERROR: ⛔", |
| 44 | + warn: "WARNING: ⚠️", |
| 45 | + }, |
| 46 | +}; |
| 47 | + |
| 48 | +const defaultFormatting: Formatting = { |
| 49 | + ...defaultFormatSettings, |
| 50 | + format: createFormatHelper(defaultFormatSettings), |
| 51 | +}; |
| 52 | + |
| 53 | +export function getFormatting( |
| 54 | + overrides?: FormattingOptions, |
| 55 | + baseline: Formatting = defaultFormatting |
| 56 | +): Formatting { |
| 57 | + if (overrides) { |
| 58 | + const { colors, inspectOptions, prefixes } = overrides; |
| 59 | + const rebuildFormat = colors || inspectOptions; |
| 60 | + |
| 61 | + // if settings have changed, create a new formatting object |
| 62 | + if (rebuildFormat || prefixes) { |
| 63 | + const result = { |
| 64 | + colors: mergeColors(baseline.colors, colors), |
| 65 | + inspectOptions: mergeInspectOptions( |
| 66 | + baseline.inspectOptions, |
| 67 | + inspectOptions |
| 68 | + ), |
| 69 | + prefixes: mergePrefixes(baseline.prefixes, prefixes), |
| 70 | + format: baseline.format, |
| 71 | + }; |
| 72 | + // update the format helper if needed, otherwise carry it forward |
| 73 | + if (rebuildFormat) { |
| 74 | + result.format = createFormatHelper(result); |
| 75 | + } |
| 76 | + return result; |
| 77 | + } |
16 | 78 | }
|
17 |
| - return undefined; |
| 79 | + return baseline; |
18 | 80 | }
|
19 | 81 |
|
20 |
| -/** |
21 |
| - * Deep merges two objects, applying the source object properties to the target object. |
22 |
| - * @param target the object to merge into |
23 |
| - * @param source the object to merge from |
24 |
| - * @param immutable if true, the target object will not be modified, instead a new object will be returned |
25 |
| - * @returns either target with applied updates (if !immutable) or a new object with the merged settings |
26 |
| - */ |
27 |
| -export function mergeSettings<T extends object>( |
28 |
| - target: T, |
29 |
| - source?: DeepPartial<T>, |
30 |
| - immutable?: boolean |
31 |
| -): T { |
32 |
| - if (!source) { |
33 |
| - return target; |
34 |
| - } |
35 |
| - if (immutable) { |
36 |
| - target = { ...target }; |
| 82 | +export function updateDefaultFormatting(options?: FormattingOptions) { |
| 83 | + const newDefault = getFormatting(options); |
| 84 | + if (newDefault !== defaultFormatting) { |
| 85 | + defaultFormatting.colors = newDefault.colors; |
| 86 | + defaultFormatting.inspectOptions = newDefault.inspectOptions; |
| 87 | + defaultFormatting.prefixes = newDefault.prefixes; |
| 88 | + defaultFormatting.format = newDefault.format; |
37 | 89 | }
|
38 |
| - for (const key in source) { |
39 |
| - if (source[key] !== undefined) { |
40 |
| - const objValue = asObject(source[key]); |
41 |
| - const objTarget = asObject(target[key]); |
42 |
| - if (objValue && objTarget) { |
43 |
| - target[key] = mergeSettings( |
44 |
| - objTarget, |
45 |
| - objValue, |
46 |
| - immutable |
47 |
| - ) as T[Extract<keyof T, string>]; |
48 |
| - } else { |
49 |
| - target[key] = source[key] as T[Extract<keyof T, string>]; |
| 90 | +} |
| 91 | + |
| 92 | +export function disableColors() { |
| 93 | + const disableMsgType = { text: stripVTControlCharacters }; |
| 94 | + updateDefaultFormatting({ |
| 95 | + inspectOptions: { colors: false }, |
| 96 | + colors: { |
| 97 | + error: disableMsgType, |
| 98 | + warn: disableMsgType, |
| 99 | + log: disableMsgType, |
| 100 | + verbose: disableMsgType, |
| 101 | + }, |
| 102 | + }); |
| 103 | +} |
| 104 | + |
| 105 | +export function defaultColors(): Readonly<ColorSettings> { |
| 106 | + return defaultFormatting.colors; |
| 107 | +} |
| 108 | + |
| 109 | +export function defaultFormat(): Readonly<FormatHelper> { |
| 110 | + return defaultFormatting.format; |
| 111 | +} |
| 112 | + |
| 113 | +function mergePrefixes( |
| 114 | + base: FormattingSettings["prefixes"], |
| 115 | + override?: Partial<FormattingSettings["prefixes"]> |
| 116 | +) { |
| 117 | + return override ? { ...base, ...override } : base; |
| 118 | +} |
| 119 | + |
| 120 | +function mergeInspectOptions( |
| 121 | + base: InspectOptions, |
| 122 | + overrides?: Partial<InspectOptions> |
| 123 | +): InspectOptions { |
| 124 | + return overrides ? { ...base, ...overrides } : base; |
| 125 | +} |
| 126 | + |
| 127 | +function mergeColors( |
| 128 | + base: ColorSettings, |
| 129 | + overrides?: DeepPartial<ColorSettings> |
| 130 | +): ColorSettings { |
| 131 | + if (overrides) { |
| 132 | + const result = { ...base, ...overrides }; |
| 133 | + for (const level of allLogLevels) { |
| 134 | + if (overrides[level]) { |
| 135 | + result[level] = { ...base[level], ...overrides[level] }; |
50 | 136 | }
|
51 | 137 | }
|
52 | 138 | }
|
53 |
| - return target; |
| 139 | + return base; |
54 | 140 | }
|
55 | 141 |
|
56 |
| -const emptyMsgColors = { text: stripVTControlCharacters }; |
57 |
| - |
58 |
| -export const disableColorOptions: DeepPartial<ReporterSettings> = { |
59 |
| - inspectOptions: { colors: false }, |
60 |
| - color: { |
61 |
| - message: { |
62 |
| - default: emptyMsgColors, |
63 |
| - error: emptyMsgColors, |
64 |
| - warn: emptyMsgColors, |
65 |
| - log: emptyMsgColors, |
66 |
| - verbose: emptyMsgColors, |
67 |
| - }, |
68 |
| - }, |
69 |
| -}; |
70 |
| - |
71 |
| -export function createFormatHelper(colorSettings: ColorSettings): FormatHelper { |
72 |
| - const { path: colorPath } = colorSettings; |
| 142 | +export function createFormatHelper(settings: FormattingSettings): FormatHelper { |
| 143 | + const { colors, inspectOptions } = settings; |
73 | 144 | return {
|
74 |
| - packageFull: (pkg: string) => formatPackageName(colorSettings, pkg), |
| 145 | + packageFull: (pkg: string) => formatPackageName(colors, pkg), |
75 | 146 | packageParts: (name: string, scope?: string) =>
|
76 |
| - formatPackageParts(colorSettings, name, scope), |
77 |
| - path: (pathValue: string) => colorPath(pathValue), |
78 |
| - duration: (time: number) => formatDuration(colorSettings, time), |
| 147 | + formatPackageParts(colors, name, scope), |
| 148 | + path: (pathValue: string) => colors.path(pathValue), |
| 149 | + duration: (time: number) => formatDuration(colors, time), |
| 150 | + serialize: (args: unknown[]) => serializeArgs(inspectOptions, args), |
79 | 151 | };
|
80 | 152 | }
|
81 | 153 |
|
@@ -123,3 +195,21 @@ function formatPackageParts(
|
123 | 195 | const { pkgName = noChange, pkgScope = noChange } = colors;
|
124 | 196 | return scope ? `${pkgScope(scope)}/${pkgName(name)}` : pkgName(name);
|
125 | 197 | }
|
| 198 | + |
| 199 | +/** |
| 200 | + * @param inspectOptions options for node:util.inspect, used to format the arguments, same as console.log |
| 201 | + * @param args args list to serialize |
| 202 | + * @returns a single string with arguments joined together with spaces, terminated with a newline |
| 203 | + */ |
| 204 | +export function serializeArgs( |
| 205 | + inspectOptions: InspectOptions, |
| 206 | + args: unknown[] |
| 207 | +): string { |
| 208 | + let msg = args |
| 209 | + .map((arg) => |
| 210 | + typeof arg === "string" ? arg : inspect(arg, inspectOptions) |
| 211 | + ) |
| 212 | + .join(" "); |
| 213 | + msg += "\n"; |
| 214 | + return msg; |
| 215 | +} |
0 commit comments