strapi-plugin-config-sync/server/controllers/config.js

69 lines
1.8 KiB
JavaScript
Raw Normal View History

2021-03-19 19:04:22 +01:00
'use strict';
2021-03-19 21:50:23 +01:00
const fs = require('fs');
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
*/
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
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-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}
*/
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
});
},
/**
* Get config diff between filesystem & db.
*
* @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.
*/
getDiff: async (ctx) => {
// 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'))) {
ctx.send({
2021-10-14 14:37:00 +02:00
message: 'No config files were found.',
});
return;
}
2021-11-10 16:22:52 +01:00
return strapi.plugin('config-sync').service('main').getFormattedDiff();
},
2021-03-19 19:04:22 +01:00
};