@sprucelabs/spruce-skill-utils 33.0.28 → 33.2.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.
@@ -1,4 +1,45 @@
1
1
  import chalk from 'chalk';
2
+ export declare class Logger implements Log {
3
+ readonly prefix: string | undefined;
4
+ private readonly baseLog?;
5
+ private readonly useColorsOption?;
6
+ private readonly transports;
7
+ private readonly colors;
8
+ private readonly pre;
9
+ private readonly shouldUseColors;
10
+ private history;
11
+ private historyLimit;
12
+ constructor(prefix?: string | undefined, options?: LogOptions);
13
+ startTrackingHistory(limit: number): void;
14
+ info(...args: LoggableType[]): string;
15
+ warn(...args: LoggableType[]): string;
16
+ error(...args: LoggableType[]): string;
17
+ buildLog(prefix?: string | undefined, options?: LogOptions): Log;
18
+ getHistory(): string[];
19
+ private write;
20
+ private shouldLog;
21
+ private resolveChalk;
22
+ private combinePrefixes;
23
+ private formatArgs;
24
+ private buildPrefixes;
25
+ private reducePrefix;
26
+ private dispatchToTransports;
27
+ private buildMessage;
28
+ private buildPlainMessage;
29
+ private decorateWithTimeDelta;
30
+ private decorateWithTimestamp;
31
+ private emit;
32
+ private getTransports;
33
+ private resolveConsoleMethod;
34
+ private resolveTransport;
35
+ private get env();
36
+ private get logLevel();
37
+ private get shouldWrite();
38
+ private get shouldLogTimeDeltas();
39
+ private get shouldLogTime();
40
+ private formatArg;
41
+ protected isMainModule(): boolean;
42
+ }
2
43
  export default function buildLog(prefix?: string | undefined, options?: LogOptions): Log;
3
44
  export declare const testLog: Log;
4
45
  export declare const stubLog: Log;
@@ -17,6 +58,8 @@ export type LoggableType = Anything | Anything[] | Record<string, Anything>;
17
58
  export type LogTransport = (...messageParts: string[]) => void;
18
59
  type TransportMap = Record<Level, LogTransport | LogTransport[] | null | undefined>;
