Add comments
parent
75bcb74bb4
commit
bc4965a337
|
@ -3,19 +3,16 @@
|
||||||
const fs = require('fs');
|
const fs = require('fs');
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* config.js controller
|
* Main controllers for config import/export.
|
||||||
*
|
|
||||||
* @description: A set of functions called "actions" of the `config` plugin.
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Default action.
|
* Export all config, from db to filesystem.
|
||||||
*
|
*
|
||||||
* @return {Object}
|
* @param {object} ctx - Request context object.
|
||||||
|
* @returns {void}
|
||||||
*/
|
*/
|
||||||
|
|
||||||
export: async (ctx) => {
|
export: async (ctx) => {
|
||||||
const coreStoreAPI = strapi.query('core_store');
|
const coreStoreAPI = strapi.query('core_store');
|
||||||
const coreStore = await coreStoreAPI.find({ _limit: -1 });
|
const coreStore = await coreStoreAPI.find({ _limit: -1 });
|
||||||
|
@ -29,6 +26,12 @@ module.exports = {
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Import all config, from filesystem to db.
|
||||||
|
*
|
||||||
|
* @param {object} ctx - Request context object.
|
||||||
|
* @returns {void}
|
||||||
|
*/
|
||||||
import: async (ctx) => {
|
import: async (ctx) => {
|
||||||
// Check for existance of the config file destination dir.
|
// Check for existance of the config file destination dir.
|
||||||
if (!fs.existsSync(strapi.plugins.config.config.destination)) {
|
if (!fs.existsSync(strapi.plugins.config.config.destination)) {
|
||||||
|
|
|
@ -4,22 +4,28 @@ const fs = require('fs');
|
||||||
const util = require('util');
|
const util = require('util');
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* config.js service
|
* Main services for config import/export.
|
||||||
*
|
|
||||||
* @description: A set of functions similar to controller's actions to avoid code duplication.
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
|
/**
|
||||||
|
* Write a single config file.
|
||||||
|
*
|
||||||
|
* @param {string} configName - The name of the config file.
|
||||||
|
* @param {string} fileContents - The JSON content of the config file.
|
||||||
|
* @returns {void}
|
||||||
|
*/
|
||||||
writeConfigFile: async (configName, fileContents) => {
|
writeConfigFile: async (configName, fileContents) => {
|
||||||
|
// Check if the config should be excluded.
|
||||||
const shouldExclude = strapi.plugins.config.config.exclude.includes(configName);
|
const shouldExclude = strapi.plugins.config.config.exclude.includes(configName);
|
||||||
if (shouldExclude) return;
|
if (shouldExclude) return;
|
||||||
|
|
||||||
|
// Check if the JSON content should be minified.
|
||||||
const json =
|
const json =
|
||||||
!strapi.plugins.config.config.minify ?
|
!strapi.plugins.config.config.minify ?
|
||||||
JSON.stringify(JSON.parse(fileContents), null, 2)
|
JSON.stringify(JSON.parse(fileContents), null, 2)
|
||||||
: fileContents;
|
: fileContents;
|
||||||
|
|
||||||
// Create the export folder if it does not yet exist.
|
|
||||||
if (!fs.existsSync(strapi.plugins.config.config.destination)) {
|
if (!fs.existsSync(strapi.plugins.config.config.destination)) {
|
||||||
fs.mkdirSync(strapi.plugins.config.config.destination, { recursive: true });
|
fs.mkdirSync(strapi.plugins.config.config.destination, { recursive: true });
|
||||||
}
|
}
|
||||||
|
@ -28,6 +34,12 @@ module.exports = {
|
||||||
await writeFile(`${strapi.plugins.config.config.destination}${configName}.json`, json);
|
await writeFile(`${strapi.plugins.config.config.destination}${configName}.json`, json);
|
||||||
},
|
},
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Read from a config file.
|
||||||
|
*
|
||||||
|
* @param {string} configName - The name of the config file.
|
||||||
|
* @returns {object} The JSON content of the config file.
|
||||||
|
*/
|
||||||
readConfigFile: async (configName) => {
|
readConfigFile: async (configName) => {
|
||||||
const readFile = util.promisify(fs.readFile);
|
const readFile = util.promisify(fs.readFile);
|
||||||
return await readFile(`${strapi.plugins.config.config.destination}${configName}.json`)
|
return await readFile(`${strapi.plugins.config.config.destination}${configName}.json`)
|
||||||
|
@ -39,14 +51,20 @@ module.exports = {
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Import a config file into the db.
|
||||||
|
*
|
||||||
|
* @param {string} configName - The name of the config file.
|
||||||
|
* @returns {void}
|
||||||
|
*/
|
||||||
importFromFile: async (configName) => {
|
importFromFile: async (configName) => {
|
||||||
|
// Check if the config should be excluded.
|
||||||
const shouldExclude = strapi.plugins.config.config.exclude.includes(configName);
|
const shouldExclude = strapi.plugins.config.config.exclude.includes(configName);
|
||||||
if (shouldExclude) return;
|
if (shouldExclude) return;
|
||||||
|
|
||||||
const coreStoreAPI = strapi.query('core_store');
|
const coreStoreAPI = strapi.query('core_store');
|
||||||
const fileContents = await strapi.plugins.config.services.config.readConfigFile(configName);
|
const fileContents = await strapi.plugins.config.services.config.readConfigFile(configName);
|
||||||
|
|
||||||
try {
|
|
||||||
const configExists = await strapi
|
const configExists = await strapi
|
||||||
.query('core_store')
|
.query('core_store')
|
||||||
.findOne({ key: configName });
|
.findOne({ key: configName });
|
||||||
|
@ -56,10 +74,5 @@ module.exports = {
|
||||||
} else {
|
} else {
|
||||||
await coreStoreAPI.update({ key: configName }, { value: fileContents });
|
await coreStoreAPI.update({ key: configName }, { value: fileContents });
|
||||||
}
|
}
|
||||||
|
|
||||||
return { success: true };
|
|
||||||
} catch (err) {
|
|
||||||
throw new Error(err);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in New Issue