Skip to content

Commit d77286a

Browse files
committed
feat: pass formatOptions and other options to reporters
1 parent c4fff18 commit d77286a

File tree

13 files changed

+118
-115
lines changed

13 files changed

+118
-115
lines changed

examples/fancy.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
import { FancyReporter } from "../src/reporters";
22
import { reporterDemo } from "./utils";
33

4-
reporterDemo(new FancyReporter({}));
4+
reporterDemo(new FancyReporter());

examples/utils/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,5 @@ export function reporterDemo(reporter) {
2727
}
2828

2929
export const consola = createConsola({
30-
reporters: [new FancyReporter({})],
30+
reporters: [new FancyReporter()],
3131
});

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
"@vitest/coverage-c8": "^0.29.8",
5151
"changelogen": "^0.5.2",
5252
"colorette": "^2.0.19",
53+
"defu": "^6.1.2",
5354
"eslint": "^8.37.0",
5455
"eslint-config-unjs": "^0.1.0",
5556
"figures": "^5.0.0",

pnpm-lock.yaml

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/consola.ts

Lines changed: 26 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,17 @@
1+
import { defu } from "defu";
12
import { LogTypes, LogType, LogLevel } from "./constants";
23
import { isLogObj } from "./utils/index";
3-
import type { ConsolaReporter, InputLogObject, LogObject } from "./types";
4+
import type {
5+
ConsolaReporter,
6+
InputLogObject,
7+
LogObject,
8+
ConsolaOptions,
9+
} from "./types";
410
import type { PromptOptions } from "./prompt";
511

612
let paused = false;
713
const queue: any[] = [];
814

