try to find the config file in various locations

This commit is contained in:
2024-05-29 11:24:13 +02:00
parent f1654d14ad
commit 4956bbcec9
+39 -11
View File
@@ -1,6 +1,7 @@
const { app, BrowserWindow } = require('electron/main');
const path = require('node:path');
const fs = require('fs');
const os = require('os');
const log = require('electron-log/main');
@@ -18,21 +19,48 @@ let win;
/**
* Loads a config file from disc.
* The file is expected to be in the application path and called “config.json”.
*/
* 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. Path of the executable
* - Linux: `app.getPath('exe')`
* - MacOS: `path.resolve(app.getPath('exe'), "../../../")`
* 3. `__dirname` (Development)
*/
function loadConfig() {
try {
const data = fs.readFileSync(path.join(__dirname, "config.json"), { encoding: 'utf8' });
config = JSON.parse(data);
} catch (err) {
if (err.code == 'ENOENT') {
console.warn('No config file found at ' + path.join(__dirname, "config.json"));
} else {
console.error(err);
const locations = [
'/assets/data',
os.platform() == 'darwin' ?
path.resolve(app.getPath('exe'), "../../../") : app.getPath('exe'),
__dirname
];
let filePath;
// Check all locations
for (let i = 0; i < locations.length; i++) {
// Update the filepath
filePath = path.join(locations[i], 'config.json');
try {
// Try access
fs.accessSync(filePath);
// Parse the file if found
console.info('Found config file at ' + filePath);
const data = fs.readFileSync(filePath, { encoding: 'utf8' });
config = JSON.parse(data);
// Break the loop if successful
return false;
} catch (err) {
console.warn('No config file found at ' + filePath);
}
}
}
/**
* Creates the browser window
*/
@@ -55,7 +83,7 @@ async function createWindow() {
win = null;
});
win.webContents.addListener("willnavigate-", validateDomain);
win.webContents.addListener("will-navigate", validateDomain);
win.once("ready-to-show", () => {
win.setKiosk(true);