2021-03-19 19:04:22 +01:00
|
|
|
'use strict';
|
|
|
|
|
2021-03-19 21:50:23 +01:00
|
|
|
const fs = require('fs');
|
2021-11-20 20:24:57 +01:00
|
|
|
const { isEmpty } = require('lodash');
|
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-11-20 20:24:57 +01:00
|
|
|
if (isEmpty(ctx.request.body)) {
|
|
|
|
await strapi.plugin('config-sync').service('main').exportAllConfig();
|
|
|
|
} else {
|
|
|
|
await Promise.all(ctx.request.body.map(async (configName) => {
|
|
|
|
await strapi.plugin('config-sync').service('main').exportSingleConfig(configName);
|
|
|
|
}));
|
2021-11-20 19:06:01 +01:00
|
|
|
}
|
|
|
|
|
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-11-20 19:06:01 +01:00
|
|
|
if (!ctx.request.body) {
|
|
|
|
ctx.send({
|
|
|
|
message: 'No config was specified for the export endpoint.',
|
|
|
|
});
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
await Promise.all(ctx.request.body.map(async (configName) => {
|
|
|
|
await strapi.plugin('config-sync').service('main').importSingleConfig(configName);
|
|
|
|
}));
|
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-11-10 16:22:52 +01:00
|
|
|
return strapi.plugin('config-sync').service('main').getFormattedDiff();
|
2021-03-24 18:38:36 +01:00
|
|
|
},
|
2021-12-08 16:05:45 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the current Strapi env.
|
|
|
|
* @returns {string} The current Strapi environment.
|
|
|
|
*/
|
2021-12-09 09:20:46 +01:00
|
|
|
getAppEnv: async () => {
|
|
|
|
return { env: strapi.server.app.env };
|
|
|
|
},
|
2021-03-19 19:04:22 +01:00
|
|
|
};
|