const fs = require('fs'); const path = require('path'); const parrentProcess = JSON.parse(process.env.PARENT_PROCESS || '{}'); let userDataPath = ''; const defaultSettings = { userDataPath: '', tmdbApiUrl: '', tmdbApiKey: '', frontPort: '', backPort: '', portableMode: '', libraryViewMode: '', favoritViewMode: '', watchlistViewMode: '', CollectionsViewMode: '', CollectionItemsViewMode: '', }; function getUserDataPath() { // return parrentProcess.electron_path || path.join(__dirname, '..', '..', 'data'); return path.join(__dirname, '..', '..'); } function getSettingsFilePath() { return path.join(getUserDataPath(), 'settings.json'); } function getSettings() { const settingsFilePath = getSettingsFilePath(); try { const settingsData = fs.readFileSync(settingsFilePath); // console.log(`Loaded settings file.`); return JSON.parse(settingsData); } catch (err) { console.error('Error reading settings file:', err); console.log('Creating default settings file...'); let newSettingsFile = { ...defaultSettings }; let defaultDataPath = path.join(getUserDataPath(), 'data') newSettingsFile.userDataPath = defaultDataPath; newSettingsFile.backPort = 4000; fs.mkdirSync(defaultDataPath, { recursive: true }); fs.writeFileSync(settingsFilePath, JSON.stringify(newSettingsFile, null, 2)); console.log('Default settings file created.'); return defaultSettings; } } function updateSettings(newSettings) { const settingsFilePath = getSettingsFilePath(); const oldSettings = getSettings(); let updatedSettings = { ...defaultSettings, ...oldSettings, ...newSettings }; fs.mkdirSync(updatedSettings.userDataPath, { recursive: true }); // making sure the newly set data path is created before saving the new config fs.writeFileSync(settingsFilePath, JSON.stringify(updatedSettings, null, 2)); } module.exports = { getSettings, updateSettings };