feat(deletion): Delete on export

pull/5/head
Boaz Poolman 2021-03-27 18:20:51 +01:00
parent 2db6203c64
commit 6223ac42eb
4 changed files with 106 additions and 28 deletions

View File

@ -2,6 +2,7 @@
const coreStoreQueryString = 'core_store'; const coreStoreQueryString = 'core_store';
const configPrefix = 'core-store'; // Should be the same as the filename. const configPrefix = 'core-store'; // Should be the same as the filename.
const difference = require('../utils/getObjectDiff');
/** /**
* Import/Export for core-store configs. * Import/Export for core-store configs.
@ -14,15 +15,38 @@ module.exports = {
* @returns {void} * @returns {void}
*/ */
exportAll: async () => { exportAll: async () => {
const coreStore = await strapi.query(coreStoreQueryString).find({ _limit: -1 }); const formattedDiff = {
fileConfig: {},
databaseConfig: {},
diff: {}
};
await Promise.all(Object.values(coreStore).map(async ({ id, ...config }) => { const fileConfig = await strapi.plugins['config-sync'].services.main.getAllConfigFromFiles(configPrefix);
const databaseConfig = await strapi.plugins['config-sync'].services.main.getAllConfigFromDatabase(configPrefix);
const diff = difference(databaseConfig, fileConfig);
formattedDiff.diff = diff;
Object.keys(diff).map((changedConfigName) => {
formattedDiff.fileConfig[changedConfigName] = fileConfig[changedConfigName];
formattedDiff.databaseConfig[changedConfigName] = databaseConfig[changedConfigName];
})
await Promise.all(Object.entries(diff).map(async ([configName, config]) => {
// Check if the config should be excluded. // Check if the config should be excluded.
const shouldExclude = strapi.plugins['config-sync'].config.exclude.includes(`${configPrefix}.${config.key}`); const shouldExclude = strapi.plugins['config-sync'].config.exclude.includes(`${configName}`);
if (shouldExclude) return; if (shouldExclude) return;
config.value = JSON.parse(config.value); const currentConfig = formattedDiff.databaseConfig[configName];
await strapi.plugins['config-sync'].services.main.writeConfigFile(configPrefix, config.key, config);
if (
!currentConfig &&
formattedDiff.fileConfig[configName]
) {
await strapi.plugins['config-sync'].services.main.deleteConfigFile(configName);
} else {
await strapi.plugins['config-sync'].services.main.writeConfigFile(configPrefix, currentConfig.key, currentConfig);
}
})); }));
}, },

View File