19
60
  export interface Log {
61
+ startTrackingHistory(limit: number): void;
62
+ getHistory(): string[];
20
63
  readonly prefix: string | undefined;
21
64
  info: (...args: LoggableType[]) => string;
22
65
  error: (...args: LoggableType[]) => string;
@@ -1,7 +1,9 @@
1
1
  import chalk from 'chalk';
2
- class Logger {
2
+ export class Logger {
3
3
  constructor(prefix = undefined, options) {
4
4
  var _a, _b, _c;
5
+ this.history = [];
6
+ this.historyLimit = 0;
5
7
  const { colors = {}, log, useColors, transportsByLevel } = options !== null && options !== void 0 ? options : {};
6
8
  const { info = 'yellow', error = 'red' } = colors;
7
9
  this.prefix = prefix;
@@ -17,6 +19,9 @@ class Logger {
17
19
  const isInteractive = (_c = (_b = (_a = getProcess()) === null || _a === void 0 ? void 0 : _a.stdout) === null || _b === void 0 ? void 0 : _b.isTTY) !== null && _c !== void 0 ? _c : false;
18
20
  this.shouldUseColors = useColors !== false && isInteractive;
19
21
  }
22
+ startTrackingHistory(limit) {
23
+ this.historyLimit = limit;
24
+ }
20
25
  info(...args) {
21
26
  return this.write(this.resolveChalk('green', this.colors.info), args, 'INFO');
22
27
  }
@@ -30,20 +35,39 @@ class Logger {
30
35
  const childPrefix = this.combinePrefixes(prefix);
31
36
  return new Logger(childPrefix, Object.assign({ log: this.baseLog, useColors: this.useColorsOption, transportsByLevel: this.transports }, options));
32
37
  }
38
+ getHistory() {
39
+ return this.history;
40
+ }
33
41
  write(chalkMethod, rawArgs, level) {
34
42
  if (!this.shouldWrite(level)) {
35
43
  return '';
36
44
  }
45
+ const shouldLog = this.shouldLog();
37
46
  const formattedArgs = this.formatArgs(rawArgs);
38
- const { prefix, chalkArgs } = this.buildPrefixes(formattedArgs);
39
- if (this.dispatchToTransports(level, prefix, formattedArgs)) {
40
- return prefix;
47
+ const { prefix, logArgs: logArgs } = this.buildPrefixes(formattedArgs);
48
+ const joined = rawArgs.join(' ');
49
+ const flattened = prefix ? prefix + ' ' + joined : joined;
50
+ if (this.historyLimit > 0) {
51
+ this.history.push(flattened);
52
+ if (this.history.length > this.historyLimit) {
53
+ this.history.shift();
54
+ }
55
+ }
56
+ if (!shouldLog ||
57
+ this.dispatchToTransports(level, prefix, formattedArgs)) {
58
+ return flattened;
41
59
  }
42
60
  const transport = this.resolveTransport(level, this.resolveConsoleMethod(level));
43
- const message = this.buildMessage(chalkMethod, chalkArgs, level, prefix);
61
+ const message = this.buildMessage(chalkMethod, logArgs, level, prefix);
44
62
  this.emit(transport, message, formattedArgs, rawArgs);
45
63
  return message;
46
64
  }
65
+ shouldLog() {
66
+ var _a;
67
+ return (this.isMainModule() ||
68
+ (this.prefix &&
69
+ ((_a = this.env.SPRUCE_LOGS) === null || _a === void 0 ? void 0 : _a.split(',').map((s) => s.trim()).includes(this.prefix))));
70
+ }
47
71
  resolveChalk(base, modifier) {
48
72
  var _a;
49
73
  const baseMethod = chalk === null || chalk === void 0 ? void 0 : chalk[base];
@@ -69,15 +93,15 @@ class Logger {
69
93
  if (!this.pre) {
70
94
  return {
71
95
  prefix: '',
72
- chalkArgs: [...args],
96
+ logArgs: [...args],
73
97
  };
74
98
  }
75
99
  const reducedPrefix = this.reducePrefix(this.pre);
76
100
  const prefixValue = reducedPrefix.length > 0 ? ` ${reducedPrefix}` : '';
77
- const chalkArgs = reducedPrefix.length > 0 ? [reducedPrefix, ...args] : [...args];
101
+ const logArgs = reducedPrefix.length > 0 ? [reducedPrefix, ...args] : [...args];
78
102
  return {
79
- prefix: prefixValue,
80
- chalkArgs,
103
+ prefix: prefixValue.trim(),
104
+ logArgs,
81
105
  };
82
106
  }
83
107
  reducePrefix(prefix) {
@@ -96,15 +120,15 @@ class Logger {
96
120
  if (transports.length === 0) {
97
121
  return false;
98
122
  }
99
- const payload = [prefix.trim(), ...args].filter((part) => part.length > 0);
123
+ const payload = [prefix, ...args].filter((part) => part.length > 0);
100
124
  for (const transport of transports) {
101
125
  transport(...payload);
102
126
  }
103
127
  return true;
104
128
  }
105
- buildMessage(chalkMethod, chalkArgs, level, prefix) {
129
+ buildMessage(chalkMethod, logArgs, level, prefix) {
106
130
  const baseMessage = this.shouldUseColors && chalkMethod
107
- ? chalkMethod(...chalkArgs)
131
+ ? chalkMethod(...logArgs)
108
132
  : this.buildPlainMessage(level, prefix);
109
133
  const withDelta = this.shouldLogTimeDeltas
110
134
  ? this.decorateWithTimeDelta(baseMessage)
@@ -114,7 +138,7 @@ class Logger {
114
138
  : withDelta;
115
139
  }
116
140
  buildPlainMessage(level, prefix) {
117
- return `(${level})${prefix}`;
141
+ return `(${level})${prefix ? ' ' + prefix : ''}`;
118
142
  }
119
143
  decorateWithTimeDelta(message) {
120
144
  const now = Date.now();
@@ -207,6 +231,9 @@ class Logger {
207
231
  }
208
232
  return 'undefined';
209
233
  }
234
+ isMainModule() {
235
+ return true;
236
+ }
210
237
  }
211
238
  export default function buildLog(prefix = undefined, options) {
212
239
  return new Logger(prefix, options);
@@ -1,4 +1,45 @@
1
1
  import chalk from 'chalk';
2
+ export declare class Logger implements Log {
3
+ readonly prefix: string | undefined;
4
+ private readonly baseLog?;
5
+ private readonly useColorsOption?;
6
+ private readonly transports;
7
+ private readonly colors;
8
+ private readonly pre;
9
+ private readonly shouldUseColors;
10
+ private history;
11
+ private historyLimit;
12
+ constructor(prefix?: string | undefined, options?: LogOptions);
13
+ startTrackingHistory(limit: number): void;
14
+ info(...args: LoggableType[]): string;
15
+ warn(...args: LoggableType[]): string;
16
+ error(...args: LoggableType[]): string;
17
+ buildLog(prefix?: string | undefined, options?: LogOptions): Log;
18
+ getHistory(): string[];
19
+ private write;
20
+ private shouldLog;
21
+ private resolveChalk;
22
+ private combinePrefixes;
23
+ private formatArgs;
24
+ private buildPrefixes;
25
+ private reducePrefix;
26
+ private dispatchToTransports;
27
+ private buildMessage;
28
+ private buildPlainMessage;
29
+ private decorateWithTimeDelta;
30
+ private decorateWithTimestamp;
31
+ private emit;
32
+ private getTransports;
33
+ private resolveConsoleMethod;
34
+ private resolveTransport;
35
+ private get env();
36
+ private get logLevel();
37
+ private get shouldWrite();
38
+ private get shouldLogTimeDeltas();
39
+ private get shouldLogTime();
40
+ private formatArg;
41
+ protected isMainModule(): boolean;
42
+ }
2
43
  export default function buildLog(prefix?: string | undefined, options?: LogOptions): Log;
3
44
  export declare const testLog: Log;
4
45
  export declare const stubLog: Log;
@@ -17,6 +58,8 @@ export type LoggableType = Anything | Anything[] | Record<string, Anything>;
17
58
  export type LogTransport = (...messageParts: string[]) => void;
18
59
  type TransportMap = Record<Level, LogTransport | LogTransport[] | null | undefined>;
19
60
  export interface Log {
61
+ startTrackingHistory(limit: number): void;
62
+ getHistory(): string[];
20
63
  readonly prefix: string | undefined;
21
64
  info: (...args: LoggableType[]) => string;
22
65
  error: (...args: LoggableType[]) => string;
@@ -3,11 +3,13 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.stubLog = exports.testLog = void 0;
6
+ exports.stubLog = exports.testLog = exports.Logger = void 0;
7
7
  exports.default = buildLog;
8
8
  const chalk_1 = __importDefault(require("chalk"));
9
9
  class Logger {
10
10
  constructor(prefix = undefined, options) {
11
+ this.history = [];
12
+ this.historyLimit = 0;
11
13
  const { colors = {}, log, useColors, transportsByLevel } = options ?? {};
12
14
  const { info = 'yellow', error = 'red' } = colors;
13
15
  this.prefix = prefix;
@@ -23,6 +25,9 @@ class Logger {
23
25
  const isInteractive = getProcess()?.stdout?.isTTY ?? false;
24
26
  this.shouldUseColors = useColors !== false && isInteractive;
25
27
  }
28
+ startTrackingHistory(limit) {
29
+ this.historyLimit = limit;
30
+ }
26
31
  info(...args) {
27
32
  return this.write(this.resolveChalk('green', this.colors.info), args, 'INFO');
28
33
  }
@@ -41,20 +46,40 @@ class Logger {
41
46
  ...options,
42
47
  });
43
48
  }
49
+ getHistory() {
50
+ return this.history;
51
+ }
44
52
  write(chalkMethod, rawArgs, level) {
45
53
  if (!this.shouldWrite(level)) {
46
54
  return '';
47
55
  }
56
+ const shouldLog = this.shouldLog();
48
57
  const formattedArgs = this.formatArgs(rawArgs);
49
- const { prefix, chalkArgs } = this.buildPrefixes(formattedArgs);
50
- if (this.dispatchToTransports(level, prefix, formattedArgs)) {
51
- return prefix;
58
+ const { prefix, logArgs: logArgs } = this.buildPrefixes(formattedArgs);
59
+ const joined = rawArgs.join(' ');
60
+ const flattened = prefix ? prefix + ' ' + joined : joined;
61
+ if (this.historyLimit > 0) {
62
+ this.history.push(flattened);
63
+ if (this.history.length > this.historyLimit) {
64
+ this.history.shift();
65
+ }
66
+ }
67
+ if (!shouldLog ||
68
+ this.dispatchToTransports(level, prefix, formattedArgs)) {
69
+ return flattened;
52
70
  }
53
71
  const transport = this.resolveTransport(level, this.resolveConsoleMethod(level));
54
- const message = this.buildMessage(chalkMethod, chalkArgs, level, prefix);
72
+ const message = this.buildMessage(chalkMethod, logArgs, level, prefix);
55
73
  this.emit(transport, message, formattedArgs, rawArgs);
56
74
  return message;
57
75
  }
76
+ shouldLog() {
77
+ return (this.isMainModule() ||
78
+ (this.prefix &&
79
+ this.env.SPRUCE_LOGS?.split(',')
80
+ .map((s) => s.trim())
81
+ .includes(this.prefix)));
82
+ }
58
83
  resolveChalk(base, modifier) {
59
84
  const baseMethod = chalk_1.default?.[base];
60
85
  if (!baseMethod) {
@@ -79,15 +104,15 @@ class Logger {
79
104
  if (!this.pre) {
80
105
  return {
81
106
  prefix: '',
82
- chalkArgs: [...args],
107
+ logArgs: [...args],
83
108
  };
84
109
  }
85
110
  const reducedPrefix = this.reducePrefix(this.pre);
86
111
  const prefixValue = reducedPrefix.length > 0 ? ` ${reducedPrefix}` : '';
87
- const chalkArgs = reducedPrefix.length > 0 ? [reducedPrefix, ...args] : [...args];
112
+ const logArgs = reducedPrefix.length > 0 ? [reducedPrefix, ...args] : [...args];
88
113
  return {
89
- prefix: prefixValue,
90
- chalkArgs,
114
+ prefix: prefixValue.trim(),
115
+ logArgs,
91
116
  };
92
117
  }
93
118
  reducePrefix(prefix) {
@@ -106,15 +131,15 @@ class Logger {
106
131
  if (transports.length === 0) {
107
132
  return false;
108
133
  }
109
- const payload = [prefix.trim(), ...args].filter((part) => part.length > 0);
134
+ const payload = [prefix, ...args].filter((part) => part.length > 0);
110
135
  for (const transport of transports) {
111
136
  transport(...payload);
112
137
  }
113
138
  return true;
114
139
  }
115
- buildMessage(chalkMethod, chalkArgs, level, prefix) {
140
+ buildMessage(chalkMethod, logArgs, level, prefix) {
116
141
  const baseMessage = this.shouldUseColors && chalkMethod
117
- ? chalkMethod(...chalkArgs)
142
+ ? chalkMethod(...logArgs)
118
143
  : this.buildPlainMessage(level, prefix);
119
144
  const withDelta = this.shouldLogTimeDeltas
120
145
  ? this.decorateWithTimeDelta(baseMessage)
@@ -124,7 +149,7 @@ class Logger {
124
149
  : withDelta;
125
150
  }
126
151
  buildPlainMessage(level, prefix) {
127
- return `(${level})${prefix}`;
152
+ return `(${level})${prefix ? ' ' + prefix : ''}`;
128
153
  }
129
154
  decorateWithTimeDelta(message) {
130
155
  const now = Date.now();
@@ -213,7 +238,11 @@ class Logger {
213
238
  }
214
239
  return 'undefined';
215
240
  }
241
+ isMainModule() {
242
+ return true;
243
+ }
216
244
  }
245
+ exports.Logger = Logger;
217
246
  function buildLog(prefix = undefined, options) {
218
247
  return new Logger(prefix, options);
219
248
  }
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "publishConfig": {
4
4
  "access": "public"
5
5
  },
6
- "version": "33.0.28",
6
+ "version": "33.2.0",
7
7
  "skill": {
8
8
  "namespace": "skill-utils",
9
9
  "upgradeIgnoreList": [