Create a default config when no file was found #6

This commit is contained in:
2024-06-03 20:40:33 +02:00
parent c4cc2170f5
commit e47611ee2e
+37 -18
View File
@@ -29,14 +29,16 @@ let menu;
* locations and in this order:
*
* 1. `/assets/presentation/config.json` (Tooloop OS)
* 2. Path of the executable
* 2. appData directory
* 3. Path of the executable
* - Linux: `app.getPath('exe')`
* - MacOS: `path.resolve(app.getPath('exe'), "../../../../")`
* 3. `__dirname` (Development)
* 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
];
@@ -44,14 +46,15 @@ function loadConfig() {
// Check all locations
for (const location of locations) {
// Update the filepath
configPath = path.join(location, 'config.json');
configPath = location;
let configFile = path.join(configPath, 'config.json');
try {
// Try access
fs.accessSync(configPath);
fs.accessSync(configFile);
// Parse the file if found
console.info('Found config file at ' + configPath);
const data = fs.readFileSync(configPath, { encoding: 'utf8' });
console.info('Found config file at ' + configFile);
const data = fs.readFileSync(configFile, { encoding: 'utf8' });
config = JSON.parse(data);
// Break the loop
@@ -63,13 +66,24 @@ function loadConfig() {
}
function saveConfig(newConfig) {
if (!configPath) {
console.error("No config file path was set");
return;
// 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);
}
}
if(config.url != newConfig.url) {
// Store changes
if (config.url != newConfig.url) {
config.url = newConfig.url;
loadUrlAsync(config.url);
}
@@ -83,15 +97,20 @@ function saveConfig(newConfig) {
}
);
config.logPath = newConfig.logPath;
fs.writeFile(configPath, JSON.stringify(config, null, " "), (error) => {
// 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 " + configPath);
});
log.info("Saved config to " + configFile);
}
);
// updare UI with new values
// Update UI with new values
configWindow.webContents.send('update-config', config);
}
@@ -152,7 +171,7 @@ function createMainWindow() {
backgroundColor: '#000000',
icon: 'images/icon-512.png',
autoHideMenuBar: true,
kiosk: true,
// kiosk: true,
webPreferences: {
preload: path.join(__dirname, 'js/preload.js'),
webSecurity: false,
@@ -299,8 +318,8 @@ app.commandLine.appendSwitch('--no-sandbox');
app.commandLine.appendSwitch('no-sandbox');
app.whenReady().then(() => {
// configModule.load();
console.log(configModule.config);
loadConfig();
log.initialize();