@likec4/log 1.17.1 → 1.18.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.
package/dist/index.mjs CHANGED
@@ -1,409 +1,5 @@
1
- const LogLevels = {
2
- silent: Number.NEGATIVE_INFINITY,
3
- fatal: 0,
4
- error: 0,
5
- warn: 1,
6
- log: 2,
7
- info: 3,
8
- success: 3,
9
- fail: 3,
10
- ready: 3,
11
- start: 3,
12
- box: 3,
13
- debug: 4,
14
- trace: 5,
15
- verbose: Number.POSITIVE_INFINITY
16
- };
17
- const LogTypes = {
18
- // Silent
19
- silent: {
20
- level: -1
21
- },
22
- // Level 0
23
- fatal: {
24
- level: LogLevels.fatal
25
- },
26
- error: {
27
- level: LogLevels.error
28
- },
29
- // Level 1
30
- warn: {
31
- level: LogLevels.warn
32
- },
33
- // Level 2
34
- log: {
35
- level: LogLevels.log
36
- },
37
- // Level 3
38
- info: {
39
- level: LogLevels.info
40
- },
41
- success: {
42
- level: LogLevels.success
43
- },
44
- fail: {
45
- level: LogLevels.fail
46
- },
47
- ready: {
48
- level: LogLevels.info
49
- },
50
- start: {
51
- level: LogLevels.info
52
- },
53
- box: {
54
- level: LogLevels.info
55
- },
56
- // Level 4
57
- debug: {
58
- level: LogLevels.debug
59
- },
60
- // Level 5
61
- trace: {
62
- level: LogLevels.trace
63
- },
64
- // Verbose
65
- verbose: {
66
- level: LogLevels.verbose
67
- }
68
- };
69
-
70
- function isObject(value) {
71
- return value !== null && typeof value === "object";
72
- }
73
- function _defu(baseObject, defaults, namespace = ".", merger) {
74
- if (!isObject(defaults)) {
75
- return _defu(baseObject, {}, namespace);
76
- }
77
- const object = Object.assign({}, defaults);
78
- for (const key in baseObject) {
79
- if (key === "__proto__" || key === "constructor") {
80
- continue;
81
- }
82
- const value = baseObject[key];
83
- if (value === null || value === void 0) {
84
- continue;
85
- }
86
- if (Array.isArray(value) && Array.isArray(object[key])) {
87
- object[key] = [...value, ...object[key]];
88
- } else if (isObject(value) && isObject(object[key])) {
89
- object[key] = _defu(
90
- value,
91
- object[key],
92
- (namespace ? `${namespace}.` : "") + key.toString());
93
- } else {
94
- object[key] = value;
95
- }
96
- }
97
- return object;
98
- }
99
- function createDefu(merger) {
100
- return (...arguments_) => (
101
- // eslint-disable-next-line unicorn/no-array-reduce
102
- arguments_.reduce((p, c) => _defu(p, c, ""), {})
103
- );
104
- }
105
- const defu = createDefu();
106
-
107
- function isPlainObject(obj) {
108
- return Object.prototype.toString.call(obj) === "[object Object]";
109
- }
110
- function isLogObj(arg) {
111
- if (!isPlainObject(arg)) {
112
- return false;
113
- }
114
- if (!arg.message && !arg.args) {
115
- return false;
116
- }
117
- if (arg.stack) {
118
- return false;
119
- }
120
- return true;
121
- }
122
-
123
- let paused = false;
124
- const queue = [];
125
- class Consola {
126
- constructor(options = {}) {
127
- const types = options.types || LogTypes;
128
- this.options = defu(
129
- {
130
- ...options,
131
- defaults: { ...options.defaults },
132
- level: _normalizeLogLevel(options.level, types),
133
- reporters: [...options.reporters || []]
134
- },
135
- {
136
- types: LogTypes,
137
- throttle: 1e3,
138
- throttleMin: 5,
139
- formatOptions: {
140
- date: true,
141
- colors: false,
142
- compact: true
143
- }
144
- }
145
- );
146
- for (const type in types) {
147
- const defaults = {
148
- type,
149
- ...this.options.defaults,
150
- ...types[type]
151
- };
152
- this[type] = this._wrapLogFn(defaults);
153
- this[type].raw = this._wrapLogFn(
154
- defaults,
155
- true
156
- );
157
- }
158
- if (this.options.mockFn) {
159
- this.mockTypes();
160
- }
161
- this._lastLog = {};
162
- }
163
- get level() {
164
- return this.options.level;
165
- }
166
- set level(level) {
167
- this.options.level = _normalizeLogLevel(
168
- level,
169
- this.options.types,
170
- this.options.level
171
- );
172
- }
173
- prompt(message, opts) {
174
- if (!this.options.prompt) {
175
- throw new Error("prompt is not supported!");
176
- }
177
- return this.options.prompt(message, opts);
178
- }
179
- create(options) {
180
- const instance = new Consola({
181
- ...this.options,
182
- ...options
183
- });
184
- if (this._mockFn) {
185
- instance.mockTypes(this._mockFn);
186
- }
187
- return instance;
188
- }
189
- withDefaults(defaults) {
190
- return this.create({
191
- ...this.options,
192
- defaults: {
193
- ...this.options.defaults,
194
- ...defaults
195
- }
196
- });
197
- }
198
- withTag(tag) {
199
- return this.withDefaults({
200
- tag: this.options.defaults.tag ? this.options.defaults.tag + ":" + tag : tag
201
- });
202
- }
203
- addReporter(reporter) {
204
- this.options.reporters.push(reporter);
205
- return this;
206
- }
207
- removeReporter(reporter) {
208
- if (reporter) {
209
- const i = this.options.reporters.indexOf(reporter);
210
- if (i >= 0) {
211
- return this.options.reporters.splice(i, 1);
212
- }
213
- } else {
214
- this.options.reporters.splice(0);
215
- }
216
- return this;
217
- }
218
- setReporters(reporters) {
219
- this.options.reporters = Array.isArray(reporters) ? reporters : [reporters];
220
- return this;
221
- }
222
- wrapAll() {
223
- this.wrapConsole();
224
- this.wrapStd();
225
- }
226
- restoreAll() {
227
- this.restoreConsole();
228
- this.restoreStd();
229
- }
230
- wrapConsole() {
231
- for (const type in this.options.types) {
232
- if (!console["__" + type]) {
233
- console["__" + type] = console[type];
234
- }
235
- console[type] = this[type].raw;
236
- }
237
- }
238
- restoreConsole() {
239
- for (const type in this.options.types) {
240
- if (console["__" + type]) {
241
- console[type] = console["__" + type];
242
- delete console["__" + type];
243
- }
244
- }
245
- }
246
- wrapStd() {
247
- this._wrapStream(this.options.stdout, "log");
248
- this._wrapStream(this.options.stderr, "log");
249
- }
250
- _wrapStream(stream, type) {
251
- if (!stream) {
252
- return;
253
- }
254
- if (!stream.__write) {
255
- stream.__write = stream.write;
256
- }
257
- stream.write = (data) => {
258
- this[type].raw(String(data).trim());
259
- };
260
- }
261
- restoreStd() {
262
- this._restoreStream(this.options.stdout);
263
- this._restoreStream(this.options.stderr);
264
- }
265
- _restoreStream(stream) {
266
- if (!stream) {
267
- return;
268
- }
269
- if (stream.__write) {
270
- stream.write = stream.__write;
271
- delete stream.__write;
272
- }
273
- }
274
- pauseLogs() {
275
- paused = true;
276
- }
277
- resumeLogs() {
278
- paused = false;
279
- const _queue = queue.splice(0);
280
- for (const item of _queue) {
281
- item[0]._logFn(item[1], item[2]);
282
- }
283
- }
284
- mockTypes(mockFn) {
285
- const _mockFn = mockFn || this.options.mockFn;
286
- this._mockFn = _mockFn;
287
- if (typeof _mockFn !== "function") {
288
- return;
289
- }
290
- for (const type in this.options.types) {
291
- this[type] = _mockFn(type, this.options.types[type]) || this[type];
292
- this[type].raw = this[type];
293
- }
294
- }
295
- _wrapLogFn(defaults, isRaw) {
296
- return (...args) => {
297
- if (paused) {
298
- queue.push([this, defaults, args, isRaw]);
299
- return;
300
- }
301
- return this._logFn(defaults, args, isRaw);
302
- };
303
- }
304
- _logFn(defaults, args, isRaw) {
305
- if ((defaults.level || 0) > this.level) {
306
- return false;
307
- }
308
- const logObj = {
309
- date: /* @__PURE__ */ new Date(),
310
- args: [],
311
- ...defaults,
312
- level: _normalizeLogLevel(defaults.level, this.options.types)
313
- };
314
- if (!isRaw && args.length === 1 && isLogObj(args[0])) {
315
- Object.assign(logObj, args[0]);
316
- } else {
317
- logObj.args = [...args];
318
- }
319
- if (logObj.message) {
320
- logObj.args.unshift(logObj.message);
321
- delete logObj.message;
322
- }
323
- if (logObj.additional) {
324
- if (!Array.isArray(logObj.additional)) {
325
- logObj.additional = logObj.additional.split("\n");
326
- }
327
- logObj.args.push("\n" + logObj.additional.join("\n"));
328
- delete logObj.additional;
329
- }
330
- logObj.type = typeof logObj.type === "string" ? logObj.type.toLowerCase() : "log";
331
- logObj.tag = typeof logObj.tag === "string" ? logObj.tag : "";
332
- const resolveLog = (newLog = false) => {
333
- const repeated = (this._lastLog.count || 0) - this.options.throttleMin;
334
- if (this._lastLog.object && repeated > 0) {
335
- const args2 = [...this._lastLog.object.args];
336
- if (repeated > 1) {
337
- args2.push(`(repeated ${repeated} times)`);
338
- }
339
- this._log({ ...this._lastLog.object, args: args2 });
340
- this._lastLog.count = 1;
341
- }
342
- if (newLog) {
343
- this._lastLog.object = logObj;
344
- this._log(logObj);
345
- }
346
- };
347
- clearTimeout(this._lastLog.timeout);
348
- const diffTime = this._lastLog.time && logObj.date ? logObj.date.getTime() - this._lastLog.time.getTime() : 0;
349
- this._lastLog.time = logObj.date;
350
- if (diffTime < this.options.throttle) {
351
- try {
352
- const serializedLog = JSON.stringify([
353
- logObj.type,
354
- logObj.tag,
355
- logObj.args
356
- ]);
357
- const isSameLog = this._lastLog.serialized === serializedLog;
358
- this._lastLog.serialized = serializedLog;
359
- if (isSameLog) {
360
- this._lastLog.count = (this._lastLog.count || 0) + 1;
361
- if (this._lastLog.count > this.options.throttleMin) {
362
- this._lastLog.timeout = setTimeout(
363
- resolveLog,
364
- this.options.throttle
365
- );
366
- return;
367
- }
368
- }
369
- } catch {
370
- }
371
- }
372
- resolveLog(true);
373
- }
374
- _log(logObj) {
375
- for (const reporter of this.options.reporters) {
376
- reporter.log(logObj, {
377
- options: this.options
378
- });
379
- }
380
- }
381
- }
382
- function _normalizeLogLevel(input, types = {}, defaultLevel = 3) {
383
- if (input === void 0) {
384
- return defaultLevel;
385
- }
386
- if (typeof input === "number") {
387
- return input;
388
- }
389
- if (types[input] && types[input].level !== void 0) {
390
- return types[input].level;
391
- }
392
- return defaultLevel;
393
- }
394
- Consola.prototype.add = Consola.prototype.addReporter;
395
- Consola.prototype.remove = Consola.prototype.removeReporter;
396
- Consola.prototype.clear = Consola.prototype.removeReporter;
397
- Consola.prototype.withScope = Consola.prototype.withTag;
398
- Consola.prototype.mock = Consola.prototype.mockTypes;
399
- Consola.prototype.pause = Consola.prototype.pauseLogs;
400
- Consola.prototype.resume = Consola.prototype.resumeLogs;
401
- function createConsola(options = {}) {
402
- return new Consola(options);
403
- }
404
-
405
- const logger = createConsola({
406
- level: LogLevels.debug
407
- });
408
-
409
- export { LogLevels, logger as consola, logger, logger as rootLogger };
1
+ export { L as LogLevels, a as consola, a as logger, a as rootLogger } from './shared/log.BMY8Anc1.mjs';
2
+ import 'node:util';
3
+ import 'node:path';
4
+ import 'node:process';
5
+ import 'node:tty';
@@ -1318,7 +1318,7 @@ function getBgColor(color = "bgWhite") {
1318
1318
  }
