discord-logify 0.1.5 → 0.1.6
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/dist/bin/cli.cjs +2 -6
- package/dist/bin/cli.js +1 -1
- package/dist/chunk-BAGWC7X3.js +156 -0
- package/dist/src/index.cjs +0 -4
- package/dist/src/index.js +1 -1
- package/package.json +1 -1
- package/scripts/postinstall.js +21 -33
package/dist/bin/cli.cjs
CHANGED
|
@@ -179,10 +179,6 @@ var log = class {
|
|
|
179
179
|
}
|
|
180
180
|
};
|
|
181
181
|
|
|
182
|
-
// src/index.ts
|
|
183
|
-
var logger = new log();
|
|
184
|
-
logger.Alert("aaaa");
|
|
185
|
-
|
|
186
182
|
// src/commands/setup.ts
|
|
187
183
|
var import_node_fs2 = __toESM(require("fs"), 1);
|
|
188
184
|
var import_node_path = __toESM(require("path"), 1);
|
|
@@ -224,7 +220,7 @@ function toggleLogFile() {
|
|
|
224
220
|
|
|
225
221
|
// bin/cli.ts
|
|
226
222
|
var program = new import_commander.Command();
|
|
227
|
-
var
|
|
223
|
+
var logger = new log();
|
|
228
224
|
program.name("logify").description("Advanced logging system for Discord").version("0.1.3");
|
|
229
225
|
program.command("setup <url>").description("Setup Discord webhook credentials").action((url) => {
|
|
230
226
|
const isCreated = setup(url);
|
|
@@ -235,7 +231,7 @@ program.command("setup <url>").description("Setup Discord webhook credentials").
|
|
|
235
231
|
}
|
|
236
232
|
});
|
|
237
233
|
program.command("add-log <prefix>").description("Add a custom log level").requiredOption("-c, --color <code>", "ANSI color code (e.g., 32m for green)").requiredOption("-t, --tag <tag>", "Log tag (e.g., [SUCCESS])").action((prefix, options) => {
|
|
238
|
-
|
|
234
|
+
logger.addLogLevel(prefix, options.color, options.tag);
|
|
239
235
|
console.log(` Log level "${prefix}" added successfully!`);
|
|
240
236
|
});
|
|
241
237
|
program.command("logFile").description("Toggle file logging on/off").action(() => {
|
package/dist/bin/cli.js
CHANGED
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
// src/index.ts
|
|
2
|
+
import "dotenv/config";
|
|
3
|
+
|
|
4
|
+
// src/core/logger.ts
|
|
5
|
+
import { existsSync, writeFileSync, readFileSync, appendFile } from "fs";
|
|
6
|
+
import * as path from "path";
|
|
7
|
+
|
|
8
|
+
// src/core/webhook.ts
|
|
9
|
+
async function send(log2) {
|
|
10
|
+
const msg = await fetch(`${process.env.WEBHOOK_URL}?wait=true`, {
|
|
11
|
+
method: "POST",
|
|
12
|
+
headers: { "Content-Type": "application/json" },
|
|
13
|
+
body: JSON.stringify({
|
|
14
|
+
content: "```ansi\n" + log2 + "\n```",
|
|
15
|
+
username: "logify"
|
|
16
|
+
})
|
|
17
|
+
});
|
|
18
|
+
if (!msg.ok) {
|
|
19
|
+
const errorText = await msg.text();
|
|
20
|
+
throw new Error(`Discord webhook error: ${msg.status} - ${errorText}`);
|
|
21
|
+
}
|
|
22
|
+
const response = await msg.json();
|
|
23
|
+
return response.id;
|
|
24
|
+
}
|
|
25
|
+
async function edit(id, log2) {
|
|
26
|
+
const url = `${process.env.WEBHOOK_URL}/messages/${id}`;
|
|
27
|
+
const response = await fetch(url, {
|
|
28
|
+
method: "PATCH",
|
|
29
|
+
headers: { "Content-Type": "application/json" },
|
|
30
|
+
body: JSON.stringify({
|
|
31
|
+
content: "```ansi\n" + log2 + "\n```"
|
|
32
|
+
})
|
|
33
|
+
});
|
|
34
|
+
if (!response.ok) {
|
|
35
|
+
const errorText = await response.text();
|
|
36
|
+
throw new Error(`Discord webhook error: ${response.status} - ${errorText}`);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
// src/core/logger.ts
|
|
41
|
+
var buffer = [];
|
|
42
|
+
var timer = null;
|
|
43
|
+
var lastMessageId = null;
|
|
44
|
+
var timeOut = 1e3;
|
|
45
|
+
var log = class {
|
|
46
|
+
logFile = false;
|
|
47
|
+
configPath = path.join(process.cwd(), "logger-config.json");
|
|
48
|
+
logPath = path.join(process.cwd(), "logify.log");
|
|
49
|
+
config = /* @__PURE__ */ new Map([]);
|
|
50
|
+
constructor() {
|
|
51
|
+
this.loadConfig();
|
|
52
|
+
}
|
|
53
|
+
loadConfig() {
|
|
54
|
+
if (!existsSync(this.configPath)) {
|
|
55
|
+
writeFileSync(this.configPath, '{"logFile":false}');
|
|
56
|
+
} else {
|
|
57
|
+
const data = readFileSync(this.configPath, "utf-8");
|
|
58
|
+
const parsedData = JSON.parse(data);
|
|
59
|
+
this.config = new Map(Object.entries(parsedData));
|
|
60
|
+
const check = this.config.get("logFile");
|
|
61
|
+
this.logFile = typeof check === "boolean" ? check : false;
|
|
62
|
+
}
|
|
63
|
+
if (this.logFile && !existsSync(this.logPath)) {
|
|
64
|
+
writeFileSync(this.logPath, "");
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
date() {
|
|
68
|
+
const d = /* @__PURE__ */ new Date();
|
|
69
|
+
const day = String(d.getDate()).padStart(2, "0");
|
|
70
|
+
const month = String(d.getMonth() + 1).padStart(2, "0");
|
|
71
|
+
const year = d.getFullYear();
|
|
72
|
+
const hour = String(d.getHours()).padStart(2, "0");
|
|
73
|
+
const min = String(d.getMinutes()).padStart(2, "0");
|
|
74
|
+
const sec = String(d.getSeconds()).padStart(2, "0");
|
|
75
|
+
return `[${day}/${month}/${year} ${hour}:${min}:${sec}]`;
|
|
76
|
+
}
|
|
77
|
+
Info(msg) {
|
|
78
|
+
process.stdout.write(`${this.date()} \x1B[36m[INFO]\x1B[0m ${msg}
|
|
79
|
+
`);
|
|
80
|
+
buffer.push(`${this.date()} \x1B[36m[INFO]\x1B[0m ${msg}
|
|
81
|
+
`);
|
|
82
|
+
this.writeLogFile(`${this.date()} [INFO] ${msg}\\n`);
|
|
83
|
+
this.sendMessage();
|
|
84
|
+
}
|
|
85
|
+
Alert(msg) {
|
|
86
|
+
process.stdout.write(`${this.date()} \x1B[33m[WARN]\x1B[0m ${msg}
|
|
87
|
+
`);
|
|
88
|
+
buffer.push(`${this.date()} \x1B[33m[WARN]\x1B[0m ${msg}
|
|
89
|
+
`);
|
|
90
|
+
this.writeLogFile(`${this.date()} [WARN] ${msg}
|
|
91
|
+
`);
|
|
92
|
+
this.sendMessage();
|
|
93
|
+
}
|
|
94
|
+
Error(msg) {
|
|
95
|
+
process.stdout.write(`${this.date()} \x1B[31m[ERROR]\x1B[0m ${msg}
|
|
96
|
+
`);
|
|
97
|
+
buffer.push(`${this.date()} \x1B[31m[ERROR]\x1B[0m ${msg}
|
|
98
|
+
`);
|
|
99
|
+
this.writeLogFile(`${this.date()} [ERROR] ${msg}
|
|
100
|
+
`);
|
|
101
|
+
this.sendMessage();
|
|
102
|
+
}
|
|
103
|
+
Log(msg, prefix) {
|
|
104
|
+
const selected = this.config.get(prefix);
|
|
105
|
+
if (typeof selected === "object" && selected !== null) {
|
|
106
|
+
process.stdout.write(`${this.date()} \x1B[${selected.color}${selected.text}\x1B[0m ${msg}
|
|
107
|
+
`);
|
|
108
|
+
buffer.push(`${this.date()} \x1B[${selected.color}${selected.text}\x1B[0m ${msg}
|
|
109
|
+
`);
|
|
110
|
+
this.writeLogFile(`${this.date()} ${selected.text} ${msg}
|
|
111
|
+
`);
|
|
112
|
+
}
|
|
113
|
+
this.sendMessage();
|
|
114
|
+
}
|
|
115
|
+
addLogLevel(prefix, logColor, logTag) {
|
|
116
|
+
this.config.set(prefix, { color: logColor, prefix, text: logTag });
|
|
117
|
+
writeFileSync(this.configPath, JSON.stringify(Object.fromEntries(this.config)), "utf-8");
|
|
118
|
+
}
|
|
119
|
+
/**
|
|
120
|
+
* Send buffered messages to Discord
|
|
121
|
+
* Uses message editing to reduce spam
|
|
122
|
+
*/
|
|
123
|
+
async sendMessage() {
|
|
124
|
+
if (timer) clearTimeout(timer);
|
|
125
|
+
timer = setTimeout(async () => {
|
|
126
|
+
const payload = buffer.join("");
|
|
127
|
+
buffer = [];
|
|
128
|
+
if (!payload) return;
|
|
129
|
+
try {
|
|
130
|
+
if (lastMessageId) {
|
|
131
|
+
await edit(lastMessageId, payload);
|
|
132
|
+
} else {
|
|
133
|
+
lastMessageId = await send(payload);
|
|
134
|
+
}
|
|
135
|
+
} catch (error) {
|
|
136
|
+
console.error("Failed to send message to Discord:", error);
|
|
137
|
+
}
|
|
138
|
+
}, timeOut);
|
|
139
|
+
}
|
|
140
|
+
/**
|
|
141
|
+
* Write log to file if enabled
|
|
142
|
+
*/
|
|
143
|
+
writeLogFile(log2) {
|
|
144
|
+
if (this.logFile) {
|
|
145
|
+
appendFile(this.logPath, log2, (err) => {
|
|
146
|
+
if (err) {
|
|
147
|
+
console.error("Failed to write to log file:", err);
|
|
148
|
+
}
|
|
149
|
+
});
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
};
|
|
153
|
+
|
|
154
|
+
export {
|
|
155
|
+
log
|
|
156
|
+
};
|
package/dist/src/index.cjs
CHANGED
package/dist/src/index.js
CHANGED
package/package.json
CHANGED
package/scripts/postinstall.js
CHANGED
|
@@ -1,57 +1,45 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
*/
|
|
7
|
-
|
|
8
|
-
import { existsSync, appendFile, writeFileSync } from 'node:fs';
|
|
9
|
-
import { join } from 'node:path';
|
|
10
|
-
import { createInterface } from 'node:readline';
|
|
11
|
-
|
|
12
|
-
const envPath = join(process.cwd(), '.env');
|
|
13
|
-
|
|
3
|
+
import { existsSync, appendFile, writeFileSync } from "node:fs";
|
|
4
|
+
import { join } from "node:path";
|
|
5
|
+
import { createInterface } from "node:readline";
|
|
14
6
|
|
|
7
|
+
if (!process.stdin.isTTY) {
|
|
8
|
+
process.exit(0);
|
|
9
|
+
}
|
|
15
10
|
|
|
11
|
+
const projectRoot = process.env.INIT_CWD ?? process.cwd();
|
|
12
|
+
const envPath = join(projectRoot, ".env");
|
|
16
13
|
|
|
17
14
|
const rl = createInterface({
|
|
18
15
|
input: process.stdin,
|
|
19
16
|
output: process.stdout,
|
|
20
17
|
});
|
|
21
18
|
|
|
22
|
-
console.log(
|
|
23
|
-
console.log(
|
|
19
|
+
console.log("\n\x1b[34mWelcome to Discord Logify!\x1b[0m");
|
|
20
|
+
console.log("━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━");
|
|
21
|
+
console.log("Project root:", projectRoot);
|
|
24
22
|
|
|
25
|
-
rl.question(
|
|
23
|
+
rl.question("\n Enter your Discord webhook URL:\n> ", (webhookUrl) => {
|
|
26
24
|
if (!webhookUrl.trim()) {
|
|
27
|
-
console.log(
|
|
25
|
+
console.log("\nWebhook URL is required");
|
|
28
26
|
rl.close();
|
|
29
|
-
process.exit(
|
|
27
|
+
process.exit(0); // mejor no romper la instalación
|
|
30
28
|
}
|
|
31
29
|
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
30
|
const envContent = `WEBHOOK_URL=${webhookUrl}`;
|
|
36
31
|
|
|
37
|
-
if(existsSync(envPath)){
|
|
38
|
-
appendFile(envPath,
|
|
32
|
+
if (existsSync(envPath)) {
|
|
33
|
+
appendFile(envPath, "\n" + envContent, "utf-8", (err) => {
|
|
39
34
|
if (err) {
|
|
40
|
-
console.error(
|
|
41
|
-
process.exit(
|
|
35
|
+
console.error("Error writing to .env file:", err);
|
|
36
|
+
process.exit(0);
|
|
42
37
|
}
|
|
43
38
|
});
|
|
44
|
-
}else{
|
|
45
|
-
writeFileSync(envPath, envContent,
|
|
39
|
+
} else {
|
|
40
|
+
writeFileSync(envPath, envContent, "utf-8");
|
|
46
41
|
}
|
|
47
42
|
|
|
48
|
-
console.log(
|
|
49
|
-
console.log('\n \x1b[34m Next steps:');
|
|
50
|
-
console.log(' 1. Import: import { log } from "discord-logify"');
|
|
51
|
-
console.log(' 2. Create: const logger = new log()');
|
|
52
|
-
console.log(' 3. Log: logger.Info("Hello from Discord!")\x1b[0m');
|
|
53
|
-
console.log('\n Learn more: https://github.com/txuli/discord-logify');
|
|
54
|
-
console.log('━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n');
|
|
55
|
-
|
|
43
|
+
console.log("\n\x1b[32mWebhook configured successfully!\x1b[0m");
|
|
56
44
|
rl.close();
|
|
57
45
|
});
|