Repository navigation
Expand file tree
/
Copy pathvite-plugin-mita-telemetry.js
More file actions
79 lines (70 loc) · 2.46 KB
/
Copy pathvite-plugin-mita-telemetry.js
File metadata and controls
79 lines (70 loc) · 2.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
// @ts-check
import fs from 'fs';
import path from 'path';
/**
* 🛠️ Vite Plugin Mita Telemetry
* Intercepta los errores fatales durante el ciclo de vida del build (Rollup/Vite)
* y los guarda en un archivo `build-errors.json` para que el Dashboard pueda
* recogerlos y mostrarlos.
*/
export default function mitaTelemetryPlugin() {
return {
name: 'vite-plugin-mita-telemetry',
configureServer(server) {
server.middlewares.use('/__mita_fs_sync', (req, res) => {
if (req.method !== 'POST') {
res.statusCode = 405;
res.end(JSON.stringify({ ok: false, error: 'Method Not Allowed' }));
return;
}
let body = '';
req.on('data', (chunk) => {
body += chunk;
});
req.on('end', () => {
try {
const payload = JSON.parse(body || '{}');
const dbFile = path.resolve(process.cwd(), 'src/assets/mita-docs-db.json');
fs.writeFileSync(dbFile, JSON.stringify(payload, null, 2), 'utf-8');
res.setHeader('Content-Type', 'application/json');
res.end(JSON.stringify({ ok: true, archivo: 'src/assets/mita-docs-db.json' }));
} catch (err) {
res.statusCode = 500;
res.end(JSON.stringify({ ok: false, error: String(err) }));
}
});
});
},
// Capturamos cualquier error en la resolución o transformación
buildEnd(error) {
if (error) {
const errorData = {
timestamp: new Date().toISOString(),
nivel: 'ERROR',
mensaje: `[Build Error] ${error.message}`,
stackTrace: error.stack || JSON.stringify(error, null, 2),
url: 'Node (Vite Build)',
};
const targetDir = path.resolve(process.cwd(), 'public');
if (!fs.existsSync(targetDir)) {
fs.mkdirSync(targetDir, { recursive: true });
}
const logFile = path.resolve(targetDir, 'mita-build-errors.json');
let existingLogs = [];
if (fs.existsSync(logFile)) {
try {
existingLogs = JSON.parse(fs.readFileSync(logFile, 'utf-8'));
} catch (e) {
existingLogs = [];
}
}
existingLogs.push(errorData);
// Guardamos máximo los últimos 50 errores de build
if (existingLogs.length > 50) {
existingLogs.shift();
}
fs.writeFileSync(logFile, JSON.stringify(existingLogs, null, 2), 'utf-8');
}
}
};
}