1319
1319
 
1320
1320
  function createConsola(options = {}) {
1321
- let level = _getDefaultLogLevel$1();
1321
+ let level = _getDefaultLogLevel();
1322
1322
  if (process.env.CONSOLA_LEVEL) {
1323
1323
  level = Number.parseInt(process.env.CONSOLA_LEVEL) ?? level;
1324
1324
  }
@@ -1335,7 +1335,7 @@ function createConsola(options = {}) {
1335
1335
  });
1336
1336
  return consola2;
1337
1337
  }
1338
- function _getDefaultLogLevel$1() {
1338
+ function _getDefaultLogLevel() {
1339
1339
  if (isDebug) {
1340
1340
  return LogLevels.debug;
1341
1341
  }
@@ -1346,10 +1346,7 @@ function _getDefaultLogLevel$1() {
1346
1346
  }
1347
1347
  createConsola();
1348
1348
 
1349
- function _getDefaultLogLevel() {
1350
- return LogLevels.debug;
1351
- }
1352
- const level = _getDefaultLogLevel();
1349
+ const level = LogLevels.debug;
1353
1350
  const consola = createConsola({
1354
1351
  level,
1355
1352
  defaults: {
@@ -1299,7 +1299,7 @@ function getBgColor(color = "bgWhite") {
1299
1299
  }
1300
1300
 
1301
1301
  function createConsola(options = {}) {
1302
- let level = _getDefaultLogLevel$1();
1302
+ let level = _getDefaultLogLevel();
1303
1303
  if (process.env.CONSOLA_LEVEL) {
1304
1304
  level = Number.parseInt(process.env.CONSOLA_LEVEL) ?? level;
1305
1305
  }
@@ -1316,7 +1316,7 @@ function createConsola(options = {}) {
1316
1316
  });
1317
1317
  return consola2;
1318
1318
  }
1319
- function _getDefaultLogLevel$1() {
1319
+ function _getDefaultLogLevel() {
1320
1320
  if (isDebug) {
1321
1321
  return LogLevels.debug;
1322
1322
  }
@@ -1327,10 +1327,7 @@ function _getDefaultLogLevel$1() {
1327
1327
  }
1328
1328
  createConsola();
1329
1329
 
1330
- function _getDefaultLogLevel() {
1331
- return LogLevels.debug;
1332
- }
1333
- const level = _getDefaultLogLevel();
1330
+ const level = LogLevels.debug;
1334
1331
  const consola = createConsola({
1335
1332
  level,
1336
1333
  defaults: {
package/package.json CHANGED
@@ -1,12 +1,13 @@
1
1
  {
2
2
  "name": "@likec4/log",
3
3
  "license": "MIT",
4
- "version": "1.17.1",
4
+ "version": "1.18.0",
5
5
  "bugs": "https://github.com/likec4/likec4/issues",
6
6
  "homepage": "https://likec4.dev",
7
7
  "author": "Denis Davydkov <denis@davydkov.com>",
8
8
  "files": [
9
- "dist"
9
+ "dist",
10
+ "lib"
10
11
  ],
11
12
  "repository": {
12
13
  "type": "git",
@@ -14,19 +15,27 @@
14
15
  "directory": "packages/log"
15
16
  },
16
17
  "type": "module",
17
- "sideEffects": false,
18
+ "main": "./dist/index.cjs",
19
+ "module": "./dist/index.mjs",
20
+ "browser": "./dist/browser.mjs",
21
+ "types": "./dist/index.d.ts",
18
22
  "exports": {
19
23
  ".": {
20
24
  "node": {
21
- "types": "./dist/node.d.ts",
22
- "import": "./dist/node.mjs",
23
- "require": "./dist/node.cjs"
24
- },
25
- "default": {
26
25
  "types": "./dist/index.d.ts",
27
26
  "import": "./dist/index.mjs",
28
27
  "require": "./dist/index.cjs"
28
+ },
29
+ "default": {
30
+ "types": "./dist/index.d.ts",
31
+ "import": "./dist/browser.mjs",
32
+ "require": "./dist/browser.cjs"
29
33
  }
34
+ },
35
+ "./browser": {
36
+ "types": "./dist/index.d.ts",
37
+ "import": "./dist/browser.mjs",
38
+ "require": "./dist/browser.cjs"
30
39
  }
31
40
  },
32
41
  "publishConfig": {
@@ -39,7 +48,7 @@
39
48
  "generate": "unbuild"
40
49
  },
41
50
  "devDependencies": {
42
- "@likec4/tsconfig": "1.17.1",
51
+ "@likec4/tsconfig": "1.18.0",
43
52
  "@types/node": "^20.17.7",
44
53
  "consola": "^3.2.3",
45
54
  "std-env": "^3.8.0",
package/dist/node.cjs DELETED
@@ -1,14 +0,0 @@
1
- 'use strict';
2
-
3
- const node = require('./shared/log.CIkEHqaW.cjs');
4
- require('node:util');
5
- require('node:path');
6
- require('node:process');
7
- require('node:tty');
8
-
9
-
10
-
11
- exports.LogLevels = node.LogLevels;
12
- exports.consola = node.consola;
13
- exports.logger = node.consola;
14
- exports.rootLogger = node.consola;
package/dist/node.d.cts DELETED
@@ -1,126 +0,0 @@
1
- type SelectOption = {
2
- label: string;
3
- value: string;
4
- hint?: string;
5
- };
6
- type TextOptions = {
7
- type?: "text";
8
- default?: string;
9
- placeholder?: string;
10
- initial?: string;
11
- };
12
- type ConfirmOptions = {
13
- type: "confirm";
14
- initial?: boolean;
15
- };
16
- type SelectOptions = {
17
- type: "select";
18
- initial?: string;
19
- options: (string | SelectOption)[];
20
- };
21
- type MultiSelectOptions = {
22
- type: "multiselect";
23
- initial?: string;
24
- options: string[] | SelectOption[];
25
- required?: boolean;
26
- };
27
- type PromptOptions = TextOptions | ConfirmOptions | SelectOptions | MultiSelectOptions;
28
- type inferPromptReturnType<T extends PromptOptions> = T extends TextOptions ? string : T extends ConfirmOptions ? boolean : T extends SelectOptions ? T["options"][number] : T extends MultiSelectOptions ? T["options"] : unknown;
29
- declare function prompt<_ = any, __ = any, T extends PromptOptions = TextOptions>(message: string, opts?: PromptOptions): Promise<inferPromptReturnType<T>>;
30
-
31
- type LogLevel = 0 | 1 | 2 | 3 | 4 | 5 | (number & {});
32
- declare const LogLevels: Record<LogType, number>;
33
- type LogType = "silent" | "fatal" | "error" | "warn" | "log" | "info" | "success" | "fail" | "ready" | "start" | "box" | "debug" | "trace" | "verbose";
34
- declare const LogTypes: Record<LogType, Partial<LogObject>>;
35
-
36
- interface ConsolaOptions {
37
- reporters: ConsolaReporter[];
38
- types: Record<LogType, InputLogObject>;
39
- level: LogLevel;
40
- defaults: InputLogObject;
41
- throttle: number;
42
- throttleMin: number;
43
- stdout?: NodeJS.WriteStream;
44
- stderr?: NodeJS.WriteStream;
45
- mockFn?: (type: LogType, defaults: InputLogObject) => (...args: any) => void;
46
- prompt?: typeof prompt | undefined;
47
- formatOptions: FormatOptions;
48
- }
49
- /**
50
- * @see https://nodejs.org/api/util.html#util_util_inspect_object_showhidden_depth_colors
51
- */
52
- interface FormatOptions {
53
- columns?: number;
54
- date?: boolean;
55
- colors?: boolean;
56
- compact?: boolean | number;
57
- [key: string]: unknown;
58
- }
59
- interface InputLogObject {
60
- level?: LogLevel;
61
- tag?: string;
62
- type?: LogType;
63
- message?: string;
64
- additional?: string | string[];
65
- args?: any[];
66
- date?: Date;
67
- }
68
- interface LogObject extends InputLogObject {
69
- level: LogLevel;
70
- type: LogType;
71
- tag: string;
72
- args: any[];
73
- date: Date;
74
- [key: string]: unknown;
75
- }
76
- interface ConsolaReporter {
77
- log: (logObj: LogObject, ctx: {
78
- options: ConsolaOptions;
79
- }) => void;
80
- }
81
-
82
- declare class Consola {
83
- options: ConsolaOptions;
84
- _lastLog: {
85
- serialized?: string;
86
- object?: LogObject;
87
- count?: number;
88
- time?: Date;
89
- timeout?: ReturnType<typeof setTimeout>;
90
- };
91
- _mockFn?: ConsolaOptions["mockFn"];
92
- constructor(options?: Partial<ConsolaOptions>);
93
- get level(): LogLevel;
94
- set level(level: LogLevel);
95
- prompt<T extends PromptOptions>(message: string, opts?: T): Promise<T extends TextOptions ? string : T extends ConfirmOptions ? boolean : T extends SelectOptions ? T["options"][number] : T extends MultiSelectOptions ? T["options"] : unknown>;
96
- create(options: Partial<ConsolaOptions>): ConsolaInstance;
97
- withDefaults(defaults: InputLogObject): ConsolaInstance;
98
- withTag(tag: string): ConsolaInstance;
99
- addReporter(reporter: ConsolaReporter): this;
100
- removeReporter(reporter: ConsolaReporter): ConsolaReporter[] | this;
101
- setReporters(reporters: ConsolaReporter[]): this;
102
- wrapAll(): void;
103
- restoreAll(): void;
104
- wrapConsole(): void;
105
- restoreConsole(): void;
106
- wrapStd(): void;
107
- _wrapStream(stream: NodeJS.WriteStream | undefined, type: LogType): void;
108
- restoreStd(): void;
109
- _restoreStream(stream?: NodeJS.WriteStream): void;
110
- pauseLogs(): void;
111
- resumeLogs(): void;
112
- mockTypes(mockFn?: ConsolaOptions["mockFn"]): void;
113
- _wrapLogFn(defaults: InputLogObject, isRaw?: boolean): (...args: any[]) => false | undefined;
114
- _logFn(defaults: InputLogObject, args: any[], isRaw?: boolean): false | undefined;
115
- _log(logObj: LogObject): void;
116
- }
117
- interface LogFn {
118
- (message: InputLogObject | any, ...args: any[]): void;
119
- raw: (...args: any[]) => void;
120
- }
121
- type ConsolaInstance = Consola & Record<LogType, LogFn>;
122
- declare function createConsola(options?: Partial<ConsolaOptions>): ConsolaInstance;
123
-
124
- declare const consola: ConsolaInstance;
125
-
126
- export { Consola, type ConsolaInstance, type ConsolaOptions, type ConsolaReporter, type FormatOptions, type InputLogObject, type LogLevel, LogLevels, type LogObject, type LogType, LogTypes, consola, createConsola, consola as logger, consola as rootLogger };