Refactor get config API calls to show the diff
parent
850c18ce58
commit
8ca6317b74
|
@ -18,16 +18,8 @@
|
|||
},
|
||||
{
|
||||
"method": "GET",
|
||||
"path": "/all/from-files",
|
||||
"handler": "config.getConfigsFromFiles",
|
||||
"config": {
|
||||
"policies": []
|
||||
}
|
||||
},
|
||||
{
|
||||
"method": "GET",
|
||||
"path": "/all/from-database",
|
||||
"handler": "config.getConfigsFromDatabase",
|
||||
"path": "/diff",
|
||||
"handler": "config.getDiff",
|
||||
"config": {
|
||||
"policies": []
|
||||
}
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
'use strict';
|
||||
|
||||
const fs = require('fs');
|
||||
const difference = require('../utils/getObjectDiff');
|
||||
|
||||
/**
|
||||
* Main controllers for config import/export.
|
||||
|
@ -45,12 +46,12 @@ module.exports = {
|
|||
},
|
||||
|
||||
/**
|
||||
* Get all configs as defined in your filesystem.
|
||||
* Get config diff between filesystem & db.
|
||||
*
|
||||
* @param {object} ctx - Request context object.
|
||||
* @returns {object} Object with key value pairs of configs.
|
||||
* @returns Object with key value pairs of config.
|
||||
*/
|
||||
getConfigsFromFiles: async (ctx) => {
|
||||
getDiff: async (ctx) => {
|
||||
// Check for existance of the config file destination dir.
|
||||
if (!fs.existsSync(strapi.plugins['config-sync'].config.destination)) {
|
||||
ctx.send({
|
||||
|
@ -60,37 +61,23 @@ module.exports = {
|
|||
return;
|
||||
}
|
||||
|
||||
const configFiles = fs.readdirSync(strapi.plugins['config-sync'].config.destination);
|
||||
let formattedConfigs = {};
|
||||
|
||||
const getConfigs = async () => {
|
||||
return Promise.all(configFiles.map(async (file) => {
|
||||
const formattedConfigName = file.slice(0, -5); // remove the .json extension.
|
||||
const fileContents = await strapi.plugins['config-sync'].services.main.readConfigFile(formattedConfigName);
|
||||
formattedConfigs[formattedConfigName] = fileContents;
|
||||
}));
|
||||
const formattedDiff = {
|
||||
fileConfig: {},
|
||||
databaseConfig: {},
|
||||
diff: {}
|
||||
};
|
||||
|
||||
await getConfigs();
|
||||
const fileConfig = await strapi.plugins['config-sync'].services.main.getAllConfigFromFiles();
|
||||
const databaseConfig = await strapi.plugins['config-sync'].services.main.getAllConfigFromDatabase();
|
||||
|
||||
ctx.send(formattedConfigs);
|
||||
const diff = difference(fileConfig, databaseConfig);
|
||||
formattedDiff.diff = diff;
|
||||
|
||||
Object.keys(diff).map((changedConfigName) => {
|
||||
formattedDiff.fileConfig[changedConfigName] = fileConfig[changedConfigName];
|
||||
formattedDiff.databaseConfig[changedConfigName] = databaseConfig[changedConfigName];
|
||||
})
|
||||
|
||||
return formattedDiff;
|
||||
},
|
||||
|
||||
/**
|
||||
* Get all configs as defined in your database.
|
||||
*
|
||||
* @param {object} ctx - Request context object.
|
||||
* @returns {object} Object with key value pairs of configs.
|
||||
*/
|
||||
getConfigsFromDatabase: async (ctx) => {
|
||||
const coreStoreAPI = strapi.query('core_store');
|
||||
const coreStore = await coreStoreAPI.find({ _limit: -1 });
|
||||
|
||||
let formattedConfigs = {};
|
||||
Object.values(coreStore).map(async ({ key, value }) => {
|
||||
formattedConfigs[key] = JSON.parse(value);
|
||||
});
|
||||
|
||||
ctx.send(formattedConfigs);
|
||||
}
|
||||
};
|
||||
|
|
|
@ -0,0 +1,23 @@
|
|||
'use strict';
|
||||
const { transform, isEqual, isArray, isObject } = require('lodash');
|
||||
|
||||
/**
|
||||
* Find difference between two objects
|
||||
* @param {object} origObj - Source object to compare newObj against
|
||||
* @param {object} newObj - New object with potential changes
|
||||
* @return {object} differences
|
||||
*/
|
||||
const difference = (origObj, newObj) => {
|
||||
function changes(newObj, origObj) {
|
||||
let arrayIndexCounter = 0
|
||||
return transform(newObj, function (result, value, key) {
|
||||
if (!isEqual(value, origObj[key])) {
|
||||
let resultKey = isArray(origObj) ? arrayIndexCounter++ : key
|
||||
result[resultKey] = (isObject(value) && isObject(origObj[key])) ? changes(value, origObj[key]) : value
|
||||
}
|
||||
})
|
||||
}
|
||||
return changes(newObj, origObj)
|
||||
}
|
||||
|
||||
module.exports = difference;
|
Loading…
Reference in New Issue