safesupport-bot/dist/setup.js

54 lines
1.3 KiB
JavaScript
Raw Normal View History

2020-12-01 03:47:32 +00:00
"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('_');
2020-12-01 18:21:16 +00:00
if (scope === 'platform') {
2020-12-01 03:47:32 +00:00
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();