9-
export interface ConsolaOptions {
10-
reporters: ConsolaReporter[];
11-
types: Record<LogType, InputLogObject>;
12-
level: LogLevel;
13-
defaults: InputLogObject;
14-
throttle: number;
15-
throttleMin: number;
16-
stdout?: NodeJS.WritableStream;
17-
stderr?: NodeJS.WritableStream;
18-
mockFn?: (type: LogType, defaults: InputLogObject) => (...args: any) => void;
19-
prompt?: typeof import("./prompt").prompt | undefined;
20-
}
21-
2215
export class Consola {
2316
options: ConsolaOptions;
2417

@@ -33,18 +26,24 @@ export class Consola {
3326
constructor(options: Partial<ConsolaOptions> = {}) {
3427
// Options
3528
const types = options.types || LogTypes;
36-
this.options = {
37-
// Defaults
38-
throttle: 1000,
39-
throttleMin: 5,
40-
// User
41-
...options,
42-
// Overrides, Normalizations and Clones
43-
defaults: { ...options.defaults },
44-
level: _normalizeLogLevel(options.level, types),
45-
reporters: [...(options.reporters || [])],
46-
types,
47-
};
29+
this.options = defu(
30+
<ConsolaOptions>{
31+
...options,
32+
defaults: { ...options.defaults },
33+
level: _normalizeLogLevel(options.level, types),
34+
reporters: [...(options.reporters || [])],
35+
},
36+
<ConsolaOptions>{
37+
types: LogTypes,
38+
throttle: 1000,
39+
throttleMin: 5,
40+
formatOptions: {
41+
date: true,
42+
colors: false,
43+
compact: true,
44+
},
45+
}
46+
);
4847

4948
// Create logger functions for current instance
5049
for (const type in types) {
@@ -353,8 +352,7 @@ export class Consola {
353352
_log(logObj: LogObject) {
354353
for (const reporter of this.options.reporters) {
355354
reporter.log(logObj, {
356-
stdout: this.stdout,
357-
stderr: this.stderr,
355+
options: this.options,
358356
});
359357
}
360358
}

src/index.browser.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import BrowserReporter from "./reporters/browser";
22
import { createConsola as _createConsola } from "./consola";
3-
import type { ConsolaOptions } from "./consola";
3+
import type { ConsolaOptions } from "./types";
44

55
export * from "./index.shared";
66

src/index.node.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
import { isDebug, isTest, isCI } from "std-env";
22
import { LogLevels, LogLevel } from "./constants";
3-
import type { ConsolaOptions } from "./consola";
3+
import type { ConsolaOptions } from "./types";
44
import { BasicReporter, FancyReporter } from "./reporters";
5-
import { createConsola as _createConsola } from "./consola";
5+
import { ConsolaInstance, createConsola as _createConsola } from "./consola";
66

77
export * from "./index.shared";
88

9-
export function createConsola(options: Partial<ConsolaOptions> = {}) {
9+
export function createConsola(
10+
options: Partial<ConsolaOptions> = {}
11+
): ConsolaInstance {
1012
// Log level
1113
let level = _getDefaultLogLevel();
1214
if (process.env.CONSOLA_LEVEL) {
@@ -17,9 +19,11 @@ export function createConsola(options: Partial<ConsolaOptions> = {}) {
1719
const consola = _createConsola({
1820
level: level as LogLevel,
1921
defaults: { level },
22+
stdout: process.stdout,
23+
stderr: process.stderr,
2024
prompt: (...args) => import("./prompt").then((m) => m.prompt(...args)),
2125
reporters: options.reporters || [
22-
isCI || isTest ? new BasicReporter({}) : new FancyReporter({}),
26+
isCI || isTest ? new BasicReporter() : new FancyReporter(),
2327
],
2428
...options,
2529
});

src/index.shared.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
export { Consola } from "./consola";
21
export { LogLevels, LogTypes } from "./constants";
2+
export { Consola } from "./consola";
33

4-
export type { ConsolaInstance, ConsolaOptions } from "./consola";
5-
export type { LogLevel, LogType } from "./constants";
64
export type * from "./types";
5+
export type { ConsolaInstance } from "./consola";
6+
export type { LogLevel, LogType } from "./constants";

src/reporters/basic.ts

Lines changed: 25 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,43 @@
1-
import util from "node:util";
2-
import { LogObject } from "../types";
1+
import { formatWithOptions } from "node:util";
2+
import type {
3+
LogObject,
4+
ConsolaReporter,
5+
ConsolaOptions,
6+
FormatOptions,
7+
} from "../types";
38
import { parseStack } from "../utils/error";
49
import { writeStream } from "../utils/stream";
510

6-
const DEFAULTS = {
7-
formatOptions: {
8-
date: true,
9-
colors: false,
10-
compact: true,
11-
},
12-
};
13-
1411
const bracket = (x: string) => (x ? `[${x}]` : "");
1512

16-
export default class BasicReporter {
17-
options: typeof DEFAULTS;
18-
19-
constructor(options: Partial<typeof DEFAULTS>) {
20-
this.options = { ...DEFAULTS, ...options };
21-
}
22-
23-
formatStack(stack: string) {
13+
export default class BasicReporter implements ConsolaReporter {
14+
formatStack(stack: string, opts: FormatOptions) {
2415
return " " + parseStack(stack).join("\n ");
2516
}
2617

27-
formatArgs(args: any[]) {
18+
formatArgs(args: any[], opts: FormatOptions) {
2819
const _args = args.map((arg) => {
2920
if (arg && typeof arg.stack === "string") {
30-
return arg.message + "\n" + this.formatStack(arg.stack);
21+
return arg.message + "\n" + this.formatStack(arg.stack, opts);
3122
}
3223
return arg;
3324
});
3425

3526
// Only supported with Node >= 10
3627
// https://nodejs.org/api/util.html#util_util_inspect_object_options
37-
return typeof util.formatWithOptions === "function"
38-
? util.formatWithOptions(this.options.formatOptions, ..._args)
39-
: util.format(..._args);
28+
return formatWithOptions(opts, ..._args);
4029
}
4130

42-
formatDate(date: Date) {
43-
return this.options.formatOptions.date ? date.toLocaleTimeString() : "";
31+
formatDate(date: Date, opts: FormatOptions) {
32+
return opts.date ? date.toLocaleTimeString() : "";
4433
}
4534

4635
filterAndJoin(arr: any[]) {
4736
return arr.filter(Boolean).join(" ");
4837
}
4938

50-
formatLogObj(logObj: LogObject, _opts: any) {
51-
const message = this.formatArgs(logObj.args);
39+
formatLogObj(logObj: LogObject, opts: FormatOptions) {
40+
const message = this.formatArgs(logObj.args, opts);
5241

5342
return this.filterAndJoin([
5443
bracket(logObj.type),
@@ -57,11 +46,17 @@ export default class BasicReporter {
5746
]);
5847
}
5948

60-
log(logObj: LogObject, { stdout, stderr }: any = {}) {
49+
log(logObj: LogObject, ctx: { options: ConsolaOptions }) {
6150
const line = this.formatLogObj(logObj, {
62-
width: stdout.columns || 0,
51+
columns: (ctx.options.stdout as any).columns || 0,
52+
...ctx.options.formatOptions,
6353
});
6454

65-
return writeStream(line + "\n", logObj.level < 2 ? stderr : stdout);
55+
return writeStream(
56+
line + "\n",
57+
logObj.level < 2
58+
? ctx.options.stderr || process.stderr
59+
: ctx.options.stdout || process.stdout
60+
);
6661
}
6762
}

src/reporters/fancy.ts

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { mainSymbols } from "figures";
33
import * as colors from "colorette";
44
import { parseStack } from "../utils/error";
55
import { TYPE_COLOR_MAP, LEVEL_COLOR_MAP } from "../utils/fancy";
6-
import { LogObject } from "../types";
6+
import { FormatOptions, LogObject } from "../types";
77
import BasicReporter from "./basic";
88

99
const DEFAULTS = {
@@ -24,10 +24,6 @@ const TYPE_ICONS = {
2424
};
2525

2626
export default class FancyReporter extends BasicReporter {
27-
constructor(options: Partial<typeof DEFAULTS>) {
28-
super({ ...DEFAULTS, ...options });
29-
}
30-
3127
formatStack(stack: string) {
3228
return (
3329
"\n" +
@@ -43,11 +39,11 @@ export default class FancyReporter extends BasicReporter {
4339
);
4440
}
4541

46-
formatType(logObj: LogObject, isBadge: boolean) {
42+
formatType(logObj: LogObject, isBadge: boolean, opts: FormatOptions) {
4743
const typeColor =
4844
(TYPE_COLOR_MAP as any)[logObj.type] ||
4945
(LEVEL_COLOR_MAP as any)[logObj.level] ||
50-
(this.options as any).secondaryColor;
46+
"gray";
5147

5248
if (isBadge) {
5349
return getBgColor(typeColor)(
@@ -62,20 +58,22 @@ export default class FancyReporter extends BasicReporter {
6258
return _type ? getColor(typeColor)(_type) : "";
6359
}
6460

65-
formatLogObj(logObj: LogObject, opts: { width: number }) {
66-
const [message, ...additional] = this.formatArgs(logObj.args).split("\n");
61+
formatLogObj(logObj: LogObject, opts: FormatOptions) {
62+
const [message, ...additional] = this.formatArgs(logObj.args, opts).split(
63+
"\n"
64+
);
6765

6866
const isBadge =
6967
typeof (logObj as any).badge !== "undefined"
7068
? Boolean((logObj as any).badge)
7169
: logObj.level < 2;
7270

73-
const secondaryColor = getColor((this.options as any).secondaryColor);
71+
const secondaryColor = getColor("gray");
7472

75-
const date = this.formatDate(logObj.date);
73+
const date = this.formatDate(logObj.date, opts);
7674
const coloredDate = date && secondaryColor(date);
7775

78-
const type = this.formatType(logObj, isBadge);
76+
const type = this.formatType(logObj, isBadge, opts);
7977

8078
const tag = logObj.tag ? secondaryColor(logObj.tag) : "";
8179

@@ -86,10 +84,11 @@ export default class FancyReporter extends BasicReporter {
8684
let line;
8785
const left = this.filterAndJoin([type, formattedMessage]);
8886
const right = this.filterAndJoin([tag, coloredDate]);
89-
const space = opts.width - stringWidth(left) - stringWidth(right) - 2;
87+
const space =
88+
(opts.columns || 0) - stringWidth(left) - stringWidth(right) - 2;
9089

9190
line =
92-
space > 0 && opts.width >= 80
91+
space > 0 && (opts.columns || 0) >= 80
9392
? left + " ".repeat(space) + right
9493
: `[ ${right} ] ${left}`;
9594

0 commit comments

Comments
 (0)