@vlandoss/loggy 0.0.7 → 0.0.8-git-9445806.0

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.
@@ -0,0 +1,53 @@
1
+ import { FormatOptions } from "consola";
2
+ import createDebug from "debug";
3
+
4
+ //#region src/types.d.ts
5
+ type AnyLogFn = (...args: unknown[]) => void;
6
+ type Formatters = Record<string, (arg: unknown) => string>;
7
+ type AnyLogger = {
8
+ namespace: string;
9
+ debug: AnyLogFn;
10
+ error: AnyLogFn;
11
+ info: AnyLogFn;
12
+ trace: AnyLogFn;
13
+ warn: AnyLogFn;
14
+ child: (options: CreateOptions) => AnyLogger;
15
+ subdebug: (namespace: string) => AnyLogFn;
16
+ start: AnyLogFn;
17
+ success: AnyLogFn;
18
+ };
19
+ type CreateOptions = {
20
+ namespace: string;
21
+ tag?: string;
22
+ formatOptions?: FormatOptions;
23
+ formatters?: Formatters;
24
+ };
25
+ type LoggerOptions = {
26
+ tag?: string;
27
+ namespace: string;
28
+ formatOptions: FormatOptions;
29
+ formatters: Formatters;
30
+ };
31
+ type LogFnOptions = {
32
+ tag: string;
33
+ message: string;
34
+ };
35
+ //#endregion
36
+ //#region src/loggy.d.ts
37
+ declare class Loggy implements AnyLogger {
38
+ #private;
39
+ constructor(options: LoggerOptions);
40
+ get namespace(): string;
41
+ debug(...args: unknown[]): void;
42
+ error(...args: unknown[]): void;
43
+ info(opts: LogFnOptions | unknown, ...args: unknown[]): void;
44
+ trace(opts: LogFnOptions | unknown, ...args: unknown[]): void;
45
+ warn(opts: LogFnOptions | unknown, ...args: unknown[]): void;
46
+ child(options: CreateOptions): Loggy;
47
+ start(opts: LogFnOptions | unknown, ...args: unknown[]): void;
48
+ success(opts: LogFnOptions | unknown, ...args: unknown[]): void;
49
+ subdebug(namespace: string): createDebug.Debugger;
50
+ }
51
+ declare function createLoggy(options: CreateOptions): Loggy;
52
+ //#endregion
53
+ export { AnyLogFn, AnyLogger, CreateOptions, type FormatOptions, Formatters, LogFnOptions, LoggerOptions, Loggy, createLoggy };
package/dist/index.mjs ADDED
@@ -0,0 +1,111 @@
1
+ import util from "node:util";
2
+ import { createConsola } from "consola";
3
+ import { colors } from "consola/utils";
4
+ import createDebug from "debug";
5
+ //#region src/const.ts
6
+ const DEFAULT_FORMAT_OPTIONS = {
7
+ colors: true,
8
+ depth: 3,
9
+ maxArrayLength: 50,
10
+ breakLength: 80,
11
+ date: false
12
+ };
13
+ const DEFAULT_FORMATTERS = {
14
+ o: (arg) => {
15
+ return util.inspect(arg, DEFAULT_FORMAT_OPTIONS).split("\n").map((str) => str.trim()).join(" ");
16
+ },
17
+ O: (arg) => {
18
+ return util.inspect(arg, DEFAULT_FORMAT_OPTIONS);
19
+ }
20
+ };
21
+ //#endregion
22
+ //#region src/loggy.ts
23
+ function isLogFnOptions(arg) {
24
+ try {
25
+ return typeof arg.message === "string" && typeof arg.tag === "string";
26
+ } catch {
27
+ return false;
28
+ }
29
+ }
30
+ var Loggy = class Loggy {
31
+ #options;
32
+ #debug;
33
+ #consola;
34
+ constructor(options) {
35
+ this.#options = options;
36
+ this.#debug = createDebug(`${options.namespace}:root`);
37
+ this.#consola = createConsola({ formatOptions: options.formatOptions });
38
+ }
39
+ get namespace() {
40
+ return this.#options.namespace;
41
+ }
42
+ debug(...args) {
43
+ this.#debug(...args);
44
+ }
45
+ error(...args) {
46
+ this.#consola.error(this.#format(...args));
47
+ }
48
+ info(opts, ...args) {
49
+ this.#consola.info(this.#format(opts, ...args));
50
+ }
51
+ trace(opts, ...args) {
52
+ this.#consola.trace(this.#format(opts, ...args));
53
+ }
54
+ warn(opts, ...args) {
55
+ this.#consola.warn(this.#format(opts, ...args));
56
+ }
57
+ child(options) {
58
+ return new Loggy({
59
+ ...this.#options,
60
+ ...options,
61
+ namespace: `${this.#options.namespace}:${options.namespace}`
62
+ });
63
+ }
64
+ start(opts, ...args) {
65
+ this.#consola.start(this.#format(opts, ...args));
66
+ }
67
+ success(opts, ...args) {
68
+ this.#consola.success(this.#format(opts, ...args));
69
+ }
70
+ subdebug(namespace) {
71
+ return createDebug(`${this.#options.namespace}:${namespace}`);
72
+ }
73
+ #format(...args) {
74
+ const [firstArg, ...restArgs] = args;
75
+ const tag = isLogFnOptions(firstArg) ? firstArg.tag : this.#options.tag;
76
+ const formattedArgs = isLogFnOptions(firstArg) ? [firstArg.message, ...restArgs] : args;
77
+ if (typeof formattedArgs[0] !== "string") formattedArgs.unshift("%O");
78
+ const [message, ...replacements] = formattedArgs;
79
+ let replacementIndex = -1;
80
+ if (typeof message !== "string") throw new TypeError("message must be a string");
81
+ let formattedMessage = message.replace(/%([a-zA-Z%])/g, (match, formatKey) => {
82
+ if (formatKey === "%") return "%";
83
+ replacementIndex++;
84
+ const formatter = this.#options.formatters[formatKey];
85
+ if (typeof formatter !== "function") return match;
86
+ const replacement = replacements[replacementIndex];
87
+ const formattedMatch = formatter(replacement);
88
+ replacements.splice(replacementIndex, 1);
89
+ replacementIndex--;
90
+ return formattedMatch;
91
+ }).split("\n").join(`\n${" ".repeat(2)}`);
92
+ formattedMessage = !tag ? formattedMessage : `${colors.cyan(`[${tag}]`)} ${formattedMessage}`;
93
+ return util.formatWithOptions(this.#options.formatOptions, formattedMessage, ...replacements);
94
+ }
95
+ };
96
+ function createLoggy(options) {
97
+ return new Loggy({
98
+ namespace: options.namespace,
99
+ tag: options.tag,
100
+ formatOptions: {
101
+ ...DEFAULT_FORMAT_OPTIONS,
102
+ ...options.formatOptions
103
+ },
104
+ formatters: {
105
+ ...DEFAULT_FORMATTERS,
106
+ ...options.formatters
107
+ }
108
+ });
109
+ }
110
+ //#endregion
111
+ export { Loggy, createLoggy };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vlandoss/loggy",
3
- "version": "0.0.7",
3
+ "version": "0.0.8-git-9445806.0",
4
4
  "description": "Console wrapper to make logging fun again",
