@kwirthmagnify/kwirth-sender-console 0.1.2 → 0.1.4

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/README.md ADDED
@@ -0,0 +1,77 @@
1
+ # Console Sender
2
+
3
+ Sender que escribe los mensajes directamente en la salida estándar del proceso Node.js. Colorea las líneas según el nivel del mensaje usando códigos ANSI y permite configurar prefijo, timestamps y etiquetas de nivel por config.
4
+
5
+ ## Cuándo usarlo
6
+
7
+ - Desarrollo local: ver alertas en el terminal donde corre Kwirth.
8
+ - Diagnóstico rápido sin necesidad de infraestructura adicional.
9
+ - Combinado con `sender-tee` para tener salida a consola y a otro destino a la vez.
10
+
11
+ ## Configuración
12
+
13
+ | Campo | Tipo | Requerido | Por defecto | Descripción |
14
+ |--------------|-----------|-----------|-------------|--------------------------------------------------|
15
+ | `name` | `string` | Sí | — | Nombre de esta config |
16
+ | `prefix` | `string` | No | — | Texto que se antepone a cada línea, ej. `[APP]` |
17
+ | `timestamps` | `boolean` | No | `true` | Incluir timestamp ISO en cada línea |
18
+ | `levels` | `boolean` | No | `true` | Incluir etiqueta de nivel `[INFO]`, `[ERROR]`… |
19
+
20
+ ## Colores por nivel
21
+
22
+ | Nivel | Color |
23
+ |-----------|----------|
24
+ | `debug` | Cian |
25
+ | `info` | Verde |
26
+ | `warning` | Amarillo |
27
+ | `error` | Rojo |
28
+
29
+ ## Ejemplos
30
+
31
+ ### Config mínima
32
+
33
+ ```json
34
+ {
35
+ "name": "default"
36
+ }
37
+ ```
38
+
39
+ Salida:
40
+ ```
41
+ [2024-01-15T10:30:00.000Z] [INFO] Pod nginx started
42
+ ```
43
+
44
+ ### Con prefijo y sin timestamps
45
+
46
+ ```json
47
+ {
48
+ "name": "app-alerts",
49
+ "prefix": "[KWIRTH]",
50
+ "timestamps": false
51
+ }
52
+ ```
53
+
54
+ Salida:
55
+ ```
56
+ [KWIRTH] [ERROR] CrashLoopBackOff detected on pod api-7f9d
57
+ ```
58
+
59
+ ### Config silenciosa (solo el body)
60
+
61
+ ```json
62
+ {
63
+ "name": "clean",
64
+ "timestamps": false,
65
+ "levels": false
66
+ }
67
+ ```
68
+
69
+ Salida:
70
+ ```
71
+ CrashLoopBackOff detected on pod api-7f9d
72
+ ```
73
+
74
+ ## Notas
75
+
76
+ - Los mensajes de nivel `error` se envían a `console.error` (stderr); `warning` a `console.warn`; el resto a `console.log`.
77
+ - `message.to` se añade al final de la línea como `→ destinatario` si está presente.
package/build.mjs ADDED
@@ -0,0 +1,28 @@
1
+ import esbuild from 'esbuild'
2
+ import fs from 'fs'
3
+ import path from 'path'
4
+
5
+ fs.mkdirSync('dist', { recursive: true })
6
+
7
+ await esbuild.build({
8
+ entryPoints: ['src/back/index.ts'],
9
+ bundle: true,
10
+ format: 'cjs',
11
+ platform: 'node',
12
+ target: 'node20',
13
+ outfile: 'dist/back.js',
14
+ loader: { '.ts': 'ts' },
15
+ minify: false,
16
+ })
17
+ console.log('Built dist/back.js')
18
+
19
+ const meta = JSON.parse(fs.readFileSync('package.json', 'utf-8'))
20
+ const distMeta = {
21
+ id: meta.id,
22
+ name: meta.name,
23
+ displayName: meta.displayName,
24
+ version: meta.version,
25
+ description: meta.description,
26
+ }
27
+ fs.writeFileSync(path.join('dist', 'package.json'), JSON.stringify(distMeta, null, 2))
28
+ console.log('Wrote dist/package.json')
@@ -0,0 +1,7 @@
1
+ {
2
+ "id": "console",
3
+ "name": "@kwirthmagnify/kwirth-sender-console",
4
+ "displayName": "Console Sender",
5
+ "version": "0.1.4",
6
+ "description": "Console sender plugin for Kwirth — writes messages to stdout, with configurable prefix, level and timestamps per named config"
7
+ }
package/package.json CHANGED
@@ -1,7 +1,21 @@
1
- {
2
- "id": "console",
3
- "name": "@kwirthmagnify/kwirth-sender-console",
4
- "displayName": "Console Sender",
5
- "version": "0.1.2",
6
- "description": "Console sender plugin for Kwirth — writes messages to stdout, with configurable prefix, level and timestamps per named config"
7
- }
1
+ {
2
+ "id": "console",
3
+ "name": "@kwirthmagnify/kwirth-sender-console",
4
+ "publisher": "@kwirthmagnify",
5
+ "version": "0.1.4",
6
+ "displayName": "Console Sender",
7
+ "description": "Console sender plugin for Kwirth — writes messages to stdout, with configurable prefix, level and timestamps per named config",
8
+ "type": "module",
9
+ "scripts": {
10
+ "build": "node build.mjs",
11
+ "watch": "node watch.mjs"
12
+ },
13
+ "dependencies": {
14
+ "@kwirthmagnify/kwirth-common-back": "^0.5.11"
15
+ },
16
+ "devDependencies": {
17
+ "@types/node": "^20.12.13",
18
+ "esbuild": "^0.27.2",
19
+ "typescript": "^5.4.0"
20
+ }
21
+ }
@@ -0,0 +1,82 @@
1
+ import { ISender, ISenderAccess, ISenderConfig, ISenderFieldDef, ISenderMessage } from '@kwirthmagnify/kwirth-common-back'
2
+
3
+ // ─── Config ────────────────────────────────────────────────────────────────────
4
+
5
+ export interface IConsoleSenderConfig extends ISenderConfig {
6
+ name: string
7
+ prefix?: string // string prepended to every line, e.g. '[KWIRTH]'
8
+ timestamps?: boolean // include ISO timestamp (default: true)
9
+ levels?: boolean // include level tag like [ERROR] (default: true)
10
+ }
11
+
12
+ const LEVEL_COLORS: Record<string, string> = {
13
+ debug: '\x1b[36m', // cyan
14
+ info: '\x1b[32m', // green
15
+ warning: '\x1b[33m', // yellow
16
+ error: '\x1b[31m', // red
17
+ }
18
+ const RESET = '\x1b[0m'
19
+
20
+ // ─── Sender ────────────────────────────────────────────────────────────────────
21
+
22
+ export class ConsoleSender implements ISender {
23
+ readonly id = 'console'
24
+ private configs = new Map<string, IConsoleSenderConfig>()
25
+
26
+ addConfig(config: ISenderConfig): void {
27
+ this.configs.set(config.name, config as IConsoleSenderConfig)
28
+ }
29
+
30
+ removeConfig(name: string): void {
31
+ this.configs.delete(name)
32
+ }
33
+
34
+ hasConfig(name: string): boolean {
35
+ return this.configs.has(name)
36
+ }
37
+
38
+ getConfigNames(): string[] {
39
+ return Array.from(this.configs.keys())
40
+ }
41
+
42
+ async send(configName: string, message: ISenderMessage): Promise<void> {
43
+ const config = this.configs.get(configName)
44
+ if (!config) throw new Error(`ConsoleSender: config '${configName}' not found`)
45
+
46
+ const useTimestamps = config.timestamps ?? true
47
+ const useLevels = config.levels ?? true
48
+ const prefix = config.prefix ? `${config.prefix} ` : ''
49
+
50
+ const ts = useTimestamps ? `[${new Date().toISOString()}] ` : ''
51
+ const level = message.level ?? 'info'
52
+ const lvTag = useLevels ? `[${level.toUpperCase()}] ` : ''
53
+ const color = LEVEL_COLORS[level] ?? ''
54
+
55
+ const subject = message.subject ? `${message.subject}: ` : ''
56
+ const to = message.to ? ` → ${Array.isArray(message.to) ? message.to.join(', ') : message.to}` : ''
57
+
58
+ const line = `${color}${ts}${prefix}${lvTag}${subject}${message.body}${to}${RESET}`
59
+
60
+ if (level === 'error') {
61
+ console.error(line)
62
+ } else if (level === 'warning') {
63
+ console.warn(line)
64
+ } else {
65
+ console.log(line)
66
+ }
67
+ }
68
+
69
+ getConfigSchema(): ISenderFieldDef[] {
70
+ return [
71
+ { name: 'name', label: 'Name', required: true },
72
+ { name: 'prefix', label: 'Prefix' },
73
+ { name: 'timestamps', label: 'Timestamps', type: 'boolean' },
74
+ { name: 'levels', label: 'Levels', type: 'boolean' },
75
+ ]
76
+ }
77
+
78
+ async startSender(_senders: ISenderAccess): Promise<void> {}
79
+ async stopSender(): Promise<void> {}
80
+ }
81
+
82
+ export default ConsoleSender
package/tsconfig.json ADDED
@@ -0,0 +1,13 @@
1
+ {
2
+ "compilerOptions": {
3
+ "target": "ES2020",
4
+ "module": "ESNext",
5
+ "moduleResolution": "bundler",
6
+ "strict": true,
7
+ "lib": ["ES2020"],
8
+ "types": ["node"],
9
+ "skipLibCheck": true,
10
+ "esModuleInterop": true
11
+ },
12
+ "include": ["src"]
13
+ }
package/watch.mjs ADDED
@@ -0,0 +1,20 @@
1
+ import esbuild from 'esbuild'
2
+ import fs from 'fs'
3
+ import path from 'path'
4
+
5
+ fs.mkdirSync('dist', { recursive: true })
6
+ const meta = JSON.parse(fs.readFileSync('package.json', 'utf-8'))
7
+ fs.writeFileSync(path.join('dist', 'package.json'), JSON.stringify({ id: meta.id, name: meta.name, displayName: meta.displayName, version: meta.version, description: meta.description }, null, 2))
8
+
9
+ const ctx = await esbuild.context({
10
+ entryPoints: ['src/back/index.ts'],
11
+ bundle: true,
12
+ format: 'cjs',
13
+ platform: 'node',
14
+ target: 'node20',
15
+ outfile: 'dist/back.js',
16
+ loader: { '.ts': 'ts' },
17
+ minify: false,
18
+ })
19
+ await ctx.watch()
20
+ console.log('[watch] Watching src/ — back.js rebuilds on every change.')
File without changes