185 lines
5.1 KiB
JavaScript
185 lines
5.1 KiB
JavaScript
const { app, BrowserWindow, dialog, ipcMain, shell } = require('electron');
|
|
const path = require('path');
|
|
const fs = require('fs-extra');
|
|
|
|
const { processFolder } = require('./services/folderProcessor');
|
|
const { processZip } = require('./services/zipProcessor');
|
|
const { resolveUnroutedDir, listUnroutedFiles } = require('./services/unrouted');
|
|
|
|
const CAD_EXTENSIONS = ['prt', 'asm', 'dwr'];
|
|
const DEFAULT_DESTINATION = './output/cad';
|
|
const SETTINGS_FILENAME = 'cad-router-settings.json';
|
|
const LINUX_RUNTIME_DIR = '.cadroute';
|
|
|
|
function buildConfig(destination) {
|
|
const resolvedDestination = String(destination || '').trim() || DEFAULT_DESTINATION;
|
|
return {
|
|
destination: resolvedDestination,
|
|
rules: CAD_EXTENSIONS.map((ext) => ({ ext, destination: resolvedDestination })),
|
|
};
|
|
}
|
|
|
|
function getSettingsPath() {
|
|
return path.join(getRuntimeDirectory(), SETTINGS_FILENAME);
|
|
}
|
|
|
|
function getRuntimeDirectory() {
|
|
if (process.platform === 'linux') {
|
|
return path.join(app.getPath('home'), LINUX_RUNTIME_DIR);
|
|
}
|
|
|
|
return app.getPath('userData');
|
|
}
|
|
|
|
async function ensureRuntimeDirectory() {
|
|
await fs.ensureDir(getRuntimeDirectory());
|
|
}
|
|
|
|
async function loadPersistedDestination() {
|
|
try {
|
|
const settingsPath = getSettingsPath();
|
|
if (!(await fs.pathExists(settingsPath))) {
|
|
return DEFAULT_DESTINATION;
|
|
}
|
|
|
|
const settings = await fs.readJson(settingsPath);
|
|
const destination = String(settings?.destination || '').trim();
|
|
return destination || DEFAULT_DESTINATION;
|
|
} catch {
|
|
return DEFAULT_DESTINATION;
|
|
}
|
|
}
|
|
|
|
async function persistDestination(destination) {
|
|
const settingsPath = getSettingsPath();
|
|
await fs.ensureDir(path.dirname(settingsPath));
|
|
await fs.writeJson(settingsPath, { destination }, { spaces: 2 });
|
|
}
|
|
|
|
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(async () => {
|
|
await ensureRuntimeDirectory();
|
|
const persistedDestination = await loadPersistedDestination();
|
|
config = buildConfig(persistedDestination);
|
|
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('process-dropped-path', async (_event, payload) => {
|
|
const droppedPath = String(payload?.path || '').trim();
|
|
if (!droppedPath) {
|
|
throw new Error('Percorso non valido');
|
|
}
|
|
|
|
const stats = await fs.stat(droppedPath).catch(() => null);
|
|
if (!stats) {
|
|
throw new Error('Percorso non trovato');
|
|
}
|
|
|
|
if (stats.isDirectory()) {
|
|
const routingResult = await processFolder(droppedPath, config);
|
|
return { canceled: false, sourceType: 'folder', ...routingResult };
|
|
}
|
|
|
|
if (stats.isFile() && path.extname(droppedPath).toLowerCase() === '.zip') {
|
|
const routingResult = await processZip(droppedPath, config);
|
|
return { canceled: false, sourceType: 'zip', ...routingResult };
|
|
}
|
|
|
|
throw new Error('Trascina una cartella o un file .zip');
|
|
});
|
|
|
|
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);
|
|
await persistDestination(config.destination);
|
|
return { destination: config.destination };
|
|
});
|
|
|
|
ipcMain.handle('open-unrouted-folder', async () => {
|
|
const unroutedDir = await resolveUnroutedDir();
|
|
const openResult = await shell.openPath(unroutedDir);
|
|
|
|
if (openResult) {
|
|
throw new Error(`Impossibile aprire la cartella: ${openResult}`);
|
|
}
|
|
|
|
return { path: unroutedDir };
|
|
});
|
|
|
|
ipcMain.handle('list-unrouted-files', async () => listUnroutedFiles());
|