5
5
  "homepage": "https://github.com/variableland/dx/tree/main/packages/loggy#readme",
6
6
  "bugs": {
@@ -8,22 +8,29 @@
8
8
  },
9
9
  "repository": {
10
10
  "type": "git",
11
- "url": "git+https://github.com/variableland/dx.git"
11
+ "url": "git+https://github.com/variableland/dx.git",
12
+ "directory": "packages/loggy"
12
13
  },
13
14
  "license": "MIT",
14
15
  "author": "rcrd <rcrd@variable.land>",
15
16
  "type": "module",
16
17
  "exports": {
17
- ".": "./src/index.ts"
18
+ ".": {
19
+ "types": "./dist/index.d.mts",
20
+ "default": "./dist/index.mjs"
21
+ }
18
22
  },
19
23
  "files": [
20
24
  "dist",
21
- "src"
25
+ "src",
26
+ "!src/**/__tests__",
27
+ "!src/**/*.test.*"
22
28
  ],
23
29
  "publishConfig": {
24
30
  "access": "public"
25
31
  },
26
32
  "engines": {
33
+ "node": ">=20.0.0",
27
34
  "bun": ">=1.0.0"
28
35
  },
29
36
  "dependencies": {
@@ -31,9 +38,11 @@
31
38
  "debug": "4.4.3"
32
39
  },
33
40
  "devDependencies": {
34
- "@types/debug": "4.1.12"
41
+ "@types/debug": "4.1.12",
42
+ "@vlandoss/tsdown-config": "^0.0.2-git-9445806.0"
35
43
  },
36
44
  "scripts": {
45
+ "build": "tsdown",
37
46
  "test:types": "rr tsc"
38
47
  }
39
48
  }