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) => {
|
2021-10-14 17:33:21 +02:00
|
|
|
await strapi.plugin('config-sync').service('main').exportAllConfig();
|
2021-03-19 21:50:23 +01:00
|
|
|
|
2021-03-25 21:24:22 +01:00
|
|
|
ctx.send({
|
2021-10-14 17:33:21 +02:00
|
|
|
message: `Config was successfully exported to ${strapi.config.get('plugin.config-sync.destination')}.`,
|
2021-03-25 21:24:22 +01:00
|
|
|
});
|
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-10-14 17:33:21 +02:00
|
|
|
if (!fs.existsSync(strapi.config.get('plugin.config-sync.destination'))) {
|
2021-03-19 21:50:23 +01:00
|
|
|
ctx.send({
|
2021-10-14 14:37:00 +02:00
|
|
|
message: 'No config files were found.',
|
2021-03-19 21:50:23 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
2021-10-14 14:37:00 +02:00
|
|
|
|
2021-10-14 17:33:21 +02:00
|
|
|
await strapi.plugin('config-sync').service('main').importAllConfig();
|
2021-03-19 19:04:22 +01:00
|
|
|
|
|
|
|
ctx.send({
|
2021-10-14 14:37:00 +02: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-10-14 14:37:00 +02:00
|
|
|
* @returns {object} formattedDiff - The formatted diff object.
|
|
|
|
* @returns {object} formattedDiff.fileConfig - The config as found in the filesystem.
|
|
|
|
* @returns {object} formattedDiff.databaseConfig - The config as found in the database.
|
|
|
|
* @returns {object} formattedDiff.diff - The diff between the file config and databse 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-10-14 17:33:21 +02:00
|
|
|
if (!fs.existsSync(strapi.config.get('plugin.config-sync.destination'))) {
|
2021-03-20 03:20:48 +01:00
|
|
|
ctx.send({
|
2021-10-14 14:37:00 +02:00
|
|
|
message: 'No config files were found.',
|
2021-03-20 03:20:48 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-03-24 18:38:36 +01:00
|
|
|
const formattedDiff = {
|
|
|
|
fileConfig: {},
|
|
|
|
databaseConfig: {},
|
2021-10-14 14:37:00 +02:00
|
|
|
diff: {},
|
2021-03-20 03:20:48 +01:00
|
|
|
};
|
2021-10-14 14:37:00 +02:00
|
|
|
|
2021-10-14 17:33:21 +02:00
|
|
|
const fileConfig = await strapi.plugin('config-sync').service('main').getAllConfigFromFiles();
|
|
|
|
const databaseConfig = await strapi.plugin('config-sync').service('main').getAllConfigFromDatabase();
|
2021-03-20 03:20:48 +01:00
|
|
|
|
2021-03-27 00:54:41 +01:00
|
|
|
const diff = difference(databaseConfig, fileConfig);
|
|
|
|
|
2021-03-24 18:38:36 +01:00
|
|
|
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-06-02 15:18:28 +02:00
|
|
|
});
|
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
|
|
|
};
|