-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
499 lines (456 loc) · 13.4 KB
/
main.js
File metadata and controls
499 lines (456 loc) · 13.4 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
const { app, BrowserWindow, Menu, dialog, ipcMain } = require('electron');
const path = require('path');
const fs = require('fs');
// Usamos el servidor proxy alternativo que no depende de http-proxy-middleware
const { startProxyServer } = require('./proxy-server');
// Variable para almacenar la ventana principal
let mainWindow;
// Ruta del archivo actualmente abierto
let currentFilePath = null;
// Puerto del servidor proxy
let proxyPort = 9000;
// Historial de archivos recientes (máximo 10)
let recentFiles = [];
const MAX_RECENT_FILES = 10;
// Lista de archivos favoritos
let favoriteFiles = [];
// Directorio de datos de la aplicación para configuraciones
const appDataPath = app.getPath('userData');
const configPath = path.join(appDataPath, 'config.json');
const tempSavePath = path.join(appDataPath, 'temp-autosave.json');
// Cargar configuración
function loadConfig() {
try {
if (fs.existsSync(configPath)) {
const config = JSON.parse(fs.readFileSync(configPath, 'utf8'));
if (config.recentFiles && Array.isArray(config.recentFiles)) {
recentFiles = config.recentFiles.filter(file => fs.existsSync(file));
}
if (config.favoriteFiles && Array.isArray(config.favoriteFiles)) {
favoriteFiles = config.favoriteFiles.filter(file => fs.existsSync(file));
}
return config;
}
} catch (error) {
console.error('Error al cargar la configuración:', error);
}
return { recentFiles: [], favoriteFiles: [] };
}
// Guardar configuración
function saveConfig() {
try {
const config = {
recentFiles: recentFiles,
favoriteFiles: favoriteFiles,
};
fs.writeFileSync(configPath, JSON.stringify(config, null, 2));
} catch (error) {
console.error('Error al guardar la configuración:', error);
}
}
// Añadir un archivo al historial de recientes
function addRecentFile(filePath) {
// Eliminar el archivo de la lista si ya existe
recentFiles = recentFiles.filter(file => file !== filePath);
// Añadir el archivo al principio de la lista
recentFiles.unshift(filePath);
// Limitar la lista al máximo de archivos
if (recentFiles.length > MAX_RECENT_FILES) {
recentFiles = recentFiles.slice(0, MAX_RECENT_FILES);
}
// Guardar la configuración actualizada
saveConfig();
// Actualizar el menú
updateRecentFilesMenu();
}
// Añadir un archivo a favoritos
function addToFavorites(filePath) {
// Verificar si el archivo ya está en favoritos
if (!favoriteFiles.includes(filePath)) {
favoriteFiles.push(filePath);
saveConfig();
updateRecentFilesMenu();
return true;
}
return false;
}
// Eliminar un archivo de favoritos
function removeFromFavorites(filePath) {
const index = favoriteFiles.indexOf(filePath);
if (index !== -1) {
favoriteFiles.splice(index, 1);
saveConfig();
updateRecentFilesMenu();
return true;
}
return false;
}
// Verificar si un archivo está en favoritos
function isInFavorites(filePath) {
return favoriteFiles.includes(filePath);
}
// Actualizar el menú de archivos recientes
function updateRecentFilesMenu() {
if (!mainWindow) return;
const template = buildMenuTemplate();
const menu = Menu.buildFromTemplate(template);
Menu.setApplicationMenu(menu);
}
// Construir la plantilla del menú
function buildMenuTemplate() {
// Construir el submenú de archivos recientes
const recentFilesSubmenu = recentFiles.map((file, index) => {
return {
label: `${index + 1}: ${path.basename(file)}`,
click: () => openSpecificFile(file),
};
});
// Si no hay archivos recientes, añadir un elemento deshabilitado
if (recentFilesSubmenu.length === 0) {
recentFilesSubmenu.push({
label: '(No hay archivos recientes)',
enabled: false,
});
} else {
// Añadir un separador y una opción para limpiar
recentFilesSubmenu.push(
{ type: 'separator' },
{
label: 'Limpiar archivos recientes',
click: () => {
recentFiles = [];
saveConfig();
updateRecentFilesMenu();
},
}
);
}
// Construir el submenú de favoritos
const favoritesSubmenu = favoriteFiles.map(file => {
return {
label: path.basename(file),
click: () => openSpecificFile(file),
};
});
// Si no hay favoritos, añadir un elemento deshabilitado
if (favoritesSubmenu.length === 0) {
favoritesSubmenu.push({
label: '(No hay favoritos)',
enabled: false,
});
}
return [
{
label: 'Archivo',
submenu: [
{
label: 'Nuevo',
accelerator: 'CmdOrCtrl+N',
click: () => {
mainWindow.webContents.send('new-file');
currentFilePath = null;
},
},
{
label: 'Abrir',
accelerator: 'CmdOrCtrl+O',
click: () => openFile(),
},
{
label: 'Archivos recientes',
submenu: recentFilesSubmenu,
},
{
label: 'Favoritos',
submenu: favoritesSubmenu,
},
{ type: 'separator' },
{
label: 'Guardar',
accelerator: 'CmdOrCtrl+S',
click: () => {
if (currentFilePath) {
mainWindow.webContents.send('save-file', currentFilePath);
} else {
saveFileAs();
}
},
},
{
label: 'Guardar como',
accelerator: 'CmdOrCtrl+Shift+S',
click: () => saveFileAs(),
},
{ type: 'separator' },
{
label: 'Salir',
accelerator: 'CmdOrCtrl+Q',
click: () => app.quit(),
},
],
},
{
label: 'Editar',
submenu: [
{ role: 'undo' },
{ role: 'redo' },
{ type: 'separator' },
{ role: 'cut' },
{ role: 'copy' },
{ role: 'paste' },
],
},
{
label: 'Vista',
submenu: [
{ role: 'reload' },
{ role: 'forceReload' },
{ role: 'toggleDevTools' },
{ type: 'separator' },
{ role: 'resetZoom' },
{ role: 'zoomIn' },
{ role: 'zoomOut' },
{ type: 'separator' },
{ role: 'togglefullscreen' },
],
},
{
label: 'OpenAPI',
submenu: [
{
label: 'Validar',
click: () => {
mainWindow.webContents.send('validate-openapi');
},
},
{
label: 'Convertir JSON a YAML',
click: () => {
mainWindow.webContents.send('convert-to-yaml');
},
},
{
label: 'Convertir YAML a JSON',
click: () => {
mainWindow.webContents.send('convert-to-json');
},
},
],
},
{
label: 'Favoritos',
submenu: [
{
label: currentFilePath
? isInFavorites(currentFilePath)
? 'Quitar de favoritos'
: 'Añadir a favoritos'
: 'Añadir a favoritos',
enabled: currentFilePath !== null,
click: () => {
if (currentFilePath) {
if (isInFavorites(currentFilePath)) {
removeFromFavorites(currentFilePath);
mainWindow.webContents.send('favorite-status', false);
} else {
addToFavorites(currentFilePath);
mainWindow.webContents.send('favorite-status', true);
}
}
},
},
],
},
];
}
function createWindow() {
// Crear la ventana del navegador
mainWindow = new BrowserWindow({
width: 1200,
height: 800,
webPreferences: {
nodeIntegration: false,
contextIsolation: true,
preload: path.join(__dirname, 'preload.js'),
webSecurity: false, // Deshabilitar la seguridad web para permitir peticiones CORS
},
});
// Permitir CORS
mainWindow.webContents.session.webRequest.onBeforeSendHeaders((details, callback) => {
callback({
requestHeaders: {
...details.requestHeaders,
Origin: '*',
},
});
});
mainWindow.webContents.session.webRequest.onHeadersReceived((details, callback) => {
callback({
responseHeaders: {
...details.responseHeaders,
'Access-Control-Allow-Origin': ['*'],
'Access-Control-Allow-Methods': ['GET, POST, PUT, DELETE, OPTIONS'],
'Access-Control-Allow-Headers': ['Content-Type, Authorization'],
},
});
});
// Cargar el archivo HTML principal
mainWindow.loadFile('index.html');
// Configurar el menú de la aplicación
const template = buildMenuTemplate();
const menu = Menu.buildFromTemplate(template);
Menu.setApplicationMenu(menu);
}
// Función para abrir un archivo específico
function openSpecificFile(filePath) {
fs.readFile(filePath, 'utf8', (err, data) => {
if (err) {
dialog.showErrorBox('Error al abrir el archivo', err.message);
return;
}
currentFilePath = filePath;
mainWindow.webContents.send('file-opened', { filePath, content: data });
// Notificar al renderer si está en favoritos
mainWindow.webContents.send('favorite-status', isInFavorites(filePath));
addRecentFile(filePath);
});
}
// Función para abrir un archivo
function openFile() {
dialog
.showOpenDialog(mainWindow, {
properties: ['openFile'],
filters: [
{ name: 'OpenAPI Files', extensions: ['json', 'yaml', 'yml'] },
{ name: 'All Files', extensions: ['*'] },
],
})
.then(result => {
if (!result.canceled && result.filePaths.length > 0) {
const filePath = result.filePaths[0];
openSpecificFile(filePath);
}
})
.catch(err => {
dialog.showErrorBox('Error al abrir el archivo', err.message);
});
}
// Función para guardar un archivo con diálogo "Guardar como"
function saveFileAs() {
dialog
.showSaveDialog(mainWindow, {
filters: [
{ name: 'JSON', extensions: ['json'] },
{ name: 'YAML', extensions: ['yaml', 'yml'] },
],
})
.then(result => {
if (!result.canceled) {
currentFilePath = result.filePath;
mainWindow.webContents.send('save-file', result.filePath);
addRecentFile(result.filePath);
}
})
.catch(err => {
dialog.showErrorBox('Error al guardar el archivo', err.message);
});
}
// Manejar el evento de guardar archivo desde el renderer
ipcMain.on('save-file-content', (event, { filePath, content }) => {
fs.writeFile(filePath, content, err => {
if (err) {
dialog.showErrorBox('Error al guardar el archivo', err.message);
return;
}
// Añadir a archivos recientes
addRecentFile(filePath);
// Mostrar una notificación de que se guardó correctamente
mainWindow.webContents.send('file-saved', filePath);
});
});
// Manejar el evento de mostrar errores en la validación
ipcMain.on('validation-error', (event, errorMessage) => {
dialog.showErrorBox('Error de validación', errorMessage);
});
// Manejar el evento de exportar Swagger
ipcMain.on('export-swagger', (event, options) => {
dialog
.showSaveDialog(mainWindow, options)
.then(result => {
if (!result.canceled) {
mainWindow.webContents.send('export-path', result.filePath);
}
})
.catch(err => {
dialog.showErrorBox('Error al exportar', err.message);
});
});
// Manejar el autoguardado temporal
ipcMain.on('auto-save-temp', (event, content) => {
try {
// Guardar el contenido en un archivo temporal
fs.writeFileSync(
tempSavePath,
JSON.stringify({
timestamp: new Date().toISOString(),
content: content,
})
);
} catch (error) {
console.error('Error en autoguardado temporal:', error);
}
});
// Manejar el toggle de favorito
ipcMain.on('toggle-favorite', event => {
if (currentFilePath) {
if (isInFavorites(currentFilePath)) {
removeFromFavorites(currentFilePath);
mainWindow.webContents.send('favorite-status', false);
} else {
addToFavorites(currentFilePath);
mainWindow.webContents.send('favorite-status', true);
}
}
});
// Comprobar si hay un autoguardado temporal al iniciar
function checkForTempSave() {
try {
if (fs.existsSync(tempSavePath)) {
const tempData = JSON.parse(fs.readFileSync(tempSavePath, 'utf8'));
// Enviar datos al renderer
mainWindow.webContents.send('temp-save-found', tempData);
}
} catch (error) {
console.error('Error al comprobar autoguardado temporal:', error);
}
}
// Cargar la configuración al inicio
loadConfig();
// Cuando la aplicación esté lista
app.whenReady().then(async () => {
try {
// Iniciar el servidor proxy
const { port, addresses } = await startProxyServer();
proxyPort = port;
// Enviar el puerto del proxy al proceso de renderizado
createWindow();
// Verificar si hay autoguardados temporales
mainWindow.webContents.on('did-finish-load', () => {
// Enviar el puerto y las direcciones disponibles
mainWindow.webContents.send('proxy-info', { port, addresses });
checkForTempSave();
});
} catch (error) {
console.error('Error al iniciar el servidor proxy:', error);
createWindow();
}
app.on('activate', function () {
// En macOS es común volver a crear una ventana cuando
// se hace clic en el icono del dock y no hay otras ventanas abiertas.
if (BrowserWindow.getAllWindows().length === 0) createWindow();
});
});
// Salir cuando todas las ventanas estén cerradas, excepto en macOS
app.on('window-all-closed', function () {
if (process.platform !== 'darwin') app.quit();
});