@ -44,6 +44,20 @@ module.exports = {
}); });
}, },
/**
* Delete config file.
*
* @param {string} configName - The name of the config file.
* @returns {void}
*/
deleteConfigFile: async (configName) => {
// Check if the config should be excluded.
const shouldExclude = strapi.plugins['config-sync'].config.exclude.includes(`${configName}`);
if (shouldExclude) return;
fs.unlinkSync(`${strapi.plugins['config-sync'].config.destination}${configName}.json`);
},
/** /**
* Read from a config file. * Read from a config file.
* *
@ -68,7 +82,7 @@ module.exports = {
* *
* @returns {object} Object with key value pairs of configs. * @returns {object} Object with key value pairs of configs.
*/ */
getAllConfigFromFiles: async () => { getAllConfigFromFiles: async (configType = null) => {
const configFiles = fs.readdirSync(strapi.plugins['config-sync'].config.destination); const configFiles = fs.readdirSync(strapi.plugins['config-sync'].config.destination);
const getConfigs = async () => { const getConfigs = async () => {
@ -77,6 +91,11 @@ module.exports = {
await Promise.all(configFiles.map(async (file) => { await Promise.all(configFiles.map(async (file) => {
const type = file.split('.')[0]; // Grab the first part of the filename. const type = file.split('.')[0]; // Grab the first part of the filename.
const name = file.split(/\.(.+)/)[1].split('.').slice(0, -1).join('.'); // Grab the rest of the filename minus the file extension. const name = file.split(/\.(.+)/)[1].split('.').slice(0, -1).join('.'); // Grab the rest of the filename minus the file extension.
if (configType && configType !== type) {
return;
}
const fileContents = await strapi.plugins['config-sync'].services.main.readConfigFile(type, name); const fileContents = await strapi.plugins['config-sync'].services.main.readConfigFile(type, name);
fileConfigs[`${type}.${name}`] = fileContents; fileConfigs[`${type}.${name}`] = fileContents;
})); }));

View File

@ -1,6 +1,7 @@
'use strict'; 'use strict';
const { sanitizeEntity } = require('strapi-utils'); const { sanitizeEntity } = require('strapi-utils');
const difference = require('../utils/getObjectDiff');
const configPrefix = 'role-permissions'; // Should be the same as the filename. const configPrefix = 'role-permissions'; // Should be the same as the filename.
@ -15,30 +16,39 @@ module.exports = {
* @returns {void} * @returns {void}
*/ */
exportAll: async () => { exportAll: async () => {
const service = const formattedDiff = {
strapi.plugins['users-permissions'].services.userspermissions; fileConfig: {},
databaseConfig: {},
diff: {}
};
const [roles, plugins] = await Promise.all([ const fileConfig = await strapi.plugins['config-sync'].services.main.getAllConfigFromFiles(configPrefix);
service.getRoles(), const databaseConfig = await strapi.plugins['config-sync'].services.main.getAllConfigFromDatabase(configPrefix);
service.getPlugins(), const diff = difference(databaseConfig, fileConfig);
]);
const rolesWithPermissions = await Promise.all( formattedDiff.diff = diff;
roles.map(async role => service.getRole(role.id, plugins))
);
const sanitizedRolesArray = rolesWithPermissions.map(role => Object.keys(diff).map((changedConfigName) => {
sanitizeEntity(role, { formattedDiff.fileConfig[changedConfigName] = fileConfig[changedConfigName];
model: strapi.plugins['users-permissions'].models.role, formattedDiff.databaseConfig[changedConfigName] = databaseConfig[changedConfigName];
}) })
);
await Promise.all(sanitizedRolesArray.map(async ({id, ...config}) => { await Promise.all(Object.entries(diff).map(async ([configName, config]) => {
// Check if the config should be excluded. // Check if the config should be excluded.
const shouldExclude = strapi.plugins['config-sync'].config.exclude.includes(`${configPrefix}.${config.type}`); const shouldExclude = strapi.plugins['config-sync'].config.exclude.includes(`${configName}`);
if (shouldExclude) return; if (shouldExclude) return;
await strapi.plugins['config-sync'].services.main.writeConfigFile(configPrefix, config.type, config); const currentConfig = formattedDiff.databaseConfig[configName];
if (
!currentConfig &&
formattedDiff.fileConfig[configName]
) {
await strapi.plugins['config-sync'].services.main.deleteConfigFile(configName);
} else {
await strapi.plugins['config-sync'].services.main.writeConfigFile(configPrefix, currentConfig.type, currentConfig);
}
})); }));
}, },

View File

@ -6,6 +6,7 @@
const webhookQueryString = 'strapi_webhooks'; const webhookQueryString = 'strapi_webhooks';
const configPrefix = 'webhooks'; // Should be the same as the filename. const configPrefix = 'webhooks'; // Should be the same as the filename.
const difference = require('../utils/getObjectDiff');
module.exports = { module.exports = {
/** /**
@ -14,14 +15,38 @@ module.exports = {
* @returns {void} * @returns {void}
*/ */
exportAll: async () => { exportAll: async () => {
const webhooks = await strapi.query(webhookQueryString).find({ _limit: -1 }); const formattedDiff = {
fileConfig: {},
databaseConfig: {},
diff: {}
};
await Promise.all(Object.values(webhooks).map(async (config) => { const fileConfig = await strapi.plugins['config-sync'].services.main.getAllConfigFromFiles(configPrefix);
const databaseConfig = await strapi.plugins['config-sync'].services.main.getAllConfigFromDatabase(configPrefix);
const diff = difference(databaseConfig, fileConfig);
formattedDiff.diff = diff;
Object.keys(diff).map((changedConfigName) => {
formattedDiff.fileConfig[changedConfigName] = fileConfig[changedConfigName];
formattedDiff.databaseConfig[changedConfigName] = databaseConfig[changedConfigName];
})
await Promise.all(Object.entries(diff).map(async ([configName, config]) => {
// Check if the config should be excluded. // Check if the config should be excluded.
const shouldExclude = strapi.plugins['config-sync'].config.exclude.includes(`${configPrefix}.${config.id}`); const shouldExclude = strapi.plugins['config-sync'].config.exclude.includes(`${configName}`);
if (shouldExclude) return; if (shouldExclude) return;
await strapi.plugins['config-sync'].services.main.writeConfigFile(configPrefix, config.id, config); const currentConfig = formattedDiff.databaseConfig[configName];
if (
!currentConfig &&
formattedDiff.fileConfig[configName]
) {
await strapi.plugins['config-sync'].services.main.deleteConfigFile(configName);
} else {
await strapi.plugins['config-sync'].services.main.writeConfigFile(configPrefix, currentConfig.id, currentConfig);
}
})); }));
}, },