feat(deletion): Delete on export
parent
2db6203c64
commit
6223ac42eb
|
@ -2,6 +2,7 @@
|
|||
|
||||
const coreStoreQueryString = 'core_store';
|
||||
const configPrefix = 'core-store'; // Should be the same as the filename.
|
||||
const difference = require('../utils/getObjectDiff');
|
||||
|
||||
/**
|
||||
* Import/Export for core-store configs.
|
||||
|
@ -14,15 +15,38 @@ module.exports = {
|
|||
* @returns {void}
|
||||
*/
|
||||
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.
|
||||
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;
|
||||
|
||||
config.value = JSON.parse(config.value);
|
||||
await strapi.plugins['config-sync'].services.main.writeConfigFile(configPrefix, config.key, 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.key, currentConfig);
|
||||
}
|
||||
}));
|
||||
},
|
||||
|
||||
|
|
|
@ -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.
|
||||
*
|
||||
|
@ -68,7 +82,7 @@ module.exports = {
|
|||
*
|
||||
* @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 getConfigs = async () => {
|
||||
|
@ -77,6 +91,11 @@ module.exports = {
|
|||
await Promise.all(configFiles.map(async (file) => {
|
||||
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.
|
||||
|
||||
if (configType && configType !== type) {
|
||||
return;
|
||||
}
|
||||
|
||||
const fileContents = await strapi.plugins['config-sync'].services.main.readConfigFile(type, name);
|
||||
fileConfigs[`${type}.${name}`] = fileContents;
|
||||
}));
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
'use strict';
|
||||
|
||||
const { sanitizeEntity } = require('strapi-utils');
|
||||
const difference = require('../utils/getObjectDiff');
|
||||
|
||||
const configPrefix = 'role-permissions'; // Should be the same as the filename.
|
||||
|
||||
|
@ -15,30 +16,39 @@ module.exports = {
|
|||
* @returns {void}
|
||||
*/
|
||||
exportAll: async () => {
|
||||
const service =
|
||||
strapi.plugins['users-permissions'].services.userspermissions;
|
||||
const formattedDiff = {
|
||||
fileConfig: {},
|
||||
databaseConfig: {},
|
||||
diff: {}
|
||||
};
|
||||
|
||||
const [roles, plugins] = await Promise.all([
|
||||
service.getRoles(),
|
||||
service.getPlugins(),
|
||||
]);
|
||||
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);
|
||||
|
||||
const rolesWithPermissions = await Promise.all(
|
||||
roles.map(async role => service.getRole(role.id, plugins))
|
||||
);
|
||||
formattedDiff.diff = diff;
|
||||
|
||||
const sanitizedRolesArray = rolesWithPermissions.map(role =>
|
||||
sanitizeEntity(role, {
|
||||
model: strapi.plugins['users-permissions'].models.role,
|
||||
Object.keys(diff).map((changedConfigName) => {
|
||||
formattedDiff.fileConfig[changedConfigName] = fileConfig[changedConfigName];
|
||||
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.
|
||||
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;
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
}));
|
||||
},
|
||||
|
||||
|
|
|
@ -6,6 +6,7 @@
|
|||
|
||||
const webhookQueryString = 'strapi_webhooks';
|
||||
const configPrefix = 'webhooks'; // Should be the same as the filename.
|
||||
const difference = require('../utils/getObjectDiff');
|
||||
|
||||
module.exports = {
|
||||
/**
|
||||
|
@ -14,14 +15,38 @@ module.exports = {
|
|||
* @returns {void}
|
||||
*/
|
||||
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.
|
||||
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;
|
||||
|
||||
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);
|
||||
}
|
||||
}));
|
||||
},
|
||||
|
||||
|
|
Loading…
Reference in New Issue