feat: Dynamically use types based on plugin dependency

pull/31/head
Boaz Poolman 2021-12-10 00:49:12 +01:00
parent b8b187acf7
commit 5cdf4de4fb
2 changed files with 49 additions and 31 deletions

View File

@ -2,7 +2,8 @@
const ConfigType = require("./type"); const ConfigType = require("./type");
module.exports = { const types = (strapi) => {
const typesObject = {
'i18n-locale': new ConfigType('plugin::i18n.locale', 'i18n-locale', 'code'), 'i18n-locale': new ConfigType('plugin::i18n.locale', 'i18n-locale', 'code'),
'core-store': new ConfigType('strapi::core-store', 'core-store', 'key', ['value']), 'core-store': new ConfigType('strapi::core-store', 'core-store', 'key', ['value']),
'user-role': new ConfigType( 'user-role': new ConfigType(
@ -15,7 +16,7 @@ module.exports = {
relationName: 'permissions', relationName: 'permissions',
parentName: 'role', parentName: 'role',
relationSortField: 'action', relationSortField: 'action',
}] }],
), ),
'admin-role': new ConfigType( 'admin-role': new ConfigType(
'admin::role', 'admin::role',
@ -27,6 +28,22 @@ module.exports = {
relationName: 'permissions', relationName: 'permissions',
parentName: 'role', parentName: 'role',
relationSortField: 'action', relationSortField: 'action',
}] }],
), ),
}; };
// Remove types for which the corresponding plugin is not installed.
Object.keys(typesObject).map((type) => {
if (type === 'i18n-locale' && !strapi.plugin('i18n')) {
delete typesObject[type];
}
if (type === 'user-role' && !strapi.plugin('users-permissions')) {
delete typesObject[type];
}
});
return typesObject;
};
module.exports = types;

View File

@ -112,6 +112,7 @@ module.exports = () => ({
if ( if (
configType && configType !== type configType && configType !== type
|| !strapi.config.get('plugin.config-sync.include').includes(type) || !strapi.config.get('plugin.config-sync.include').includes(type)
|| !types(strapi)[type]
|| strapi.config.get('plugin.config-sync.exclude').includes(`${type}.${name}`) || strapi.config.get('plugin.config-sync.exclude').includes(`${type}.${name}`)
) { ) {
return; return;
@ -138,11 +139,11 @@ module.exports = () => ({
let databaseConfigs = {}; let databaseConfigs = {};
await Promise.all(strapi.config.get('plugin.config-sync.include').map(async (type) => { await Promise.all(strapi.config.get('plugin.config-sync.include').map(async (type) => {
if (configType && configType !== type) { if (configType && configType !== type || !types(strapi)[type]) {
return; return;
} }
const config = await types[type].getAllFromDatabase(); const config = await types(strapi)[type].getAllFromDatabase();
databaseConfigs = Object.assign(config, databaseConfigs); databaseConfigs = Object.assign(config, databaseConfigs);
})); }));
@ -218,7 +219,7 @@ module.exports = () => ({
const fileContents = await strapi.plugin('config-sync').service('main').readConfigFile(type, name); const fileContents = await strapi.plugin('config-sync').service('main').readConfigFile(type, name);
try { try {
await types[type].importSingle(name, fileContents); await types(strapi)[type].importSingle(name, fileContents);
if (onSuccess) { if (onSuccess) {
onSuccess(`${type}.${name}`); onSuccess(`${type}.${name}`);
} }
@ -242,7 +243,7 @@ module.exports = () => ({
const [type, name] = configName.split('.'); // Split the configName. const [type, name] = configName.split('.'); // Split the configName.
try { try {
await types[type].exportSingle(configName); await types(strapi)[type].exportSingle(configName);
if (onSuccess) { if (onSuccess) {
onSuccess(`${type}.${name}`); onSuccess(`${type}.${name}`);
} }