Config-Funktionen in eigenes Modul, fixes #7
This commit is contained in:
@@ -1,166 +1,27 @@
|
||||
const { app, BrowserWindow, ipcMain, nativeTheme, Menu, MenuItem } = require('electron/main');
|
||||
const { app, BrowserWindow, nativeTheme, Menu, MenuItem } = require('electron/main');
|
||||
const path = require('node:path');
|
||||
const fs = require('fs');
|
||||
const os = require('os');
|
||||
const log = require('electron-log/main');
|
||||
const isReachable = require('is-reachable');
|
||||
const { setInterval } = require('node:timers/promises');
|
||||
const configModule = require('./js/config.js');
|
||||
const config = require('./js/config.js');
|
||||
const { Config } = require('./js/config.js');
|
||||
|
||||
const isMac = process.platform === 'darwin';
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Properties
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
let config;
|
||||
let configPath;
|
||||
let configWindow;
|
||||
let win;
|
||||
let menu;
|
||||
|
||||
const isMac = process.platform === 'darwin';
|
||||
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Function
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Loads a config file from disc.
|
||||
* The file is expected to be named `config.json` and is searched for in these
|
||||
* locations and in this order:
|
||||
*
|
||||
* 1. `/assets/presentation/config.json` (Tooloop OS)
|
||||
* 2. appData directory
|
||||
* 3. Path of the executable
|
||||
* - Linux: `app.getPath('exe')`
|
||||
* - MacOS: `path.resolve(app.getPath('exe'), "../../../../")`
|
||||
* 4. `__dirname` (Development)
|
||||
*/
|
||||
function loadConfig() {
|
||||
const locations = [
|
||||
'/assets/presentation',
|
||||
path.join(app.getPath('appData'), app.name),
|
||||
isMac ? path.resolve(app.getPath('exe'), "../../../../") : app.getPath('exe'),
|
||||
__dirname
|
||||
];
|
||||
|
||||
// Check all locations
|
||||
for (const location of locations) {
|
||||
// Update the filepath
|
||||
configPath = location;
|
||||
let configFile = path.join(configPath, 'config.json');
|
||||
try {
|
||||
// Try access
|
||||
fs.accessSync(configFile);
|
||||
|
||||
// Parse the file if found
|
||||
console.info('Found config file at ' + configFile);
|
||||
const data = fs.readFileSync(configFile, { encoding: 'utf8' });
|
||||
config = JSON.parse(data);
|
||||
|
||||
// Break the loop
|
||||
break;
|
||||
} catch (err) {
|
||||
console.warn('No config file found at ' + configPath);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function saveConfig(newConfig) {
|
||||
// If there was no config file before create a new object
|
||||
// and store a default save path
|
||||
if (!config) {
|
||||
config = {
|
||||
url: "",
|
||||
whitelist: [],
|
||||
logPath: ""
|
||||
};
|
||||
|
||||
try {
|
||||
configPath = fs.accessSync('/assets/presentation');
|
||||
} catch (error) {
|
||||
configPath = path.join(app.getPath('appData'), app.name);
|
||||
}
|
||||
}
|
||||
|
||||
// Store changes
|
||||
if (config.url != newConfig.url) {
|
||||
config.url = newConfig.url;
|
||||
loadUrlAsync(config.url);
|
||||
}
|
||||
|
||||
config.whitelist = [];
|
||||
let whitelist = newConfig.whitelist.split(";");
|
||||
whitelist = whitelist.filter(function (entry) { return entry.trim() != ''; });
|
||||
whitelist.forEach(
|
||||
token => {
|
||||
config.whitelist.push(token.replace(/\r?\n|\r/g, ""));
|
||||
}
|
||||
);
|
||||
|
||||
config.logPath = newConfig.logPath;
|
||||
|
||||
// Write file
|
||||
let configFile = path.join(configPath, 'config.json');
|
||||
fs.writeFile(
|
||||
configFile,
|
||||
JSON.stringify(config, null, " "),
|
||||
(error) => {
|
||||
if (error) log.warn('Error writing to ' + configPath, error);
|
||||
log.info("Saved config to " + configFile);
|
||||
}
|
||||
);
|
||||
|
||||
// Update UI with new values
|
||||
configWindow.webContents.send('update-config', config);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Creates a modal config view and attaches as to the main window
|
||||
*/
|
||||
function showConfigWindow() {
|
||||
// create lazily
|
||||
if (configWindow == undefined) {
|
||||
configWindow = new BrowserWindow({
|
||||
parent: win,
|
||||
width: 640,
|
||||
height: 460,
|
||||
minimizable: false,
|
||||
maximizable: false,
|
||||
fullscreenable: false,
|
||||
backgroundColor: '#1f1f1f',
|
||||
autoHideMenuBar: true,
|
||||
excludedFromShownWindowsMenu: true,
|
||||
webPreferences: {
|
||||
preload: path.join(__dirname, 'js/configPreload.js')
|
||||
}
|
||||
});
|
||||
|
||||
configWindow.on('close', (event) => {
|
||||
event.preventDefault();
|
||||
configWindow.hide();
|
||||
});
|
||||
|
||||
ipcMain.on('save-config', (event, configData) => {
|
||||
configWindow.hide();
|
||||
saveConfig(configData);
|
||||
});
|
||||
ipcMain.on('cancel-config', (event) => {
|
||||
configWindow.hide();
|
||||
configWindow.webContents.send('update-config', config);
|
||||
});
|
||||
|
||||
configWindow.loadFile('./html/config.html');
|
||||
}
|
||||
|
||||
// update text field values
|
||||
configWindow.webContents.send('update-config', config);
|
||||
|
||||
// show window
|
||||
configWindow.show();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Creates the browser window
|
||||
*/
|
||||
@@ -197,8 +58,8 @@ function createMainWindow() {
|
||||
win.on("page-title-updated", (event) => event.preventDefault());
|
||||
|
||||
// Load page from config file
|
||||
if (config != undefined && 'url' in config) {
|
||||
loadUrlAsync(config.url);
|
||||
if (config.data && 'url' in config.data && config.data.url) {
|
||||
loadUrlAsync(config.data.url);
|
||||
} else {
|
||||
try {
|
||||
// Load file from data folder if available
|
||||
@@ -231,7 +92,12 @@ function createMenu() {
|
||||
{
|
||||
label: 'Config',
|
||||
accelerator: isMac ? 'Cmd+,' : 'Ctrl+,',
|
||||
click: () => { showConfigWindow(); }
|
||||
click: () => {
|
||||
config.showWindow(
|
||||
win,
|
||||
(newConfig) => { loadUrlAsync(newConfig.url); }
|
||||
);
|
||||
}
|
||||
}
|
||||
]
|
||||
}));
|
||||
@@ -263,6 +129,7 @@ function validateDomain(event) {
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Tests if the host of the url is reachable.
|
||||
* Access is tried in a 1 second interval and the url is loaded if successfull.
|
||||
@@ -277,8 +144,8 @@ async function loadUrlAsync(testurl) {
|
||||
reachable = await isReachable(url.href);
|
||||
if (reachable) {
|
||||
log.info("Successfull access to " + url.hostname);
|
||||
log.info("Loading " + config.url);
|
||||
win.loadURL(config.url);
|
||||
log.info("Loading " + config.data.url);
|
||||
win.loadURL(config.data.url);
|
||||
} else {
|
||||
log.warn("Could not reach " + url.hostname);
|
||||
|
||||
@@ -300,11 +167,13 @@ async function loadUrlAsync(testurl) {
|
||||
}
|
||||
// Load other protocols (i. e. offline) immedately
|
||||
else {
|
||||
win.loadURL(config.url);
|
||||
win.loadURL(config.data.url);
|
||||
}
|
||||
|
||||
} catch (err) {
|
||||
log.error(err);
|
||||
log.error(err.message);
|
||||
// Load fallback page
|
||||
win.loadFile('./html/onboarding.html');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -319,12 +188,11 @@ app.commandLine.appendSwitch('no-sandbox');
|
||||
|
||||
app.whenReady().then(() => {
|
||||
|
||||
// configModule.load();
|
||||
loadConfig();
|
||||
config.load();
|
||||
|
||||
log.initialize();
|
||||
if (config != undefined && 'logPath' in config) {
|
||||
log.transports.file.resolvePathFn = () => config.logPath;
|
||||
if (config.data != undefined && 'logPath' in config.data) {
|
||||
log.transports.file.resolvePathFn = () => config.data.logPath;
|
||||
}
|
||||
|
||||
log.info('----------------------------------------------');
|
||||
|
||||
Reference in New Issue
Block a user