@bemoje/stacktrace 2.0.0 → 2.0.1

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.
Files changed (3) hide show
  1. package/lib/index.cjs +23 -15
  2. package/lib/index.js +22 -14
  3. package/package.json +3 -2
package/lib/index.cjs CHANGED
@@ -1,17 +1,28 @@
1
1
  'use strict';
2
2
 
3
- var colors = require('ansi-colors');
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 colors__default = /*#__PURE__*/_interopDefault(colors);
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 colors__default.default.red(`${error.name}: ${error.message}`);
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 += colors__default.default.gray(methodName);
47
- fp = colors__default.default.gray(file);
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, colors__default.default.yellow(base));
59
+ fp = file.replace(base, colors3__default.default.yellow(base));
51
60
  } else {
52
61
  s += methodName;
53
- fp = file.replace(base, colors__default.default.red(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
- colors__default.default.magenta("stack:"),
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: true
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
  });
package/lib/index.js CHANGED
@@ -1,10 +1,21 @@
1
- import colors from 'ansi-colors';
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 colors.red(`${error.name}: ${error.message}`);
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 += colors.gray(methodName);
40
- fp = colors.gray(file);
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, colors.yellow(base));
52
+ fp = file.replace(base, colors3.yellow(base));
44
53
  } else {
45
54
  s += methodName;
46
- fp = file.replace(base, colors.red(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
- colors.magenta("stack:"),
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: true
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
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bemoje/stacktrace",
3
- "version": "2.0.0",
3
+ "version": "2.0.1",
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
- "stacktrace-parser": "^0.1.11"
14
+ "es-toolkit": "^1.45.1"
14
15
  },
15
16
  "keywords": [
16
17
  "stacktrace",