"use strict"; const fs = require('fs'); const fetch = require('node-fetch'); const config = require('../config.json'); const getSettings = async () => { try { const url = `${config.settingsEndpoint}?homeserver=${encodeURIComponent(config.matrixServerUrl)}`; if (!config.matrixServerUrl) { throw new Error("The matrix server url is not provided"); } console.log(`Fetching settings for ${config.matrixServerUrl}`); const res = await fetch(url); const data = await res.json(); const { fields, schedule = [] } = data; return Object.entries(fields).reduce((settingsObj, [k, v]) => { const [scope, key] = k.split('_'); if (scope === 'platform') { settingsObj[key] = v; } return settingsObj; }, {}); } catch (err) { console.log("Error fetching settings", err); return null; } }; const writeConfig = async () => { const settings = await getSettings(); if (!settings) { return console.log('No settings to update'); } const updatedSettings = Object.assign(config, settings); fs.writeFile('config.json', JSON.stringify(updatedSettings, null, 2), function (err) { if (err) return console.log("Error updating settings", err); console.log(`Updated settings to config.json`); console.log(updatedSettings); }); }; writeConfig();