@sprucelabs/spruce-skill-utils 32.0.117 → 32.0.119
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,7 @@
|
|
|
1
1
|
import chalk from 'chalk';
|
|
2
|
+
export default function buildLog(prefix?: string | undefined, options?: LogOptions): Log;
|
|
3
|
+
export declare const testLog: Log;
|
|
4
|
+
export declare const stubLog: Log;
|
|
2
5
|
export interface LogOptions {
|
|
3
6
|
log?: (...args: any[]) => void;
|
|
4
7
|
useColors?: boolean;
|
|
@@ -9,10 +12,10 @@ export interface LogOptions {
|
|
|
9
12
|
};
|
|
10
13
|
}
|
|
11
14
|
export type Level = 'ERROR' | 'INFO' | 'WARN';
|
|
12
|
-
export type LogTransport = (...messageParts: string[]) => void;
|
|
13
|
-
type TransportMap = Record<Level, LogTransport | LogTransport[] | null | undefined>;
|
|
14
15
|
type Anything = string | number | boolean | null | undefined | Error | unknown;
|
|
15
16
|
export type LoggableType = Anything | Anything[] | Record<string, Anything>;
|
|
17
|
+
export type LogTransport = (...messageParts: string[]) => void;
|
|
18
|
+
type TransportMap = Record<Level, LogTransport | LogTransport[] | null | undefined>;
|
|
16
19
|
export interface Log {
|
|
17
20
|
readonly prefix: string | undefined;
|
|
18
21
|
info: (...args: LoggableType[]) => string;
|
|
@@ -21,7 +24,4 @@ export interface Log {
|
|
|
21
24
|
buildLog(prefix: string | undefined, options?: LogOptions): Log;
|
|
22
25
|
}
|
|
23
26
|
type Color = keyof typeof chalk;
|
|
24
|
-
export default function buildLog(prefix?: string | undefined, options?: LogOptions): Log;
|
|
25
|
-
export declare const testLog: Log;
|
|
26
|
-
export declare const stubLog: Log;
|
|
27
27
|
export {};
|
|
@@ -1,128 +1,216 @@
|
|
|
1
1
|
import chalk from 'chalk';
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
2
|
+
class Logger {
|
|
3
|
+
constructor(prefix = undefined, options) {
|
|
4
|
+
var _a, _b, _c;
|
|
5
|
+
const { colors = {}, log, useColors, transportsByLevel } = options !== null && options !== void 0 ? options : {};
|
|
6
|
+
const { info = 'yellow', error = 'red' } = colors;
|
|
7
|
+
this.prefix = prefix;
|
|
8
|
+
this.baseLog = log;
|
|
9
|
+
this.useColorsOption = useColors;
|
|
10
|
+
this.transports = {
|
|
11
|
+
ERROR: transportsByLevel === null || transportsByLevel === void 0 ? void 0 : transportsByLevel.ERROR,
|
|
12
|
+
INFO: transportsByLevel === null || transportsByLevel === void 0 ? void 0 : transportsByLevel.INFO,
|
|
13
|
+
WARN: transportsByLevel === null || transportsByLevel === void 0 ? void 0 : transportsByLevel.WARN,
|
|
14
|
+
};
|
|
15
|
+
this.colors = { info, error };
|
|
16
|
+
this.pre = prefix ? `${prefix} ::` : undefined;
|
|
17
|
+
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
|
+
this.shouldUseColors = useColors !== false && isInteractive;
|
|
5
19
|
}
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
const transports = {
|
|
23
|
-
ERROR: (_d = options === null || options === void 0 ? void 0 : options.transportsByLevel) === null || _d === void 0 ? void 0 : _d.ERROR,
|
|
24
|
-
INFO: (_e = options === null || options === void 0 ? void 0 : options.transportsByLevel) === null || _e === void 0 ? void 0 : _e.INFO,
|
|
25
|
-
WARN: (_f = options === null || options === void 0 ? void 0 : options.transportsByLevel) === null || _f === void 0 ? void 0 : _f.WARN,
|
|
26
|
-
};
|
|
27
|
-
const logUtil = {
|
|
28
|
-
prefix,
|
|
29
|
-
info(...args) {
|
|
30
|
-
var _a;
|
|
31
|
-
//@ts-ignore
|
|
32
|
-
return write((_a = chalk === null || chalk === void 0 ? void 0 : chalk.green) === null || _a === void 0 ? void 0 : _a[info], args, 'INFO');
|
|
33
|
-
},
|
|
34
|
-
warn(...args) {
|
|
35
|
-
var _a;
|
|
36
|
-
//@ts-ignore
|
|
37
|
-
return write((_a = chalk === null || chalk === void 0 ? void 0 : chalk.yellow) === null || _a === void 0 ? void 0 : _a[info], args, 'WARN');
|
|
38
|
-
},
|
|
39
|
-
error(...args) {
|
|
40
|
-
var _a;
|
|
41
|
-
//@ts-ignore
|
|
42
|
-
return write((_a = chalk === null || chalk === void 0 ? void 0 : chalk.red) === null || _a === void 0 ? void 0 : _a[error], args, 'ERROR');
|
|
43
|
-
},
|
|
44
|
-
buildLog(prefix = undefined, options) {
|
|
45
|
-
return buildLog(`${pre ? `${pre} ` : ''}${prefix}`, Object.assign({ log,
|
|
46
|
-
useColors, transportsByLevel: transports }, options));
|
|
47
|
-
},
|
|
48
|
-
};
|
|
49
|
-
return logUtil;
|
|
50
|
-
function getTransports(level) {
|
|
51
|
-
const t = transports[level];
|
|
52
|
-
if (!t) {
|
|
53
|
-
return [];
|
|
20
|
+
info(...args) {
|
|
21
|
+
return this.write(this.resolveChalk('green', this.colors.info), args, 'INFO');
|
|
22
|
+
}
|
|
23
|
+
warn(...args) {
|
|
24
|
+
return this.write(this.resolveChalk('yellow', this.colors.info), args, 'WARN');
|
|
25
|
+
}
|
|
26
|
+
error(...args) {
|
|
27
|
+
return this.write(this.resolveChalk('red', this.colors.error), args, 'ERROR');
|
|
28
|
+
}
|
|
29
|
+
buildLog(prefix = undefined, options) {
|
|
30
|
+
const childPrefix = this.combinePrefixes(prefix);
|
|
31
|
+
return new Logger(childPrefix, Object.assign({ log: this.baseLog, useColors: this.useColorsOption, transportsByLevel: this.transports }, options));
|
|
32
|
+
}
|
|
33
|
+
write(chalkMethod, rawArgs, level) {
|
|
34
|
+
if (!this.shouldWrite(level)) {
|
|
35
|
+
return '';
|
|
54
36
|
}
|
|
55
|
-
|
|
56
|
-
|
|
37
|
+
const formattedArgs = this.formatArgs(rawArgs);
|
|
38
|
+
const { prefix, chalkArgs } = this.buildPrefixes(formattedArgs);
|
|
39
|
+
if (this.dispatchToTransports(level, prefix, formattedArgs)) {
|
|
40
|
+
return prefix;
|
|
57
41
|
}
|
|
58
|
-
|
|
42
|
+
const transport = this.resolveTransport(level, this.resolveConsoleMethod(level));
|
|
43
|
+
const message = this.buildMessage(chalkMethod, chalkArgs, level, prefix);
|
|
44
|
+
this.emit(transport, message, formattedArgs, rawArgs);
|
|
45
|
+
return message;
|
|
59
46
|
}
|
|
60
|
-
|
|
61
|
-
var _a
|
|
62
|
-
|
|
63
|
-
|
|
47
|
+
resolveChalk(base, modifier) {
|
|
48
|
+
var _a;
|
|
49
|
+
const baseMethod = chalk === null || chalk === void 0 ? void 0 : chalk[base];
|
|
50
|
+
if (!baseMethod) {
|
|
51
|
+
return undefined;
|
|
64
52
|
}
|
|
65
|
-
const
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
const parts = pre.split(' :: ');
|
|
72
|
-
builtPrefix = parts
|
|
73
|
-
.splice(parts.length - length, length)
|
|
74
|
-
.join(' :: ');
|
|
75
|
-
}
|
|
76
|
-
chalkArgs = [builtPrefix, ...chalkArgs];
|
|
53
|
+
const styled = baseMethod === null || baseMethod === void 0 ? void 0 : baseMethod[modifier];
|
|
54
|
+
return (_a = styled) !== null && _a !== void 0 ? _a : baseMethod;
|
|
55
|
+
}
|
|
56
|
+
combinePrefixes(next) {
|
|
57
|
+
if (next === undefined) {
|
|
58
|
+
return this.prefix;
|
|
77
59
|
}
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
60
|
+
if (!this.pre) {
|
|
61
|
+
return next;
|
|
62
|
+
}
|
|
63
|
+
return `${this.pre} ${next}`;
|
|
64
|
+
}
|
|
65
|
+
formatArgs(rawArgs) {
|
|
66
|
+
return rawArgs.map((arg) => this.formatArg(arg));
|
|
67
|
+
}
|
|
68
|
+
buildPrefixes(args) {
|
|
69
|
+
if (!this.pre) {
|
|
70
|
+
return {
|
|
71
|
+
prefix: '',
|
|
72
|
+
chalkArgs: [...args],
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
const reducedPrefix = this.reducePrefix(this.pre);
|
|
76
|
+
const prefixValue = reducedPrefix.length > 0 ? ` ${reducedPrefix}` : '';
|
|
77
|
+
const chalkArgs = reducedPrefix.length > 0 ? [reducedPrefix, ...args] : [...args];
|
|
78
|
+
return {
|
|
79
|
+
prefix: prefixValue,
|
|
80
|
+
chalkArgs,
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
reducePrefix(prefix) {
|
|
84
|
+
const length = getMaxLogPrefixesLength();
|
|
85
|
+
if (typeof length === 'number' && !Number.isNaN(length)) {
|
|
86
|
+
if (length <= 0) {
|
|
87
|
+
return '';
|
|
83
88
|
}
|
|
84
|
-
|
|
89
|
+
const parts = prefix.split(' :: ');
|
|
90
|
+
return parts.slice(-length).join(' :: ');
|
|
91
|
+
}
|
|
92
|
+
return prefix;
|
|
93
|
+
}
|
|
94
|
+
dispatchToTransports(level, prefix, args) {
|
|
95
|
+
const transports = this.getTransports(level);
|
|
96
|
+
if (transports.length === 0) {
|
|
97
|
+
return false;
|
|
98
|
+
}
|
|
99
|
+
const payload = [prefix.trim(), ...args].filter((part) => part.length > 0);
|
|
100
|
+
for (const transport of transports) {
|
|
101
|
+
transport(...payload);
|
|
85
102
|
}
|
|
86
|
-
|
|
87
|
-
|
|
103
|
+
return true;
|
|
104
|
+
}
|
|
105
|
+
buildMessage(chalkMethod, chalkArgs, level, prefix) {
|
|
106
|
+
const baseMessage = this.shouldUseColors && chalkMethod
|
|
107
|
+
? chalkMethod(...chalkArgs)
|
|
108
|
+
: this.buildPlainMessage(level, prefix);
|
|
109
|
+
const withDelta = this.shouldLogTimeDeltas
|
|
110
|
+
? this.decorateWithTimeDelta(baseMessage)
|
|
111
|
+
: baseMessage;
|
|
112
|
+
return this.shouldLogTime
|
|
113
|
+
? this.decorateWithTimestamp(withDelta)
|
|
114
|
+
: withDelta;
|
|
115
|
+
}
|
|
116
|
+
buildPlainMessage(level, prefix) {
|
|
117
|
+
return `(${level})${prefix}`;
|
|
118
|
+
}
|
|
119
|
+
decorateWithTimeDelta(message) {
|
|
120
|
+
const now = Date.now();
|
|
121
|
+
const diff = now - lastLogTimeMs;
|
|
122
|
+
lastLogTimeMs = now;
|
|
123
|
+
return `(${diff}ms) ${message}`;
|
|
124
|
+
}
|
|
125
|
+
decorateWithTimestamp(message) {
|
|
126
|
+
return `(${new Date().toISOString()}) ${message}`;
|
|
127
|
+
}
|
|
128
|
+
emit(transport, message, formattedArgs, rawArgs) {
|
|
129
|
+
if (transport.isConsole) {
|
|
130
|
+
transport.fn(message, ...rawArgs);
|
|
131
|
+
return;
|
|
132
|
+
}
|
|
133
|
+
if (this.shouldUseColors === false) {
|
|
134
|
+
transport.fn(message, ...formattedArgs);
|
|
135
|
+
return;
|
|
136
|
+
}
|
|
137
|
+
transport.fn(message);
|
|
138
|
+
}
|
|
139
|
+
getTransports(level) {
|
|
140
|
+
const transport = this.transports[level];
|
|
141
|
+
if (!transport) {
|
|
142
|
+
return [];
|
|
143
|
+
}
|
|
144
|
+
return Array.isArray(transport) ? transport : [transport];
|
|
145
|
+
}
|
|
146
|
+
resolveConsoleMethod(level) {
|
|
88
147
|
switch (level) {
|
|
89
148
|
case 'ERROR':
|
|
90
|
-
|
|
91
|
-
break;
|
|
149
|
+
return 'error';
|
|
92
150
|
case 'WARN':
|
|
93
|
-
|
|
94
|
-
break;
|
|
151
|
+
return 'warn';
|
|
95
152
|
default:
|
|
96
|
-
|
|
97
|
-
break;
|
|
153
|
+
return 'log';
|
|
98
154
|
}
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
const diff = now - lastLogTimeMs;
|
|
111
|
-
lastLogTimeMs = now;
|
|
112
|
-
message = `(${diff}ms) ${message}`;
|
|
113
|
-
}
|
|
114
|
-
if (env.SHOULD_LOG_TIME !== 'false') {
|
|
115
|
-
message = `(${new Date().toISOString()}) ${message}`;
|
|
155
|
+
}
|
|
156
|
+
resolveTransport(level, logMethod) {
|
|
157
|
+
var _a, _b, _c;
|
|
158
|
+
if (this.baseLog) {
|
|
159
|
+
const logFn = this.baseLog;
|
|
160
|
+
return {
|
|
161
|
+
fn: (...parts) => {
|
|
162
|
+
logFn(...parts);
|
|
163
|
+
},
|
|
164
|
+
isConsole: false,
|
|
165
|
+
};
|
|
116
166
|
}
|
|
117
|
-
if (
|
|
118
|
-
|
|
167
|
+
if (level === 'ERROR' && ((_b = (_a = getProcess()) === null || _a === void 0 ? void 0 : _a.stderr) === null || _b === void 0 ? void 0 : _b.write)) {
|
|
168
|
+
return {
|
|
169
|
+
fn: (...parts) => {
|
|
170
|
+
var _a, _b, _c;
|
|
171
|
+
const stringParts = parts.map((part) => typeof part === 'string' ? part : this.formatArg(part));
|
|
172
|
+
(_c = (_b = (_a = getProcess()) === null || _a === void 0 ? void 0 : _a.stderr) === null || _b === void 0 ? void 0 : _b.write) === null || _c === void 0 ? void 0 : _c.call(_b, stringParts.join(' ') + '\n');
|
|
173
|
+
},
|
|
174
|
+
isConsole: false,
|
|
175
|
+
};
|
|
119
176
|
}
|
|
120
|
-
|
|
121
|
-
|
|
177
|
+
const consoleMethod = ((_c = console[logMethod]) !== null && _c !== void 0 ? _c : console.log).bind(console);
|
|
178
|
+
return {
|
|
179
|
+
fn: (...parts) => {
|
|
180
|
+
consoleMethod(...parts);
|
|
181
|
+
},
|
|
182
|
+
isConsole: true,
|
|
183
|
+
};
|
|
184
|
+
}
|
|
185
|
+
get env() {
|
|
186
|
+
var _a, _b;
|
|
187
|
+
return (_b = (_a = getProcess()) === null || _a === void 0 ? void 0 : _a.env) !== null && _b !== void 0 ? _b : {};
|
|
188
|
+
}
|
|
189
|
+
get logLevel() {
|
|
190
|
+
var _a;
|
|
191
|
+
return (_a = this.env.LOG_LEVEL) !== null && _a !== void 0 ? _a : undefined;
|
|
192
|
+
}
|
|
193
|
+
get shouldWrite() {
|
|
194
|
+
const levelSetting = this.logLevel;
|
|
195
|
+
return (level) => shouldWrite(levelSetting, level);
|
|
196
|
+
}
|
|
197
|
+
get shouldLogTimeDeltas() {
|
|
198
|
+
return this.env.SHOULD_LOG_TIME_DELTAS !== 'false';
|
|
199
|
+
}
|
|
200
|
+
get shouldLogTime() {
|
|
201
|
+
return this.env.SHOULD_LOG_TIME !== 'false';
|
|
202
|
+
}
|
|
203
|
+
formatArg(value) {
|
|
204
|
+
const formatter = value === null || value === void 0 ? void 0 : value.toString;
|
|
205
|
+
if (typeof formatter === 'function') {
|
|
206
|
+
return formatter.call(value);
|
|
122
207
|
}
|
|
123
|
-
return
|
|
208
|
+
return 'undefined';
|
|
124
209
|
}
|
|
125
210
|
}
|
|
211
|
+
export default function buildLog(prefix = undefined, options) {
|
|
212
|
+
return new Logger(prefix, options);
|
|
213
|
+
}
|
|
126
214
|
export const testLog = buildLog('TEST', {
|
|
127
215
|
log: (...parts) => {
|
|
128
216
|
var _a, _b, _c;
|
|
@@ -133,18 +221,28 @@ export const stubLog = buildLog('STUB', {
|
|
|
133
221
|
log: () => { },
|
|
134
222
|
useColors: false,
|
|
135
223
|
});
|
|
136
|
-
function shouldWrite(
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
if (((_b = (_a = getProcess()) === null || _a === void 0 ? void 0 : _a.env) === null || _b === void 0 ? void 0 : _b.LOG_LEVEL) == 'none') {
|
|
140
|
-
return false;
|
|
141
|
-
}
|
|
142
|
-
if (((_d = (_c = getProcess()) === null || _c === void 0 ? void 0 : _c.env) === null || _d === void 0 ? void 0 : _d.LOG_LEVEL) == 'error' && level !== 'ERROR') {
|
|
143
|
-
return false;
|
|
144
|
-
}
|
|
145
|
-
if (((_f = (_e = getProcess()) === null || _e === void 0 ? void 0 : _e.env) === null || _f === void 0 ? void 0 : _f.LOG_LEVEL) == 'warn' && level === 'INFO') {
|
|
224
|
+
function shouldWrite(logLevel, level) {
|
|
225
|
+
switch (logLevel === null || logLevel === void 0 ? void 0 : logLevel.toLowerCase()) {
|
|
226
|
+
case 'none':
|
|
146
227
|
return false;
|
|
147
|
-
|
|
228
|
+
case 'error':
|
|
229
|
+
return level === 'ERROR';
|
|
230
|
+
case 'warn':
|
|
231
|
+
return level !== 'INFO';
|
|
232
|
+
default:
|
|
233
|
+
return true;
|
|
148
234
|
}
|
|
149
|
-
return true;
|
|
150
235
|
}
|
|
236
|
+
function getProcess() {
|
|
237
|
+
if (typeof process !== 'undefined') {
|
|
238
|
+
return process;
|
|
239
|
+
}
|
|
240
|
+
return null;
|
|
241
|
+
}
|
|
242
|
+
let lastLogTimeMs = Date.now();
|
|
243
|
+
const getMaxLogPrefixesLength = () => {
|
|
244
|
+
var _a, _b, _c, _d;
|
|
245
|
+
return typeof ((_b = (_a = getProcess()) === null || _a === void 0 ? void 0 : _a.env) === null || _b === void 0 ? void 0 : _b.MAXIMUM_LOG_PREFIXES_LENGTH) === 'string'
|
|
246
|
+
? +((_d = (_c = getProcess()) === null || _c === void 0 ? void 0 : _c.env) === null || _d === void 0 ? void 0 : _d.MAXIMUM_LOG_PREFIXES_LENGTH)
|
|
247
|
+
: undefined;
|
|
248
|
+
};
|
|
@@ -1,4 +1,7 @@
|
|
|
1
1
|
import chalk from 'chalk';
|
|
2
|
+
export default function buildLog(prefix?: string | undefined, options?: LogOptions): Log;
|
|
3
|
+
export declare const testLog: Log;
|
|
4
|
+
export declare const stubLog: Log;
|
|
2
5
|
export interface LogOptions {
|
|
3
6
|
log?: (...args: any[]) => void;
|
|
4
7
|
useColors?: boolean;
|
|
@@ -9,10 +12,10 @@ export interface LogOptions {
|
|
|
9
12
|
};
|
|
10
13
|
}
|
|
11
14
|
export type Level = 'ERROR' | 'INFO' | 'WARN';
|
|
12
|
-
export type LogTransport = (...messageParts: string[]) => void;
|
|
13
|
-
type TransportMap = Record<Level, LogTransport | LogTransport[] | null | undefined>;
|
|
14
15
|
type Anything = string | number | boolean | null | undefined | Error | unknown;
|
|
15
16
|
export type LoggableType = Anything | Anything[] | Record<string, Anything>;
|
|
17
|
+
export type LogTransport = (...messageParts: string[]) => void;
|
|
18
|
+
type TransportMap = Record<Level, LogTransport | LogTransport[] | null | undefined>;
|
|
16
19
|
export interface Log {
|
|
17
20
|
readonly prefix: string | undefined;
|
|
18
21
|
info: (...args: LoggableType[]) => string;
|
|
@@ -21,7 +24,4 @@ export interface Log {
|
|
|
21
24
|
buildLog(prefix: string | undefined, options?: LogOptions): Log;
|
|
22
25
|
}
|
|
23
26
|
type Color = keyof typeof chalk;
|
|
24
|
-
export default function buildLog(prefix?: string | undefined, options?: LogOptions): Log;
|
|
25
|
-
export declare const testLog: Log;
|
|
26
|
-
export declare const stubLog: Log;
|
|
27
27
|
export {};
|
|
@@ -6,128 +6,217 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
6
6
|
exports.stubLog = exports.testLog = void 0;
|
|
7
7
|
exports.default = buildLog;
|
|
8
8
|
const chalk_1 = __importDefault(require("chalk"));
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
9
|
+
class Logger {
|
|
10
|
+
constructor(prefix = undefined, options) {
|
|
11
|
+
const { colors = {}, log, useColors, transportsByLevel } = options ?? {};
|
|
12
|
+
const { info = 'yellow', error = 'red' } = colors;
|
|
13
|
+
this.prefix = prefix;
|
|
14
|
+
this.baseLog = log;
|
|
15
|
+
this.useColorsOption = useColors;
|
|
16
|
+
this.transports = {
|
|
17
|
+
ERROR: transportsByLevel?.ERROR,
|
|
18
|
+
INFO: transportsByLevel?.INFO,
|
|
19
|
+
WARN: transportsByLevel?.WARN,
|
|
20
|
+
};
|
|
21
|
+
this.colors = { info, error };
|
|
22
|
+
this.pre = prefix ? `${prefix} ::` : undefined;
|
|
23
|
+
const isInteractive = getProcess()?.stdout?.isTTY ?? false;
|
|
24
|
+
this.shouldUseColors = useColors !== false && isInteractive;
|
|
12
25
|
}
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
info(...args) {
|
|
35
|
-
//@ts-ignore
|
|
36
|
-
return write(chalk_1.default?.green?.[info], args, 'INFO');
|
|
37
|
-
},
|
|
38
|
-
warn(...args) {
|
|
39
|
-
//@ts-ignore
|
|
40
|
-
return write(chalk_1.default?.yellow?.[info], args, 'WARN');
|
|
41
|
-
},
|
|
42
|
-
error(...args) {
|
|
43
|
-
//@ts-ignore
|
|
44
|
-
return write(chalk_1.default?.red?.[error], args, 'ERROR');
|
|
45
|
-
},
|
|
46
|
-
buildLog(prefix = undefined, options) {
|
|
47
|
-
return buildLog(`${pre ? `${pre} ` : ''}${prefix}`, {
|
|
48
|
-
log,
|
|
49
|
-
useColors,
|
|
50
|
-
transportsByLevel: transports,
|
|
51
|
-
...options,
|
|
52
|
-
});
|
|
53
|
-
},
|
|
54
|
-
};
|
|
55
|
-
return logUtil;
|
|
56
|
-
function getTransports(level) {
|
|
57
|
-
const t = transports[level];
|
|
58
|
-
if (!t) {
|
|
59
|
-
return [];
|
|
26
|
+
info(...args) {
|
|
27
|
+
return this.write(this.resolveChalk('green', this.colors.info), args, 'INFO');
|
|
28
|
+
}
|
|
29
|
+
warn(...args) {
|
|
30
|
+
return this.write(this.resolveChalk('yellow', this.colors.info), args, 'WARN');
|
|
31
|
+
}
|
|
32
|
+
error(...args) {
|
|
33
|
+
return this.write(this.resolveChalk('red', this.colors.error), args, 'ERROR');
|
|
34
|
+
}
|
|
35
|
+
buildLog(prefix = undefined, options) {
|
|
36
|
+
const childPrefix = this.combinePrefixes(prefix);
|
|
37
|
+
return new Logger(childPrefix, {
|
|
38
|
+
log: this.baseLog,
|
|
39
|
+
useColors: this.useColorsOption,
|
|
40
|
+
transportsByLevel: this.transports,
|
|
41
|
+
...options,
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
write(chalkMethod, rawArgs, level) {
|
|
45
|
+
if (!this.shouldWrite(level)) {
|
|
46
|
+
return '';
|
|
60
47
|
}
|
|
61
|
-
|
|
62
|
-
|
|
48
|
+
const formattedArgs = this.formatArgs(rawArgs);
|
|
49
|
+
const { prefix, chalkArgs } = this.buildPrefixes(formattedArgs);
|
|
50
|
+
if (this.dispatchToTransports(level, prefix, formattedArgs)) {
|
|
51
|
+
return prefix;
|
|
63
52
|
}
|
|
64
|
-
|
|
53
|
+
const transport = this.resolveTransport(level, this.resolveConsoleMethod(level));
|
|
54
|
+
const message = this.buildMessage(chalkMethod, chalkArgs, level, prefix);
|
|
55
|
+
this.emit(transport, message, formattedArgs, rawArgs);
|
|
56
|
+
return message;
|
|
65
57
|
}
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
58
|
+
resolveChalk(base, modifier) {
|
|
59
|
+
const baseMethod = chalk_1.default?.[base];
|
|
60
|
+
if (!baseMethod) {
|
|
61
|
+
return undefined;
|
|
69
62
|
}
|
|
70
|
-
const
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
.join(' :: ');
|
|
80
|
-
}
|
|
81
|
-
chalkArgs = [builtPrefix, ...chalkArgs];
|
|
63
|
+
const styled = baseMethod?.[modifier];
|
|
64
|
+
return styled ?? baseMethod;
|
|
65
|
+
}
|
|
66
|
+
combinePrefixes(next) {
|
|
67
|
+
if (next === undefined) {
|
|
68
|
+
return this.prefix;
|
|
69
|
+
}
|
|
70
|
+
if (!this.pre) {
|
|
71
|
+
return next;
|
|
82
72
|
}
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
73
|
+
return `${this.pre} ${next}`;
|
|
74
|
+
}
|
|
75
|
+
formatArgs(rawArgs) {
|
|
76
|
+
return rawArgs.map((arg) => this.formatArg(arg));
|
|
77
|
+
}
|
|
78
|
+
buildPrefixes(args) {
|
|
79
|
+
if (!this.pre) {
|
|
80
|
+
return {
|
|
81
|
+
prefix: '',
|
|
82
|
+
chalkArgs: [...args],
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
const reducedPrefix = this.reducePrefix(this.pre);
|
|
86
|
+
const prefixValue = reducedPrefix.length > 0 ? ` ${reducedPrefix}` : '';
|
|
87
|
+
const chalkArgs = reducedPrefix.length > 0 ? [reducedPrefix, ...args] : [...args];
|
|
88
|
+
return {
|
|
89
|
+
prefix: prefixValue,
|
|
90
|
+
chalkArgs,
|
|
91
|
+
};
|
|
92
|
+
}
|
|
93
|
+
reducePrefix(prefix) {
|
|
94
|
+
const length = getMaxLogPrefixesLength();
|
|
95
|
+
if (typeof length === 'number' && !Number.isNaN(length)) {
|
|
96
|
+
if (length <= 0) {
|
|
97
|
+
return '';
|
|
88
98
|
}
|
|
89
|
-
|
|
99
|
+
const parts = prefix.split(' :: ');
|
|
100
|
+
return parts.slice(-length).join(' :: ');
|
|
101
|
+
}
|
|
102
|
+
return prefix;
|
|
103
|
+
}
|
|
104
|
+
dispatchToTransports(level, prefix, args) {
|
|
105
|
+
const transports = this.getTransports(level);
|
|
106
|
+
if (transports.length === 0) {
|
|
107
|
+
return false;
|
|
108
|
+
}
|
|
109
|
+
const payload = [prefix.trim(), ...args].filter((part) => part.length > 0);
|
|
110
|
+
for (const transport of transports) {
|
|
111
|
+
transport(...payload);
|
|
112
|
+
}
|
|
113
|
+
return true;
|
|
114
|
+
}
|
|
115
|
+
buildMessage(chalkMethod, chalkArgs, level, prefix) {
|
|
116
|
+
const baseMessage = this.shouldUseColors && chalkMethod
|
|
117
|
+
? chalkMethod(...chalkArgs)
|
|
118
|
+
: this.buildPlainMessage(level, prefix);
|
|
119
|
+
const withDelta = this.shouldLogTimeDeltas
|
|
120
|
+
? this.decorateWithTimeDelta(baseMessage)
|
|
121
|
+
: baseMessage;
|
|
122
|
+
return this.shouldLogTime
|
|
123
|
+
? this.decorateWithTimestamp(withDelta)
|
|
124
|
+
: withDelta;
|
|
125
|
+
}
|
|
126
|
+
buildPlainMessage(level, prefix) {
|
|
127
|
+
return `(${level})${prefix}`;
|
|
128
|
+
}
|
|
129
|
+
decorateWithTimeDelta(message) {
|
|
130
|
+
const now = Date.now();
|
|
131
|
+
const diff = now - lastLogTimeMs;
|
|
132
|
+
lastLogTimeMs = now;
|
|
133
|
+
return `(${diff}ms) ${message}`;
|
|
134
|
+
}
|
|
135
|
+
decorateWithTimestamp(message) {
|
|
136
|
+
return `(${new Date().toISOString()}) ${message}`;
|
|
137
|
+
}
|
|
138
|
+
emit(transport, message, formattedArgs, rawArgs) {
|
|
139
|
+
if (transport.isConsole) {
|
|
140
|
+
transport.fn(message, ...rawArgs);
|
|
141
|
+
return;
|
|
142
|
+
}
|
|
143
|
+
if (this.shouldUseColors === false) {
|
|
144
|
+
transport.fn(message, ...formattedArgs);
|
|
145
|
+
return;
|
|
146
|
+
}
|
|
147
|
+
transport.fn(message);
|
|
148
|
+
}
|
|
149
|
+
getTransports(level) {
|
|
150
|
+
const transport = this.transports[level];
|
|
151
|
+
if (!transport) {
|
|
152
|
+
return [];
|
|
90
153
|
}
|
|
91
|
-
|
|
92
|
-
|
|
154
|
+
return Array.isArray(transport) ? transport : [transport];
|
|
155
|
+
}
|
|
156
|
+
resolveConsoleMethod(level) {
|
|
93
157
|
switch (level) {
|
|
94
158
|
case 'ERROR':
|
|
95
|
-
|
|
96
|
-
break;
|
|
159
|
+
return 'error';
|
|
97
160
|
case 'WARN':
|
|
98
|
-
|
|
99
|
-
break;
|
|
161
|
+
return 'warn';
|
|
100
162
|
default:
|
|
101
|
-
|
|
102
|
-
break;
|
|
103
|
-
}
|
|
104
|
-
const transport = log ??
|
|
105
|
-
(level === 'ERROR' && getProcess()?.stderr?.write
|
|
106
|
-
? (...args) => {
|
|
107
|
-
getProcess()?.stderr.write(args.join(' ') + '\n');
|
|
108
|
-
}
|
|
109
|
-
: (console[logMethod] ?? console.log).bind(console));
|
|
110
|
-
let message = shouldUseColors === false
|
|
111
|
-
? `(${level})${prefix}`
|
|
112
|
-
: chalkMethod?.(...chalkArgs);
|
|
113
|
-
if (env.SHOULD_LOG_TIME_DELTAS !== 'false') {
|
|
114
|
-
const now = Date.now();
|
|
115
|
-
const diff = now - lastLogTimeMs;
|
|
116
|
-
lastLogTimeMs = now;
|
|
117
|
-
message = `(${diff}ms) ${message}`;
|
|
118
|
-
}
|
|
119
|
-
if (env.SHOULD_LOG_TIME !== 'false') {
|
|
120
|
-
message = `(${new Date().toISOString()}) ${message}`;
|
|
121
|
-
}
|
|
122
|
-
if (shouldUseColors === false) {
|
|
123
|
-
transport(message, ...args);
|
|
124
|
-
}
|
|
125
|
-
else {
|
|
126
|
-
transport(message);
|
|
163
|
+
return 'log';
|
|
127
164
|
}
|
|
128
|
-
|
|
165
|
+
}
|
|
166
|
+
resolveTransport(level, logMethod) {
|
|
167
|
+
if (this.baseLog) {
|
|
168
|
+
const logFn = this.baseLog;
|
|
169
|
+
return {
|
|
170
|
+
fn: (...parts) => {
|
|
171
|
+
logFn(...parts);
|
|
172
|
+
},
|
|
173
|
+
isConsole: false,
|
|
174
|
+
};
|
|
175
|
+
}
|
|
176
|
+
if (level === 'ERROR' && getProcess()?.stderr?.write) {
|
|
177
|
+
return {
|
|
178
|
+
fn: (...parts) => {
|
|
179
|
+
const stringParts = parts.map((part) => typeof part === 'string' ? part : this.formatArg(part));
|
|
180
|
+
getProcess()?.stderr?.write?.(stringParts.join(' ') + '\n');
|
|
181
|
+
},
|
|
182
|
+
isConsole: false,
|
|
183
|
+
};
|
|
184
|
+
}
|
|
185
|
+
const consoleMethod = (console[logMethod] ?? console.log).bind(console);
|
|
186
|
+
return {
|
|
187
|
+
fn: (...parts) => {
|
|
188
|
+
consoleMethod(...parts);
|
|
189
|
+
},
|
|
190
|
+
isConsole: true,
|
|
191
|
+
};
|
|
192
|
+
}
|
|
193
|
+
get env() {
|
|
194
|
+
return getProcess()?.env ?? {};
|
|
195
|
+
}
|
|
196
|
+
get logLevel() {
|
|
197
|
+
return this.env.LOG_LEVEL ?? undefined;
|
|
198
|
+
}
|
|
199
|
+
get shouldWrite() {
|
|
200
|
+
const levelSetting = this.logLevel;
|
|
201
|
+
return (level) => shouldWrite(levelSetting, level);
|
|
202
|
+
}
|
|
203
|
+
get shouldLogTimeDeltas() {
|
|
204
|
+
return this.env.SHOULD_LOG_TIME_DELTAS !== 'false';
|
|
205
|
+
}
|
|
206
|
+
get shouldLogTime() {
|
|
207
|
+
return this.env.SHOULD_LOG_TIME !== 'false';
|
|
208
|
+
}
|
|
209
|
+
formatArg(value) {
|
|
210
|
+
const formatter = value?.toString;
|
|
211
|
+
if (typeof formatter === 'function') {
|
|
212
|
+
return formatter.call(value);
|
|
213
|
+
}
|
|
214
|
+
return 'undefined';
|
|
129
215
|
}
|
|
130
216
|
}
|
|
217
|
+
function buildLog(prefix = undefined, options) {
|
|
218
|
+
return new Logger(prefix, options);
|
|
219
|
+
}
|
|
131
220
|
exports.testLog = buildLog('TEST', {
|
|
132
221
|
log: (...parts) => {
|
|
133
222
|
getProcess()?.stderr?.write?.(parts.join(' ') + '\n');
|
|
@@ -137,17 +226,27 @@ exports.stubLog = buildLog('STUB', {
|
|
|
137
226
|
log: () => { },
|
|
138
227
|
useColors: false,
|
|
139
228
|
});
|
|
140
|
-
function shouldWrite(
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
return false;
|
|
144
|
-
}
|
|
145
|
-
if (getProcess()?.env?.LOG_LEVEL == 'error' && level !== 'ERROR') {
|
|
146
|
-
return false;
|
|
147
|
-
}
|
|
148
|
-
if (getProcess()?.env?.LOG_LEVEL == 'warn' && level === 'INFO') {
|
|
229
|
+
function shouldWrite(logLevel, level) {
|
|
230
|
+
switch (logLevel?.toLowerCase()) {
|
|
231
|
+
case 'none':
|
|
149
232
|
return false;
|
|
150
|
-
|
|
233
|
+
case 'error':
|
|
234
|
+
return level === 'ERROR';
|
|
235
|
+
case 'warn':
|
|
236
|
+
return level !== 'INFO';
|
|
237
|
+
default:
|
|
238
|
+
return true;
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
function getProcess() {
|
|
242
|
+
if (typeof process !== 'undefined') {
|
|
243
|
+
return process;
|
|
151
244
|
}
|
|
152
|
-
return
|
|
245
|
+
return null;
|
|
153
246
|
}
|
|
247
|
+
let lastLogTimeMs = Date.now();
|
|
248
|
+
const getMaxLogPrefixesLength = () => {
|
|
249
|
+
return typeof getProcess()?.env?.MAXIMUM_LOG_PREFIXES_LENGTH === 'string'
|
|
250
|
+
? +getProcess()?.env?.MAXIMUM_LOG_PREFIXES_LENGTH
|
|
251
|
+
: undefined;
|
|
252
|
+
};
|
package/package.json
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
"publishConfig": {
|
|
4
4
|
"access": "public"
|
|
5
5
|
},
|
|
6
|
-
"version": "32.0.
|
|
6
|
+
"version": "32.0.119",
|
|
7
7
|
"skill": {
|
|
8
8
|
"namespace": "skill-utils",
|
|
9
9
|
"upgradeIgnoreList": [
|
|
@@ -54,7 +54,8 @@
|
|
|
54
54
|
"watch.tsc": "tsc -w",
|
|
55
55
|
"post.watch.build": "yarn run build.copy-files && yarn run build.resolve-paths",
|
|
56
56
|
"watch.build.dev": "tsc-watch --sourceMap --onCompilationComplete 'yarn run post.watch.build'",
|
|
57
|
-
"lint.tsc": "tsc -p . --noEmit"
|
|
57
|
+
"lint.tsc": "tsc -p . --noEmit",
|
|
58
|
+
"test.browser": "support/test-browser.sh"
|
|
58
59
|
},
|
|
59
60
|
"dependencies": {
|
|
60
61
|
"@sprucelabs/globby": "^2.0.506",
|