@hackthedev/terminal-logger 1.0.0 → 1.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.
- package/.github/workflows/publish.yml +43 -0
- package/README.md +2 -0
- package/index.mjs +95 -17
- package/package.json +1 -1
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
name: Publish to npm
|
|
2
|
+
|
|
3
|
+
on:
|
|
4
|
+
push:
|
|
5
|
+
branches:
|
|
6
|
+
- main
|
|
7
|
+
|
|
8
|
+
permissions:
|
|
9
|
+
contents: write
|
|
10
|
+
|
|
11
|
+
jobs:
|
|
12
|
+
publish:
|
|
13
|
+
runs-on: ubuntu-latest
|
|
14
|
+
|
|
15
|
+
steps:
|
|
16
|
+
- uses: actions/checkout@v4
|
|
17
|
+
with:
|
|
18
|
+
persist-credentials: true
|
|
19
|
+
|
|
20
|
+
- name: Skip version bump commits
|
|
21
|
+
run: |
|
|
22
|
+
if git log -1 --pretty=%B | grep -q "chore: bump version"; then
|
|
23
|
+
echo "Version bump commit detected, skipping."
|
|
24
|
+
exit 0
|
|
25
|
+
fi
|
|
26
|
+
|
|
27
|
+
- uses: actions/setup-node@v4
|
|
28
|
+
with:
|
|
29
|
+
node-version: 20
|
|
30
|
+
registry-url: https://registry.npmjs.org/
|
|
31
|
+
|
|
32
|
+
- run: npm ci
|
|
33
|
+
|
|
34
|
+
- run: |
|
|
35
|
+
git config user.name "github-actions"
|
|
36
|
+
git config user.email "actions@github.com"
|
|
37
|
+
npm version patch -m "chore: bump version %s"
|
|
38
|
+
git push
|
|
39
|
+
|
|
40
|
+
- run: npm publish --access public
|
|
41
|
+
env:
|
|
42
|
+
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
|
|
43
|
+
|
package/README.md
CHANGED
package/index.mjs
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import fs from "fs";
|
|
2
|
+
|
|
1
3
|
export default class Logger {
|
|
2
4
|
static logDebug = false;
|
|
3
5
|
|
|
@@ -32,45 +34,121 @@ export default class Logger {
|
|
|
32
34
|
|
|
33
35
|
static log(level, message, color = Logger.colors.fgWhite) {
|
|
34
36
|
if (message instanceof Error) {
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
console.log(`${color}${Logger.displayDate()}[${level}]\n${JSON.stringify(message, null, 4)}${Logger.colors.reset}`);
|
|
38
|
-
} else {
|
|
39
|
-
console.log(`${color}${Logger.displayDate()}[${level}] ${message}${Logger.colors.reset}`);
|
|
37
|
+
Logger.printErrorWithFrame(level, message, color);
|
|
38
|
+
return;
|
|
40
39
|
}
|
|
40
|
+
|
|
41
|
+
console.log(
|
|
42
|
+
`${color}${Logger.displayDate()}[${level}] ${message}${Logger.colors.reset}`
|
|
43
|
+
);
|
|
41
44
|
}
|
|
42
45
|
|
|
43
|
-
static
|
|
44
|
-
|
|
45
|
-
|
|
46
|
+
static printErrorWithFrame(level, err, color) {
|
|
47
|
+
const reset = Logger.colors.reset;
|
|
48
|
+
|
|
49
|
+
console.log(
|
|
50
|
+
`${color}${Logger.displayDate()}[${level}] ${err.name}: ${err.message}${reset}`
|
|
51
|
+
);
|
|
52
|
+
|
|
53
|
+
if (!err.stack) return;
|
|
54
|
+
|
|
55
|
+
const stackLines = err.stack.split("\n");
|
|
56
|
+
const firstFrame = stackLines.find(l => l.includes("(") && l.includes(":"));
|
|
57
|
+
const match = firstFrame?.match(/\((.*):(\d+):(\d+)\)/);
|
|
58
|
+
|
|
59
|
+
if (!match) {
|
|
60
|
+
console.log(`${color}${err.stack}${reset}`);
|
|
61
|
+
return;
|
|
46
62
|
}
|
|
63
|
+
|
|
64
|
+
const [, file, lineStr, colStr] = match;
|
|
65
|
+
const line = parseInt(lineStr, 10);
|
|
66
|
+
const col = parseInt(colStr, 10);
|
|
67
|
+
|
|
68
|
+
try {
|
|
69
|
+
const lines = fs.readFileSync(file, "utf8").split("\n");
|
|
70
|
+
const start = Math.max(0, line - 3);
|
|
71
|
+
const end = Math.min(lines.length, line + 2);
|
|
72
|
+
|
|
73
|
+
for (let i = start; i < end; i++) {
|
|
74
|
+
const ln = String(i + 1).padStart(3, " ");
|
|
75
|
+
console.log(`${color}${ln} | ${lines[i]}${reset}`);
|
|
76
|
+
if (i + 1 === line) {
|
|
77
|
+
console.log(
|
|
78
|
+
`${color} | ${" ".repeat(Math.max(col - 1, 0))}^${reset}`
|
|
79
|
+
);
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
} catch {
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
for (const l of stackLines.slice(1)) {
|
|
86
|
+
console.log(`${color}${l}${reset}`);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
static space(amount = 1) {
|
|
92
|
+
for (let i = 0; i < amount; i++) console.log(" ");
|
|
47
93
|
}
|
|
48
94
|
|
|
49
95
|
static info(message, color = "") {
|
|
50
|
-
Logger.log(
|
|
96
|
+
Logger.log(
|
|
97
|
+
"INFO",
|
|
98
|
+
message,
|
|
99
|
+
color ? Logger.colors.fgCyan + color : Logger.colors.fgCyan
|
|
100
|
+
);
|
|
51
101
|
}
|
|
52
102
|
|
|
53
103
|
static success(message, color = "") {
|
|
54
|
-
Logger.log(
|
|
104
|
+
Logger.log(
|
|
105
|
+
"SUCCESS",
|
|
106
|
+
message,
|
|
107
|
+
color ? Logger.colors.fgGreen + color : Logger.colors.fgGreen
|
|
108
|
+
);
|
|
55
109
|
}
|
|
56
110
|
|
|
57
111
|
static warn(message, color = "") {
|
|
58
|
-
Logger.log(
|
|
112
|
+
Logger.log(
|
|
113
|
+
"WARN",
|
|
114
|
+
message,
|
|
115
|
+
color ? Logger.colors.fgYellow + color : Logger.colors.fgYellow
|
|
116
|
+
);
|
|
59
117
|
}
|
|
60
118
|
|
|
61
119
|
static error(message, color = "") {
|
|
62
|
-
Logger.log(
|
|
120
|
+
Logger.log(
|
|
121
|
+
"ERROR",
|
|
122
|
+
message,
|
|
123
|
+
color ? Logger.colors.fgRed + color : Logger.colors.fgRed
|
|
124
|
+
);
|
|
63
125
|
}
|
|
64
126
|
|
|
65
127
|
static debug(message, color = "") {
|
|
66
|
-
if(!Logger.logDebug) return;
|
|
67
|
-
Logger.log(
|
|
128
|
+
if (!Logger.logDebug) return;
|
|
129
|
+
Logger.log(
|
|
130
|
+
"DEBUG",
|
|
131
|
+
message,
|
|
132
|
+
color
|
|
133
|
+
? Logger.colors.bright + Logger.colors.fgBlack + color
|
|
134
|
+
: Logger.colors.bright + Logger.colors.fgBlack
|
|
135
|
+
);
|
|
68
136
|
}
|
|
69
137
|
|
|
70
138
|
static displayDate() {
|
|
71
|
-
const
|
|
72
|
-
const date =
|
|
73
|
-
|
|
139
|
+
const d = new Date();
|
|
140
|
+
const date =
|
|
141
|
+
d.getFullYear() +
|
|
142
|
+
"-" +
|
|
143
|
+
String(d.getMonth() + 1).padStart(2, "0") +
|
|
144
|
+
"-" +
|
|
145
|
+
String(d.getDate()).padStart(2, "0");
|
|
146
|
+
const time =
|
|
147
|
+
String(d.getHours()).padStart(2, "0") +
|
|
148
|
+
":" +
|
|
149
|
+
String(d.getMinutes()).padStart(2, "0") +
|
|
150
|
+
":" +
|
|
151
|
+
String(d.getSeconds()).padStart(2, "0");
|
|
74
152
|
return `[${date} ${time}] `;
|
|
75
153
|
}
|
|
76
154
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hackthedev/terminal-logger",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.1",
|
|
4
4
|
"description": "This small library was designed for NodeJS and to bring pretty logs to the terminal. It has the following features:",
|
|
5
5
|
"homepage": "https://github.com/NETWORK-Z-Dev/terminal-logger#readme",
|
|
6
6
|
"bugs": {
|