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");
module.exports = {
'i18n-locale': new ConfigType('plugin::i18n.locale', 'i18n-locale', 'code'),
'core-store': new ConfigType('strapi::core-store', 'core-store', 'key', ['value']),
'user-role': new ConfigType(
'plugin::users-permissions.role',
'user-role',
'type',
[],
[{
queryString: 'plugin::users-permissions.permission',
relationName: 'permissions',
parentName: 'role',
relationSortField: 'action',
}]
),
'admin-role': new ConfigType(
'admin::role',
'admin-role',
'code',
[],
[{
queryString: 'admin::permission',
relationName: 'permissions',
parentName: 'role',
relationSortField: 'action',
}]
),
const types = (strapi) => {
const typesObject = {
'i18n-locale': new ConfigType('plugin::i18n.locale', 'i18n-locale', 'code'),
'core-store': new ConfigType('strapi::core-store', 'core-store', 'key', ['value']),
'user-role': new ConfigType(
'plugin::users-permissions.role',
'user-role',
'type',
[],
[{
queryString: 'plugin::users-permissions.permission',
relationName: 'permissions',
parentName: 'role',
relationSortField: 'action',
}],
),
'admin-role': new ConfigType(
'admin::role',
'admin-role',
'code',
[],
[{
queryString: 'admin::permission',
relationName: 'permissions',
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 (
configType && configType !== type
|| !strapi.config.get('plugin.config-sync.include').includes(type)
|| !types(strapi)[type]
|| strapi.config.get('plugin.config-sync.exclude').includes(`${type}.${name}`)
) {
return;
@ -138,11 +139,11 @@ module.exports = () => ({
let databaseConfigs = {};
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;
}
const config = await types[type].getAllFromDatabase();
const config = await types(strapi)[type].getAllFromDatabase();
databaseConfigs = Object.assign(config, databaseConfigs);
}));
@ -218,7 +219,7 @@ module.exports = () => ({
const fileContents = await strapi.plugin('config-sync').service('main').readConfigFile(type, name);
try {
await types[type].importSingle(name, fileContents);
await types(strapi)[type].importSingle(name, fileContents);
if (onSuccess) {
onSuccess(`${type}.${name}`);
}
@ -242,7 +243,7 @@ module.exports = () => ({
const [type, name] = configName.split('.'); // Split the configName.
try {
await types[type].exportSingle(configName);
await types(strapi)[type].exportSingle(configName);
if (onSuccess) {
onSuccess(`${type}.${name}`);
}