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

98 lines
2.6 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-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
*/
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
ctx.send({
message: `Config was successfully exported to ${strapi.config.get('plugin.config-sync.syncDir')}.`,
});
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) => {
// Check for existance of the config file sync dir.
if (!fs.existsSync(strapi.config.get('plugin.config-sync.syncDir'))) {
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
2022-11-28 17:00:35 +01:00
if (!ctx.request.body.config) {
2021-11-20 19:06:01 +01:00
ctx.send({
2022-11-28 17:00:35 +01:00
message: 'No config was specified for the import endpoint.',
2021-11-20 19:06:01 +01:00
});
return;
}
2022-11-28 17:00:35 +01:00
await Promise.all(ctx.request.body.config.map(async (configName) => {
await strapi.plugin('config-sync').service('main').importSingleConfig(configName, null, ctx.request.body.force);
2021-11-20 19:06:01 +01:00
}));
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 sync dir.
if (!fs.existsSync(strapi.config.get('plugin.config-sync.syncDir'))) {
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-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 () => {
2022-11-28 17:00:35 +01:00
return {
env: strapi.server.app.env,
config: strapi.config.get('plugin.config-sync'),
};
2021-12-09 09:20:46 +01:00
},
2021-03-19 19:04:22 +01:00
};