feat: persiste la destinazione output in un file settings locale
This commit is contained in:
32
main.js
32
main.js
@@ -1,11 +1,13 @@
|
||||
const { app, BrowserWindow, dialog, ipcMain } = require('electron');
|
||||
const path = require('path');
|
||||
const fs = require('fs-extra');
|
||||
|
||||
const { processFolder } = require('./services/folderProcessor');
|
||||
const { processZip } = require('./services/zipProcessor');
|
||||
|
||||
const CAD_EXTENSIONS = ['prt', 'asm', 'dwr'];
|
||||
const DEFAULT_DESTINATION = './output/cad';
|
||||
const SETTINGS_FILENAME = 'cad-router-settings.json';
|
||||
|
||||
function buildConfig(destination) {
|
||||
const resolvedDestination = String(destination || '').trim() || DEFAULT_DESTINATION;
|
||||
@@ -15,6 +17,31 @@ function buildConfig(destination) {
|
||||
};
|
||||
}
|
||||
|
||||
function getSettingsPath() {
|
||||
return path.join(app.getPath('userData'), SETTINGS_FILENAME);
|
||||
}
|
||||
|
||||
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() {
|
||||
@@ -31,7 +58,9 @@ function createWindow() {
|
||||
win.loadFile(path.join(__dirname, 'renderer', 'index.html'));
|
||||
}
|
||||
|
||||
app.whenReady().then(() => {
|
||||
app.whenReady().then(async () => {
|
||||
const persistedDestination = await loadPersistedDestination();
|
||||
config = buildConfig(persistedDestination);
|
||||
createWindow();
|
||||
|
||||
app.on('activate', () => {
|
||||
@@ -98,5 +127,6 @@ ipcMain.handle('update-destination', async (_event, payload) => {
|
||||
}
|
||||
|
||||
config = buildConfig(destination);
|
||||
await persistDestination(config.destination);
|
||||
return { destination: config.destination };
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user