Server: Reimplemented config system

This commit is contained in:
Paweł Malak 2021-10-21 17:14:25 +02:00
parent 85ee5da025
commit b7de1e3d27
6 changed files with 107 additions and 114 deletions

View file

@ -0,0 +1,41 @@
const { DataTypes } = require('sequelize');
const { INTEGER, DATE, STRING, TINYINT, FLOAT, TEXT } = DataTypes;
const { readFile, writeFile, copyFile } = require('fs/promises');
const Config = require('../../models/Config');
const up = async (query) => {
await copyFile('utils/init/initialConfig.json', 'data/config.json');
const initConfigFile = await readFile('data/config.json', 'utf-8');
const parsedNewConfig = JSON.parse(initConfigFile);
const existingConfig = await Config.findAll({ raw: true });
for (let pair of existingConfig) {
const { key, value, valueType } = pair;
let newValue = value;
if (valueType == 'number') {
newValue = parseFloat(value);
} else if (valueType == 'boolean') {
newValue = value == 1;
}
parsedNewConfig[key] = newValue;
}
const newConfig = JSON.stringify(parsedNewConfig);
await writeFile('data/config.json', newConfig);
// await query.dropTable('config');
};
const down = async (query) => {
// await query.dropTable('config');
};
module.exports = {
up,
down,
};

10
utils/checkFileExists.js Normal file
View file

@ -0,0 +1,10 @@
const fs = require('fs');
const checkFileExists = (path) => {
return fs.promises
.access(path, fs.constants.F_OK)
.then(() => true)
.catch(() => false);
};
module.exports = checkFileExists;

View file

@ -2,8 +2,8 @@ const initConfig = require('./initConfig');
const initFiles = require('./initFiles');
const initApp = async () => {
await initConfig();
await initFiles();
await initConfig();
};
module.exports = initApp;

View file

@ -1,39 +1,25 @@
const { Op } = require('sequelize');
const Config = require('../../models/Config');
const { config } = require('./initialConfig.json');
const Logger = require('../Logger');
const logger = new Logger();
const { copyFile, readFile, writeFile } = require('fs/promises');
const checkFileExists = require('../checkFileExists');
const initialConfig = require('./initialConfig.json');
const initConfig = async () => {
// Get config values
const configPairs = await Config.findAll({
where: {
key: {
[Op.or]: config.map((pair) => pair.key),
},
},
});
const configExists = await checkFileExists('data/config.json');
// Get key from each pair
const configKeys = configPairs.map((pair) => pair.key);
// Create missing pairs
config.forEach(async ({ key, value }) => {
if (!configKeys.includes(key)) {
await Config.create({
key,
value,
valueType: typeof value,
});
}
});
if (process.env.NODE_ENV == 'development') {
logger.log('Initial config created');
if (!configExists) {
await copyFile('utils/init/initialConfig.json', 'data/config.json');
}
return;
const existingConfig = await readFile('data/config.json', 'utf-8');
const parsedConfig = JSON.parse(existingConfig);
// Add new config pairs if necessary
for (let key in initialConfig) {
if (!Object.keys(parsedConfig).includes(key)) {
parsedConfig[key] = initialConfig[key];
}
}
await writeFile('data/config.json', JSON.stringify(parsedConfig));
};
module.exports = initConfig;

View file

@ -1,84 +1,22 @@
{
"config": [
{
"key": "WEATHER_API_KEY",
"value": ""
},
{
"key": "lat",
"value": 0
},
{
"key": "long",
"value": 0
},
{
"key": "isCelsius",
"value": true
},
{
"key": "customTitle",
"value": "Flame"
},
{
"key": "pinAppsByDefault",
"value": true
},
{
"key": "pinCategoriesByDefault",
"value": true
},
{
"key": "hideHeader",
"value": false
},
{
"key": "useOrdering",
"value": "createdAt"
},
{
"key": "appsSameTab",
"value": false
},
{
"key": "bookmarksSameTab",
"value": false
},
{
"key": "searchSameTab",
"value": false
},
{
"key": "hideApps",
"value": false
},
{
"key": "hideCategories",
"value": false
},
{
"key": "hideSearch",
"value": false
},
{
"key": "defaultSearchProvider",
"value": "l"
},
{
"key": "dockerApps",
"value": false
},
{
"key": "dockerHost",
"value": "localhost"
},
{
"key": "kubernetesApps",
"value": false
},
{
"key": "unpinStoppedApps",
"value": false
}
]
"WEATHER_API_KEY": "",
"lat": 0,
"long": 0,
"isCelsius": true,
"customTitle": "Flame",
"pinAppsByDefault": true,
"pinCategoriesByDefault": true,
"hideHeader": false,
"useOrdering": "createdAt",
"appsSameTab": false,
"bookmarksSameTab": false,
"searchSameTab": false,
"hideApps": false,
"hideCategories": false,
"hideSearch": false,
"defaultSearchProvider": "l",
"dockerApps": false,
"dockerHost": "localhost",
"kubernetesApps": false,
"unpinStoppedApps": false
}

18
utils/loadConfig.js Normal file
View file

@ -0,0 +1,18 @@
const { readFile } = require('fs/promises');
const checkFileExists = require('../utils/checkFileExists');
const initConfig = require('../utils/init/initConfig');
const loadConfig = async () => {
const configExists = await checkFileExists('data/config.json');
if (!configExists) {
await initConfig();
}
const config = await readFile('data/config.json', 'utf-8');
const parsedConfig = JSON.parse(config);
return parsedConfig;
};
module.exports = loadConfig;