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,31 +2,48 @@
const ConfigType = require("./type"); const ConfigType = require("./type");
module.exports = { const types = (strapi) => {
'i18n-locale': new ConfigType('plugin::i18n.locale', 'i18n-locale', 'code'), const typesObject = {
'core-store': new ConfigType('strapi::core-store', 'core-store', 'key', ['value']), 'i18n-locale': new ConfigType('plugin::i18n.locale', 'i18n-locale', 'code'),
'user-role': new ConfigType( 'core-store': new ConfigType('strapi::core-store', 'core-store', 'key', ['value']),
'plugin::users-permissions.role', 'user-role': new ConfigType(
'user-role', 'plugin::users-permissions.role',
'type', 'user-role',
[], 'type',
[{ [],
queryString: 'plugin::users-permissions.permission', [{
relationName: 'permissions', queryString: 'plugin::users-permissions.permission',
parentName: 'role', relationName: 'permissions',
relationSortField: 'action', parentName: 'role',
}] relationSortField: 'action',
), }],
'admin-role': new ConfigType( ),
'admin::role', 'admin-role': new ConfigType(
'admin-role', 'admin::role',
'code', 'admin-role',
[], 'code',
[{ [],
queryString: 'admin::permission', [{
relationName: 'permissions', queryString: 'admin::permission',
parentName: 'role', relationName: 'permissions',
relationSortField: 'action', parentName: 'role',
}] 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}`);
} }