@bemoje/stacktrace 2.0.0 → 2.0.2
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/lib/index.cjs +25 -15
- package/lib/index.cjs.map +1 -0
- package/lib/index.js +24 -14
- package/lib/index.js.map +1 -0
- package/package.json +6 -3
package/lib/index.cjs
CHANGED
|
@@ -1,17 +1,28 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
var
|
|
3
|
+
var colors3 = require('ansi-colors');
|
|
4
4
|
var util = require('util');
|
|
5
|
+
var esToolkit = require('es-toolkit');
|
|
5
6
|
var stacktraceParser = require('stacktrace-parser');
|
|
6
7
|
var upath = require('upath');
|
|
7
8
|
|
|
8
9
|
function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
|
|
9
10
|
|
|
10
|
-
var
|
|
11
|
+
var colors3__default = /*#__PURE__*/_interopDefault(colors3);
|
|
11
12
|
var upath__default = /*#__PURE__*/_interopDefault(upath);
|
|
12
13
|
|
|
13
14
|
var __defProp = Object.defineProperty;
|
|
14
15
|
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
16
|
+
var isTerminalColorSupported = esToolkit.once(/* @__PURE__ */ __name(function isTerminalColorSupported2() {
|
|
17
|
+
const { NODE_DISABLE_COLORS, NO_COLOR, TERM } = process.env;
|
|
18
|
+
return colors3__default.default.enabled = colors3__default.default.enabled && !NODE_DISABLE_COLORS && NO_COLOR == null && TERM !== "dumb" && process.stdout && process.stdout.isTTY;
|
|
19
|
+
}, "isTerminalColorSupported"));
|
|
20
|
+
var initColorSettings = esToolkit.once(/* @__PURE__ */ __name(function initColorSettings2() {
|
|
21
|
+
const enabled = isTerminalColorSupported();
|
|
22
|
+
colors3__default.default.enabled = enabled;
|
|
23
|
+
util.inspect.defaultOptions.colors = enabled;
|
|
24
|
+
return enabled;
|
|
25
|
+
}, "initColorSettings"));
|
|
15
26
|
function prettyStackTrace(error, options = {}) {
|
|
16
27
|
return [
|
|
17
28
|
renderMessage(error),
|
|
@@ -22,7 +33,7 @@ function prettyStackTrace(error, options = {}) {
|
|
|
22
33
|
}
|
|
23
34
|
__name(prettyStackTrace, "prettyStackTrace");
|
|
24
35
|
function renderMessage(error) {
|
|
25
|
-
return
|
|
36
|
+
return colors3__default.default.red(`${error.name}: ${error.message}`);
|
|
26
37
|
}
|
|
27
38
|
__name(renderMessage, "renderMessage");
|
|
28
39
|
function renderStack(error, options) {
|
|
@@ -30,9 +41,7 @@ function renderStack(error, options) {
|
|
|
30
41
|
return "";
|
|
31
42
|
}
|
|
32
43
|
const frames = stacktraceParser.parse(error.stack);
|
|
33
|
-
const offset = frames.reduce((acc, frame) =>
|
|
34
|
-
return Math.max(acc, frame.methodName.length);
|
|
35
|
-
}, 0);
|
|
44
|
+
const offset = frames.reduce((acc, frame) => Math.max(acc, frame.methodName.length), 0);
|
|
36
45
|
const stack = frames.map((frame) => {
|
|
37
46
|
if (frame.file) {
|
|
38
47
|
frame.file = upath__default.default.relative(process.cwd(), frame.file.replace(/^file:[/\\]+/, ""));
|
|
@@ -43,14 +52,14 @@ function renderStack(error, options) {
|
|
|
43
52
|
if (file) {
|
|
44
53
|
const base = upath__default.default.basename(file);
|
|
45
54
|
if (file.startsWith("node:")) {
|
|
46
|
-
s +=
|
|
47
|
-
fp =
|
|
55
|
+
s += colors3__default.default.gray(methodName);
|
|
56
|
+
fp = colors3__default.default.gray(file);
|
|
48
57
|
} else if (file.includes("node_modules")) {
|
|
49
58
|
s += methodName;
|
|
50
|
-
fp = file.replace(base,
|
|
59
|
+
fp = file.replace(base, colors3__default.default.yellow(base));
|
|
51
60
|
} else {
|
|
52
61
|
s += methodName;
|
|
53
|
-
fp = file.replace(base,
|
|
62
|
+
fp = file.replace(base, colors3__default.default.red(base));
|
|
54
63
|
}
|
|
55
64
|
}
|
|
56
65
|
s += " ".repeat(2 + offset - methodName.length);
|
|
@@ -58,7 +67,7 @@ function renderStack(error, options) {
|
|
|
58
67
|
return s;
|
|
59
68
|
});
|
|
60
69
|
return [
|
|
61
|
-
|
|
70
|
+
colors3__default.default.magenta("stack:"),
|
|
62
71
|
...stack
|
|
63
72
|
].join("\n");
|
|
64
73
|
}
|
|
@@ -73,9 +82,7 @@ function renderProps(error, options) {
|
|
|
73
82
|
"frames",
|
|
74
83
|
"stack"
|
|
75
84
|
]);
|
|
76
|
-
const keys = Object.getOwnPropertyNames(error).filter((key) =>
|
|
77
|
-
return !ignore.has(key);
|
|
78
|
-
});
|
|
85
|
+
const keys = Object.getOwnPropertyNames(error).filter((key) => !ignore.has(key));
|
|
79
86
|
if (!keys.length) {
|
|
80
87
|
return "";
|
|
81
88
|
}
|
|
@@ -86,13 +93,14 @@ function renderProps(error, options) {
|
|
|
86
93
|
}
|
|
87
94
|
}
|
|
88
95
|
return util.inspect(data, {
|
|
89
|
-
colors:
|
|
96
|
+
colors: colors3__default.default.enabled
|
|
90
97
|
});
|
|
91
98
|
}
|
|
92
99
|
__name(renderProps, "renderProps");
|
|
93
100
|
|
|
94
101
|
// src/enablePrettyStackTrace.ts
|
|
95
102
|
function enablePrettyStackTrace() {
|
|
103
|
+
initColorSettings();
|
|
96
104
|
process.prependOnceListener("uncaughtException", (e) => {
|
|
97
105
|
console.error(prettyStackTrace(e));
|
|
98
106
|
});
|
|
@@ -101,3 +109,5 @@ __name(enablePrettyStackTrace, "enablePrettyStackTrace");
|
|
|
101
109
|
|
|
102
110
|
exports.enablePrettyStackTrace = enablePrettyStackTrace;
|
|
103
111
|
exports.prettyStackTrace = prettyStackTrace;
|
|
112
|
+
//# sourceMappingURL=index.cjs.map
|
|
113
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../node/src/isTerminalColorSupported.ts","../../../node/src/initColorSettings.ts","../../src/prettyStackTrace.ts","../../src/enablePrettyStackTrace.ts"],"names":["isTerminalColorSupported","once","NODE_DISABLE_COLORS","NO_COLOR","TERM","process","env","colors","enabled","stdout","isTTY","initColorSettings","inspect","defaultOptions","prettyStackTrace","error","options","renderMessage","renderProps","renderStack","join","red","name","message","stack","omitStack","frames","parse","offset","reduce","acc","frame","Math","max","methodName","length","map","file","upath","relative","cwd","replace","lineNumber","column","s","fp","base","basename","startsWith","gray","includes","yellow","repeat","magenta","omitProps","ignore","Set","keys","Object","getOwnPropertyNames","filter","key","has","data","k","v","entries","enablePrettyStackTrace","prependOnceListener","e","console"],"mappings":";;;;;;;;;;;;;;;AAMO,IAAMA,wBAAAA,GAA2BC,cAAAA,iBAAK,MAAA,CAAA,SAASD,yBAAAA,GAAAA;AACpD,EAAA,MAAM,EAAEE,mBAAAA,EAAqBC,QAAAA,EAAUC,IAAAA,KAASC,OAAAA,CAAQC,GAAAA;AACxD,EAAA,OAAQC,wBAAAA,CAAOC,OAAAA,GACbD,wBAAAA,CAAOC,OAAAA,IACP,CAACN,mBAAAA,IACDC,QAAAA,IAAY,IAAA,IACZC,IAAAA,KAAS,MAAA,IACTC,OAAAA,CAAQI,MAAAA,IACRJ,QAAQI,MAAAA,CAAOC,KAAAA;AACnB,CAAA,EAT6C,0BAAA,CAS7C,CAAA;ACPO,IAAMC,iBAAAA,GAAoBV,cAAAA,iBAAK,MAAA,CAAA,SAASU,kBAAAA,GAAAA;AAC7C,EAAA,MAAMH,UAAUR,wBAAAA,EAAAA;AAChBO,EAAAA,yBAAOC,OAAAA,GAAUA,OAAAA;AACjBI,EAAAA,YAAAA,CAAQC,eAAeN,MAAAA,GAASC,OAAAA;AAChC,EAAA,OAAOA,OAAAA;AACT,CAAA,EALsC,mBAAA,CAKtC,CAAA;ACJO,SAASM,gBAAAA,CACdC,KAAAA,EACAC,OAAAA,GAAwD,EAAC,EAAC;AAE1D,EAAA,OAAO;AAACC,IAAAA,aAAAA,CAAcF,KAAAA,CAAAA;AAAQG,IAAAA,WAAAA,CAAYH,OAAOC,OAAAA,CAAAA;AAAUG,IAAAA,WAAAA,CAAYJ,OAAOC,OAA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sBAAAA,EAAAA,wBAAAA,CAAAA","file":"index.cjs","sourcesContent":["import colors from 'ansi-colors'\nimport { once } from 'es-toolkit'\n\n/**\n * Check if colored terminal output is (probably) supported.\n */\nexport const isTerminalColorSupported = once(function isTerminalColorSupported() {\n const { NODE_DISABLE_COLORS, NO_COLOR, TERM } = process.env\n return (colors.enabled =\n colors.enabled &&\n !NODE_DISABLE_COLORS &&\n NO_COLOR == null &&\n TERM !== 'dumb' &&\n process.stdout &&\n process.stdout.isTTY)\n})\n","import colors from 'ansi-colors'\nimport { inspect } from 'util'\nimport { isTerminalColorSupported } from './isTerminalColorSupported'\nimport { once } from 'es-toolkit'\n\n/**\n * Initializes color settings for the terminal and Node.js inspect utility based on terminal support.\n */\nexport const initColorSettings = once(function initColorSettings() {\n const enabled = isTerminalColorSupported()\n colors.enabled = enabled\n inspect.defaultOptions.colors = enabled\n return enabled\n})\n","import type { StackFrame } from 'stacktrace-parser'\nimport colors from 'ansi-colors'\nimport { inspect } from 'util'\nimport { parse } from 'stacktrace-parser'\nimport upath from 'upath'\n\n/**\n * Formats stack traces with colors and improved readability for debugging.\n */\nexport function prettyStackTrace(\n error: Error,\n options: { omitStack?: boolean; omitProps?: boolean } = {}\n): string {\n return [renderMessage(error), renderProps(error, options), renderStack(error, options), ''].join('\\n')\n}\n\nfunction renderMessage(error: Error) {\n return colors.red(`${error.name}: ${error.message}`)\n}\n\nfunction renderStack(error: Error, options: { omitStack?: boolean; omitProps?: boolean }) {\n if (!error.stack || options.omitStack) {\n return ''\n }\n\n const frames: StackFrame[] = parse(error.stack)\n\n // width of the first column = the longest frame.cell string\n const offset = frames.reduce((acc: number, frame) => Math.max(acc, frame.methodName.length), 0)\n\n const stack = frames.map((frame) => {\n if (frame.file) {\n frame.file = upath.relative(process.cwd(), frame.file.replace(/^file:[/\\\\]+/, ''))\n }\n const { methodName, file, lineNumber, column } = frame\n let s = ' '\n let fp: string = ''\n if (file) {\n const base = upath.basename(file)\n if (file.startsWith('node:')) {\n s += colors.gray(methodName)\n fp = colors.gray(file)\n } else if (file.includes('node_modules')) {\n s += methodName\n fp = file!.replace(base, colors.yellow(base))\n } else {\n s += methodName\n fp = file!.replace(base, colors.red(base))\n }\n }\n s += ' '.repeat(2 + offset - methodName.length)\n s += `${fp}:${lineNumber}:${column}`\n return s\n })\n\n return [\n colors.magenta('stack:'), //\n ...stack,\n ].join('\\n')\n}\n\nfunction renderProps(error: Error, options: { omitStack?: boolean; omitProps?: boolean }) {\n if (options.omitProps) {\n return ''\n }\n const ignore = new Set(['name', 'message', 'frames', 'stack'])\n const keys = Object.getOwnPropertyNames(error).filter((key) => !ignore.has(key))\n if (!keys.length) {\n return ''\n }\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n const data = {} as any\n for (const [k, v] of Object.entries(error)) {\n if (keys.includes(k)) {\n data[k] = v\n }\n }\n // const data = _.pick(error, keys)\n return inspect(data, { colors: colors.enabled })\n}\n","import { initColorSettings } from '@mono/node/initColorSettings'\nimport { prettyStackTrace } from './prettyStackTrace'\n\n/**\n * Enables pretty stack trace formatting for uncaught exceptions.\n */\nexport function enablePrettyStackTrace() {\n initColorSettings()\n process.prependOnceListener('uncaughtException', (e) => {\n console.error(prettyStackTrace(e))\n })\n}\n"]}
|
package/lib/index.js
CHANGED
|
@@ -1,10 +1,21 @@
|
|
|
1
|
-
import
|
|
1
|
+
import colors3 from 'ansi-colors';
|
|
2
2
|
import { inspect } from 'util';
|
|
3
|
+
import { once } from 'es-toolkit';
|
|
3
4
|
import { parse } from 'stacktrace-parser';
|
|
4
5
|
import upath from 'upath';
|
|
5
6
|
|
|
6
7
|
var __defProp = Object.defineProperty;
|
|
7
8
|
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
9
|
+
var isTerminalColorSupported = once(/* @__PURE__ */ __name(function isTerminalColorSupported2() {
|
|
10
|
+
const { NODE_DISABLE_COLORS, NO_COLOR, TERM } = process.env;
|
|
11
|
+
return colors3.enabled = colors3.enabled && !NODE_DISABLE_COLORS && NO_COLOR == null && TERM !== "dumb" && process.stdout && process.stdout.isTTY;
|
|
12
|
+
}, "isTerminalColorSupported"));
|
|
13
|
+
var initColorSettings = once(/* @__PURE__ */ __name(function initColorSettings2() {
|
|
14
|
+
const enabled = isTerminalColorSupported();
|
|
15
|
+
colors3.enabled = enabled;
|
|
16
|
+
inspect.defaultOptions.colors = enabled;
|
|
17
|
+
return enabled;
|
|
18
|
+
}, "initColorSettings"));
|
|
8
19
|
function prettyStackTrace(error, options = {}) {
|
|
9
20
|
return [
|
|
10
21
|
renderMessage(error),
|
|
@@ -15,7 +26,7 @@ function prettyStackTrace(error, options = {}) {
|
|
|
15
26
|
}
|
|
16
27
|
__name(prettyStackTrace, "prettyStackTrace");
|
|
17
28
|
function renderMessage(error) {
|
|
18
|
-
return
|
|
29
|
+
return colors3.red(`${error.name}: ${error.message}`);
|
|
19
30
|
}
|
|
20
31
|
__name(renderMessage, "renderMessage");
|
|
21
32
|
function renderStack(error, options) {
|
|
@@ -23,9 +34,7 @@ function renderStack(error, options) {
|
|
|
23
34
|
return "";
|
|
24
35
|
}
|
|
25
36
|
const frames = parse(error.stack);
|
|
26
|
-
const offset = frames.reduce((acc, frame) =>
|
|
27
|
-
return Math.max(acc, frame.methodName.length);
|
|
28
|
-
}, 0);
|
|
37
|
+
const offset = frames.reduce((acc, frame) => Math.max(acc, frame.methodName.length), 0);
|
|
29
38
|
const stack = frames.map((frame) => {
|
|
30
39
|
if (frame.file) {
|
|
31
40
|
frame.file = upath.relative(process.cwd(), frame.file.replace(/^file:[/\\]+/, ""));
|
|
@@ -36,14 +45,14 @@ function renderStack(error, options) {
|
|
|
36
45
|
if (file) {
|
|
37
46
|
const base = upath.basename(file);
|
|
38
47
|
if (file.startsWith("node:")) {
|
|
39
|
-
s +=
|
|
40
|
-
fp =
|
|
48
|
+
s += colors3.gray(methodName);
|
|
49
|
+
fp = colors3.gray(file);
|
|
41
50
|
} else if (file.includes("node_modules")) {
|
|
42
51
|
s += methodName;
|
|
43
|
-
fp = file.replace(base,
|
|
52
|
+
fp = file.replace(base, colors3.yellow(base));
|
|
44
53
|
} else {
|
|
45
54
|
s += methodName;
|
|
46
|
-
fp = file.replace(base,
|
|
55
|
+
fp = file.replace(base, colors3.red(base));
|
|
47
56
|
}
|
|
48
57
|
}
|
|
49
58
|
s += " ".repeat(2 + offset - methodName.length);
|
|
@@ -51,7 +60,7 @@ function renderStack(error, options) {
|
|
|
51
60
|
return s;
|
|
52
61
|
});
|
|
53
62
|
return [
|
|
54
|
-
|
|
63
|
+
colors3.magenta("stack:"),
|
|
55
64
|
...stack
|
|
56
65
|
].join("\n");
|
|
57
66
|
}
|
|
@@ -66,9 +75,7 @@ function renderProps(error, options) {
|
|
|
66
75
|
"frames",
|
|
67
76
|
"stack"
|
|
68
77
|
]);
|
|
69
|
-
const keys = Object.getOwnPropertyNames(error).filter((key) =>
|
|
70
|
-
return !ignore.has(key);
|
|
71
|
-
});
|
|
78
|
+
const keys = Object.getOwnPropertyNames(error).filter((key) => !ignore.has(key));
|
|
72
79
|
if (!keys.length) {
|
|
73
80
|
return "";
|
|
74
81
|
}
|
|
@@ -79,13 +86,14 @@ function renderProps(error, options) {
|
|
|
79
86
|
}
|
|
80
87
|
}
|
|
81
88
|
return inspect(data, {
|
|
82
|
-
colors:
|
|
89
|
+
colors: colors3.enabled
|
|
83
90
|
});
|
|
84
91
|
}
|
|
85
92
|
__name(renderProps, "renderProps");
|
|
86
93
|
|
|
87
94
|
// src/enablePrettyStackTrace.ts
|
|
88
95
|
function enablePrettyStackTrace() {
|
|
96
|
+
initColorSettings();
|
|
89
97
|
process.prependOnceListener("uncaughtException", (e) => {
|
|
90
98
|
console.error(prettyStackTrace(e));
|
|
91
99
|
});
|
|
@@ -93,3 +101,5 @@ function enablePrettyStackTrace() {
|
|
|
93
101
|
__name(enablePrettyStackTrace, "enablePrettyStackTrace");
|
|
94
102
|
|
|
95
103
|
export { enablePrettyStackTrace, prettyStackTrace };
|
|
104
|
+
//# sourceMappingURL=index.js.map
|
|
105
|
+
//# sourceMappingURL=index.js.map
|
package/lib/index.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../node/src/isTerminalColorSupported.ts","../../../node/src/initColorSettings.ts","../../src/prettyStackTrace.ts","../../src/enablePrettyStackTrace.ts"],"names":["isTerminalColorSupported","once","NODE_DISABLE_COLORS","NO_COLOR","TERM","process","env","colors","enabled","stdout","isTTY","initColorSettings","inspect","defaultOptions","prettyStackTrace","error","options","renderMessage","renderProps","renderStack","join","red","name","message","stack","omitStack","frames","parse","offset","reduce","acc","frame","Math","max","methodName","length","map","file","upath","relative","cwd","replace","lineNumber","column","s","fp","base","basename","startsWith","gray","includes","yellow","repeat","magenta","omitProps","ignore","Set","keys","Object","getOwnPropertyNames","filter","key","has","data","k","v","entries","enablePrettyStackTrace","prependOnceListener","e","console"],"mappings":";;;;;;;;AAMO,IAAMA,wBAAAA,GAA2BC,IAAAA,iBAAK,MAAA,CAAA,SAASD,yBAAAA,GAAAA;AACpD,EAAA,MAAM,EAAEE,mBAAAA,EAAqBC,QAAAA,EAAUC,IAAAA,KAASC,OAAAA,CAAQC,GAAAA;AACxD,EAAA,OAAQC,OAAAA,CAAOC,OAAAA,GACbD,OAAAA,CAAOC,OAAAA,IACP,CAACN,mBAAAA,IACDC,QAAAA,IAAY,IAAA,IACZC,IAAAA,KAAS,MAAA,IACTC,OAAAA,CAAQI,MAAAA,IACRJ,QAAQI,MAAAA,CAAOC,KAAAA;AACnB,CAAA,EAT6C,0BAAA,CAS7C,CAAA;ACPO,IAAMC,iBAAAA,GAAoBV,IAAAA,iBAAK,MAAA,CAAA,SAASU,kBAAAA,GAAAA;AAC7C,EAAA,MAAMH,UAAUR,wBAAAA,EAAAA;AAChBO,EAAAA,QAAOC,OAAAA,GAAUA,OAAAA;AACjBI,EAAAA,OAAAA,CAAQC,eAAeN,MAAAA,GAASC,OAAAA;AAChC,EAAA,OAAOA,OAAAA;A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AA;AACdxD,EAAAA,iBAAAA,EAAAA;AACAN,EAAAA,OAAAA,CAAQ+D,mBAAAA,CAAoB,mBAAA,EAAqB,CAACC,CAAAA,KAAAA;AAChDC,IAAAA,OAAAA,CAAQvD,KAAAA,CAAMD,gBAAAA,CAAiBuD,CAAAA,CAAAA,CAAAA;EACjC,CAAA,CAAA;AACF;AALgBF,MAAAA,CAAAA,sBAAAA,EAAAA,wBAAAA,CAAAA","file":"index.js","sourcesContent":["import colors from 'ansi-colors'\nimport { once } from 'es-toolkit'\n\n/**\n * Check if colored terminal output is (probably) supported.\n */\nexport const isTerminalColorSupported = once(function isTerminalColorSupported() {\n const { NODE_DISABLE_COLORS, NO_COLOR, TERM } = process.env\n return (colors.enabled =\n colors.enabled &&\n !NODE_DISABLE_COLORS &&\n NO_COLOR == null &&\n TERM !== 'dumb' &&\n process.stdout &&\n process.stdout.isTTY)\n})\n","import colors from 'ansi-colors'\nimport { inspect } from 'util'\nimport { isTerminalColorSupported } from './isTerminalColorSupported'\nimport { once } from 'es-toolkit'\n\n/**\n * Initializes color settings for the terminal and Node.js inspect utility based on terminal support.\n */\nexport const initColorSettings = once(function initColorSettings() {\n const enabled = isTerminalColorSupported()\n colors.enabled = enabled\n inspect.defaultOptions.colors = enabled\n return enabled\n})\n","import type { StackFrame } from 'stacktrace-parser'\nimport colors from 'ansi-colors'\nimport { inspect } from 'util'\nimport { parse } from 'stacktrace-parser'\nimport upath from 'upath'\n\n/**\n * Formats stack traces with colors and improved readability for debugging.\n */\nexport function prettyStackTrace(\n error: Error,\n options: { omitStack?: boolean; omitProps?: boolean } = {}\n): string {\n return [renderMessage(error), renderProps(error, options), renderStack(error, options), ''].join('\\n')\n}\n\nfunction renderMessage(error: Error) {\n return colors.red(`${error.name}: ${error.message}`)\n}\n\nfunction renderStack(error: Error, options: { omitStack?: boolean; omitProps?: boolean }) {\n if (!error.stack || options.omitStack) {\n return ''\n }\n\n const frames: StackFrame[] = parse(error.stack)\n\n // width of the first column = the longest frame.cell string\n const offset = frames.reduce((acc: number, frame) => Math.max(acc, frame.methodName.length), 0)\n\n const stack = frames.map((frame) => {\n if (frame.file) {\n frame.file = upath.relative(process.cwd(), frame.file.replace(/^file:[/\\\\]+/, ''))\n }\n const { methodName, file, lineNumber, column } = frame\n let s = ' '\n let fp: string = ''\n if (file) {\n const base = upath.basename(file)\n if (file.startsWith('node:')) {\n s += colors.gray(methodName)\n fp = colors.gray(file)\n } else if (file.includes('node_modules')) {\n s += methodName\n fp = file!.replace(base, colors.yellow(base))\n } else {\n s += methodName\n fp = file!.replace(base, colors.red(base))\n }\n }\n s += ' '.repeat(2 + offset - methodName.length)\n s += `${fp}:${lineNumber}:${column}`\n return s\n })\n\n return [\n colors.magenta('stack:'), //\n ...stack,\n ].join('\\n')\n}\n\nfunction renderProps(error: Error, options: { omitStack?: boolean; omitProps?: boolean }) {\n if (options.omitProps) {\n return ''\n }\n const ignore = new Set(['name', 'message', 'frames', 'stack'])\n const keys = Object.getOwnPropertyNames(error).filter((key) => !ignore.has(key))\n if (!keys.length) {\n return ''\n }\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n const data = {} as any\n for (const [k, v] of Object.entries(error)) {\n if (keys.includes(k)) {\n data[k] = v\n }\n }\n // const data = _.pick(error, keys)\n return inspect(data, { colors: colors.enabled })\n}\n","import { initColorSettings } from '@mono/node/initColorSettings'\nimport { prettyStackTrace } from './prettyStackTrace'\n\n/**\n * Enables pretty stack trace formatting for uncaught exceptions.\n */\nexport function enablePrettyStackTrace() {\n initColorSettings()\n process.prependOnceListener('uncaughtException', (e) => {\n console.error(prettyStackTrace(e))\n })\n}\n"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@bemoje/stacktrace",
|
|
3
|
-
"version": "2.0.
|
|
3
|
+
"version": "2.0.2",
|
|
4
4
|
"description": "Beautiful, color-formatted stack trace output for Node.js errors with syntax-highlighted file paths and structured error properties.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"sideEffects": false,
|
|
@@ -9,8 +9,9 @@
|
|
|
9
9
|
},
|
|
10
10
|
"dependencies": {
|
|
11
11
|
"ansi-colors": "^4.1.3",
|
|
12
|
+
"stacktrace-parser": "^0.1.11",
|
|
12
13
|
"upath": "^2.0.1",
|
|
13
|
-
"
|
|
14
|
+
"es-toolkit": "^1.45.1"
|
|
14
15
|
},
|
|
15
16
|
"keywords": [
|
|
16
17
|
"stacktrace",
|
|
@@ -30,7 +31,9 @@
|
|
|
30
31
|
"./lib/index.js",
|
|
31
32
|
"./lib/index.d.ts",
|
|
32
33
|
"./lib/index.cjs",
|
|
33
|
-
"./lib/index.d.cts"
|
|
34
|
+
"./lib/index.d.cts",
|
|
35
|
+
"./lib/index.js.map",
|
|
36
|
+
"./lib/index.cjs.map"
|
|
34
37
|
],
|
|
35
38
|
"exports": {
|
|
36
39
|
".": {
|