2021-03-19 19:04:22 +01:00
|
|
|
'use strict';
|
|
|
|
|
2021-03-19 21:50:23 +01:00
|
|
|
const fs = require('fs');
|
2021-03-24 18:38:36 +01:00
|
|
|
const difference = require('../utils/getObjectDiff');
|
2021-03-19 21:50:23 +01:00
|
|
|
|
2021-03-19 19:04:22 +01:00
|
|
|
/**
|
2021-03-20 00:16:26 +01:00
|
|
|
* Main controllers for config import/export.
|
2021-03-19 19:04:22 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
/**
|
2021-03-20 00:16:26 +01:00
|
|
|
* Export all config, from db to filesystem.
|
2021-03-19 19:04:22 +01:00
|
|
|
*
|
2021-03-20 00:16:26 +01:00
|
|
|
* @param {object} ctx - Request context object.
|
|
|
|
* @returns {void}
|
2021-03-19 19:04:22 +01:00
|
|
|
*/
|
2021-03-24 16:22:03 +01:00
|
|
|
exportAll: async (ctx) => {
|
|
|
|
await strapi.plugins['config-sync'].services.main.exportAllConfig();
|
2021-03-19 21:50:23 +01:00
|
|
|
|
|
|
|
ctx.send({
|
2021-03-21 18:04:18 +01:00
|
|
|
message: `Config was successfully exported to ${strapi.plugins['config-sync'].config.destination}.`
|
2021-03-19 21:50:23 +01:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2021-03-20 00:16:26 +01:00
|
|
|
/**
|
|
|
|
* Import all config, from filesystem to db.
|
|
|
|
*
|
|
|
|
* @param {object} ctx - Request context object.
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
2021-03-24 16:22:03 +01:00
|
|
|
importAll: async (ctx) => {
|
2021-03-19 21:50:23 +01:00
|
|
|
// Check for existance of the config file destination dir.
|
2021-03-21 18:04:18 +01:00
|
|
|
if (!fs.existsSync(strapi.plugins['config-sync'].config.destination)) {
|
2021-03-19 21:50:23 +01:00
|
|
|
ctx.send({
|
|
|
|
message: 'No config files were found.'
|
|
|
|
});
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-03-24 16:22:03 +01:00
|
|
|
await strapi.plugins['config-sync'].services.main.importAllConfig();
|
2021-03-19 19:04:22 +01:00
|
|
|
|
|
|
|
ctx.send({
|
2021-03-19 21:50:23 +01:00
|
|
|
message: 'Config was successfully imported.'
|
2021-03-19 19:04:22 +01:00
|
|
|
});
|
2021-03-20 03:20:48 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2021-03-24 18:38:36 +01:00
|
|
|
* Get config diff between filesystem & db.
|
2021-03-20 03:20:48 +01:00
|
|
|
*
|
|
|
|
* @param {object} ctx - Request context object.
|
2021-03-24 18:38:36 +01:00
|
|
|
* @returns Object with key value pairs of config.
|
2021-03-20 03:20:48 +01:00
|
|
|
*/
|
2021-03-24 18:38:36 +01:00
|
|
|
getDiff: async (ctx) => {
|
2021-03-20 03:20:48 +01:00
|
|
|
// Check for existance of the config file destination dir.
|
2021-03-21 18:04:18 +01:00
|
|
|
if (!fs.existsSync(strapi.plugins['config-sync'].config.destination)) {
|
2021-03-20 03:20:48 +01:00
|
|
|
ctx.send({
|
|
|
|
message: 'No config files were found.'
|
|
|
|
});
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-03-24 18:38:36 +01:00
|
|
|
const formattedDiff = {
|
|
|
|
fileConfig: {},
|
|
|
|
databaseConfig: {},
|
|
|
|
diff: {}
|
2021-03-20 03:20:48 +01:00
|
|
|
};
|
2021-03-24 18:38:36 +01:00
|
|
|
|
|
|
|
const fileConfig = await strapi.plugins['config-sync'].services.main.getAllConfigFromFiles();
|
|
|
|
const databaseConfig = await strapi.plugins['config-sync'].services.main.getAllConfigFromDatabase();
|
2021-03-20 03:20:48 +01:00
|
|
|
|
2021-03-24 18:38:36 +01:00
|
|
|
const diff = difference(fileConfig, databaseConfig);
|
|
|
|
formattedDiff.diff = diff;
|
2021-03-20 03:20:48 +01:00
|
|
|
|
2021-03-24 18:38:36 +01:00
|
|
|
Object.keys(diff).map((changedConfigName) => {
|
|
|
|
formattedDiff.fileConfig[changedConfigName] = fileConfig[changedConfigName];
|
|
|
|
formattedDiff.databaseConfig[changedConfigName] = databaseConfig[changedConfigName];
|
|
|
|
})
|
2021-03-20 03:20:48 +01:00
|
|
|
|
2021-03-24 18:38:36 +01:00
|
|
|
return formattedDiff;
|
|
|
|
},
|
2021-03-19 19:04:22 +01:00
|
|
|
};
|