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
|
|
|
*/
|
2021-03-19 21:50:23 +01:00
|
|
|
export: async (ctx) => {
|
|
|
|
const coreStoreAPI = strapi.query('core_store');
|
|
|
|
const coreStore = await coreStoreAPI.find({ _limit: -1 });
|
|
|
|
|
|
|
|
Object.values(coreStore).map(async ({ key, value }) => {
|
|
|
|
await strapi.plugins.config.services.config.writeConfigFile(key, value);
|
|
|
|
});
|
|
|
|
|
|
|
|
ctx.send({
|
|
|
|
message: `Config was successfully exported to ${strapi.plugins.config.config.destination}.`
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
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-19 21:50:23 +01:00
|
|
|
import: async (ctx) => {
|
|
|
|
// Check for existance of the config file destination dir.
|
|
|
|
if (!fs.existsSync(strapi.plugins.config.config.destination)) {
|
|
|
|
ctx.send({
|
|
|
|
message: 'No config files were found.'
|
|
|
|
});
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const configFiles = fs.readdirSync(strapi.plugins.config.config.destination);
|
|
|
|
|
|
|
|
configFiles.map((file) => {
|
|
|
|
strapi.plugins.config.services.config.importFromFile(file.slice(0, -5));
|
|
|
|
});
|
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
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get all configs as defined in your filesystem.
|
|
|
|
*
|
|
|
|
* @param {object} ctx - Request context object.
|
|
|
|
* @returns {object} Object with key value pairs of configs.
|
|
|
|
*/
|
|
|
|
getConfigsFromFiles: async (ctx) => {
|
|
|
|
// Check for existance of the config file destination dir.
|
|
|
|
if (!fs.existsSync(strapi.plugins.config.config.destination)) {
|
|
|
|
ctx.send({
|
|
|
|
message: 'No config files were found.'
|
|
|
|
});
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const configFiles = fs.readdirSync(strapi.plugins.config.config.destination);
|
|
|
|
let formattedConfigs = {};
|
|
|
|
|
|
|
|
const getConfigs = async () => {
|
|
|
|
return Promise.all(configFiles.map(async (file) => {
|
|
|
|
const formattedConfigName = file.slice(0, -5); // remove the .json extension.
|
|
|
|
const fileContents = await strapi.plugins.config.services.config.readConfigFile(formattedConfigName);
|
|
|
|
formattedConfigs[formattedConfigName] = fileContents;
|
|
|
|
}));
|
|
|
|
};
|
|
|
|
|
|
|
|
await getConfigs();
|
|
|
|
|
|
|
|
ctx.send(formattedConfigs);
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get all configs as defined in your database.
|
|
|
|
*
|
|
|
|
* @param {object} ctx - Request context object.
|
|
|
|
* @returns {object} Object with key value pairs of configs.
|
|
|
|
*/
|
|
|
|
getConfigsFromDatabase: async (ctx) => {
|
|
|
|
const coreStoreAPI = strapi.query('core_store');
|
|
|
|
const coreStore = await coreStoreAPI.find({ _limit: -1 });
|
|
|
|
|
|
|
|
let formattedConfigs = {};
|
|
|
|
Object.values(coreStore).map(async ({ key, value }) => {
|
|
|
|
formattedConfigs[key] = JSON.parse(value);
|
|
|
|
});
|
|
|
|
|
|
|
|
ctx.send(formattedConfigs);
|
2021-03-19 19:04:22 +01:00
|
|
|
}
|
|
|
|
};
|