-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvite-plugin-mita-telemetry.js
More file actions
53 lines (45 loc) · 1.53 KB
/
Copy pathvite-plugin-mita-telemetry.js
File metadata and controls
53 lines (45 loc) · 1.53 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
// @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',
// 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');
}
}
};
}