Skip to content

Commit 5cdf4de

Browse files
committed
feat: Dynamically use types based on plugin dependency
1 parent b8b187a commit 5cdf4de

File tree

2 files changed

+49
-31
lines changed

2 files changed

+49
-31
lines changed

server/config/types.js

Lines changed: 44 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,31 +2,48 @@
22

33
const ConfigType = require("./type");
44

5-
module.exports = {
6-
'i18n-locale': new ConfigType('plugin::i18n.locale', 'i18n-locale', 'code'),
7-
'core-store': new ConfigType('strapi::core-store', 'core-store', 'key', ['value']),
8-
'user-role': new ConfigType(
9-
'plugin::users-permissions.role',
10-
'user-role',
11-
'type',
12-
[],
13-
[{
14-
queryString: 'plugin::users-permissions.permission',
15-
relationName: 'permissions',
16-
parentName: 'role',
17-
relationSortField: 'action',
18-
}]
19-
),
20-
'admin-role': new ConfigType(
21-
'admin::role',
22-
'admin-role',
23-
'code',
24-
[],
25-
[{
26-
queryString: 'admin::permission',
27-
relationName: 'permissions',
28-
parentName: 'role',
29-
relationSortField: 'action',
30-
}]
31-
),
5+
const types = (strapi) => {
6+
const typesObject = {
7+
'i18n-locale': new ConfigType('plugin::i18n.locale', 'i18n-locale', 'code'),
8+
'core-store': new ConfigType('strapi::core-store', 'core-store', 'key', ['value']),
9+
'user-role': new ConfigType(
10+
'plugin::users-permissions.role',
11+
'user-role',
12+
'type',
13+
[],
14+
[{
15+
queryString: 'plugin::users-permissions.permission',
16+
relationName: 'permissions',
17+
parentName: 'role',
18+
relationSortField: 'action',
19+
}],
20+
),
21+
'admin-role': new ConfigType(
22+
'admin::role',
23+
'admin-role',
24+
'code',
25+
[],
26+
[{
27+
queryString: 'admin::permission',
28+
relationName: 'permissions',
29+
parentName: 'role',
30+
relationSortField: 'action',
31+
}],
32+
),
33+
};
34+
35+
// Remove types for which the corresponding plugin is not installed.
36+
Object.keys(typesObject).map((type) => {
37+
if (type === 'i18n-locale' && !strapi.plugin('i18n')) {
38+
delete typesObject[type];
39+
}
40+
41+
if (type === 'user-role' && !strapi.plugin('users-permissions')) {
42+
delete typesObject[type];
43+
}
44+
});
45+
46+
return typesObject;
3247
};
48+
49+
module.exports = types;

server/services/main.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ module.exports = () => ({
112112
if (
113113
configType && configType !== type
114114
|| !strapi.config.get('plugin.config-sync.include').includes(type)
115+
|| !types(strapi)[type]
115116
|| strapi.config.get('plugin.config-sync.exclude').includes(`${type}.${name}`)
116117
) {
117118
return;
@@ -138,11 +139,11 @@ module.exports = () => ({
138139
let databaseConfigs = {};
139140

140141
await Promise.all(strapi.config.get('plugin.config-sync.include').map(async (type) => {
141-
if (configType && configType !== type) {
142+
if (configType && configType !== type || !types(strapi)[type]) {
142143
return;
143144
}
144145

145-
const config = await types[type].getAllFromDatabase();
146+
const config = await types(strapi)[type].getAllFromDatabase();
146147
databaseConfigs = Object.assign(config, databaseConfigs);
147148
}));
148149

@@ -218,7 +219,7 @@ module.exports = () => ({
218219
const fileContents = await strapi.plugin('config-sync').service('main').readConfigFile(type, name);
219220

220221
try {
221-
await types[type].importSingle(name, fileContents);
222+
await types(strapi)[type].importSingle(name, fileContents);
222223
if (onSuccess) {
223224
onSuccess(`${type}.${name}`);
224225
}
@@ -242,7 +243,7 @@ module.exports = () => ({
242243
const [type, name] = configName.split('.'); // Split the configName.
243244

244245
try {
245-
await types[type].exportSingle(configName);
246+
await types(strapi)[type].exportSingle(configName);
246247
if (onSuccess) {
247248
onSuccess(`${type}.${name}`);
248249
}

0 commit comments

Comments
 (0)