Files
cad-data-router/main.js

103 lines
2.6 KiB
JavaScript

const { app, BrowserWindow, dialog, ipcMain } = require('electron');
const path = require('path');
const { processFolder } = require('./services/folderProcessor');
const { processZip } = require('./services/zipProcessor');
const CAD_EXTENSIONS = ['prt', 'asm', 'dwr'];
const DEFAULT_DESTINATION = './output/cad';
function buildConfig(destination) {
const resolvedDestination = String(destination || '').trim() || DEFAULT_DESTINATION;
return {
destination: resolvedDestination,
rules: CAD_EXTENSIONS.map((ext) => ({ ext, destination: resolvedDestination })),
};
}
let config = buildConfig(DEFAULT_DESTINATION);
function createWindow() {
const win = new BrowserWindow({
width: 900,
height: 640,
webPreferences: {
preload: path.join(__dirname, 'preload.js'),
contextIsolation: true,
nodeIntegration: false,
},
});
win.loadFile(path.join(__dirname, 'renderer', 'index.html'));
}
app.whenReady().then(() => {
createWindow();
app.on('activate', () => {
if (BrowserWindow.getAllWindows().length === 0) {
createWindow();
}
});
});
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') app.quit();
});
ipcMain.handle('select-folder', async () => {
const result = await dialog.showOpenDialog({
properties: ['openDirectory'],
});
if (result.canceled || !result.filePaths[0]) {
return { canceled: true };
}
const routingResult = await processFolder(result.filePaths[0], config);
return { canceled: false, ...routingResult };
});
ipcMain.handle('select-zip', async () => {
const result = await dialog.showOpenDialog({
properties: ['openFile'],
filters: [{ name: 'Zip', extensions: ['zip'] }],
});
if (result.canceled || !result.filePaths[0]) {
return { canceled: true };
}
const routingResult = await processZip(result.filePaths[0], config);
return { canceled: false, ...routingResult };
});
ipcMain.handle('get-destination', async () => ({
destination: config.destination || DEFAULT_DESTINATION,
}));
ipcMain.handle('select-destination-folder', async () => {
const result = await dialog.showOpenDialog({
properties: ['openDirectory'],
});
if (result.canceled || !result.filePaths[0]) {
return { canceled: true };
}
return {
canceled: false,
path: result.filePaths[0],
};
});
ipcMain.handle('update-destination', async (_event, payload) => {
const destination = String(payload?.destination || '').trim();
if (!destination) {
throw new Error('La destinazione non puo essere vuota');
}
config = buildConfig(destination);
return { destination: config.destination };
});