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