mirror of
https://github.com/strapi/strapi.git
synced 2025-11-01 02:16:03 +00:00
WIP
This commit is contained in:
parent
f97c775566
commit
bdd75d9154
@ -9,11 +9,11 @@ dotenv.config({ path: process.env.ENV_PATH });
|
||||
|
||||
process.env.NODE_ENV = process.env.NODE_ENV || 'development';
|
||||
|
||||
const createConfigProvider = require('../../utils/config-provider');
|
||||
const getPrefixedDeps = require('../../utils/get-prefixed-dependencies');
|
||||
const loadPolicies = require('../load-policies');
|
||||
const loadFunctions = require('../load-functions');
|
||||
const loadConfigDir = require('./config-loader');
|
||||
const createConfigProvider = require('./config-provider');
|
||||
|
||||
const { version: strapiVersion } = require(path.join(__dirname, '../../../package.json'));
|
||||
|
||||
|
||||
26
packages/core/strapi/lib/core/domain/config.js
Normal file
26
packages/core/strapi/lib/core/domain/config.js
Normal file
@ -0,0 +1,26 @@
|
||||
'use strict'
|
||||
|
||||
const { defaultsDeep, getOr, set, has } = require('lodash/fp');
|
||||
|
||||
const createConfig = (config = {}, defaultConfig = {}) => {
|
||||
const currentConfig = defaultsDeep(defaultConfig, config);
|
||||
|
||||
return Object.assign(currentConfig, {
|
||||
get(path, defaultValue) {
|
||||
return getOr(defaultValue, path, currentConfig);
|
||||
},
|
||||
|
||||
set(path, val) {
|
||||
set(path, val, currentConfig);
|
||||
return this;
|
||||
},
|
||||
|
||||
has(path) {
|
||||
return has(path, currentConfig);
|
||||
},
|
||||
});
|
||||
};
|
||||
|
||||
module.exports = {
|
||||
createConfig,
|
||||
};
|
||||
49
packages/core/strapi/lib/core/domain/content-type.js
Normal file
49
packages/core/strapi/lib/core/domain/content-type.js
Normal file
@ -0,0 +1,49 @@
|
||||
'use strict'
|
||||
|
||||
const { cloneDeep, camelCase } = require('lodash/fp');
|
||||
|
||||
const createContentType = (model, { apiName, pluginName } = {}) => {
|
||||
// todo : validate schema with yup
|
||||
const createdContentType = cloneDeep(model);
|
||||
const singularModelName = camelCase(model.singularName);
|
||||
const pluralModelName = camelCase(model.pluralName);
|
||||
|
||||
if (apiName) {
|
||||
Object.assign(createdContentType, {
|
||||
uid: `application::${apiName}.${singularModelName}`,
|
||||
apiName,
|
||||
collectionName: model.collectionName || singularModelName,
|
||||
});
|
||||
} else if (pluginName) {
|
||||
Object.assign(createdContentType, {
|
||||
uid: `plugins::${pluginName}.${singularModelName}`,
|
||||
plugin: pluginName,
|
||||
collectionName:
|
||||
createdContentType.collectionName || `${pluginName}_${singularModelName}`.toLowerCase(),
|
||||
});
|
||||
} else {
|
||||
Object.assign(createdContentType, {
|
||||
uid: `strapi::${singularModelName}`,
|
||||
plugin: 'admin',
|
||||
});
|
||||
}
|
||||
|
||||
Object.assign(createdContentType, {
|
||||
kind: createdContentType.kind || 'collectionType',
|
||||
modelType: 'contentType',
|
||||
modelName: singularModelName,
|
||||
singularName: singularModelName,
|
||||
pluralName: pluralModelName,
|
||||
});
|
||||
Object.defineProperty(createdContentType, 'privateAttributes', {
|
||||
get() {
|
||||
return strapi.getModel(createdContentType.uid).privateAttributes;
|
||||
},
|
||||
});
|
||||
|
||||
return createdContentType;
|
||||
};
|
||||
|
||||
module.exports = {
|
||||
createContentType,
|
||||
}
|
||||
@ -1,21 +1,21 @@
|
||||
'use strict';
|
||||
|
||||
const loadApis = require('./load-apis');
|
||||
const loadAdmin = require('./load-admin');
|
||||
// const loadPlugins = require('./load-plugins');
|
||||
const loadMiddlewares = require('./load-middlewares');
|
||||
const loadExtensions = require('./load-extensions');
|
||||
const loadHooks = require('./load-hooks');
|
||||
const bootstrap = require('./bootstrap');
|
||||
const loadComponents = require('./load-components');
|
||||
|
||||
module.exports = {
|
||||
loadApis,
|
||||
loadAdmin,
|
||||
// loadPlugins,
|
||||
loadMiddlewares,
|
||||
loadHooks,
|
||||
loadExtensions,
|
||||
loadComponents,
|
||||
bootstrap,
|
||||
};
|
||||
// 'use strict';
|
||||
//
|
||||
// const loadApis = require('./load-apis');
|
||||
// const loadAdmin = require('./load-admin');
|
||||
// // const loadPlugins = require('./load-plugins');
|
||||
// const loadMiddlewares = require('./load-middlewares');
|
||||
// const loadExtensions = require('./load-extensions');
|
||||
// const loadHooks = require('./load-hooks');
|
||||
// const bootstrap = require('./bootstrap');
|
||||
// const loadComponents = require('./load-components');
|
||||
//
|
||||
// module.exports = {
|
||||
// loadApis,
|
||||
// loadAdmin,
|
||||
// // loadPlugins,
|
||||
// loadMiddlewares,
|
||||
// loadHooks,
|
||||
// loadExtensions,
|
||||
// loadComponents,
|
||||
// bootstrap,
|
||||
// };
|
||||
|
||||
15
packages/core/strapi/lib/core/plugins/config-provider.js
Normal file
15
packages/core/strapi/lib/core/plugins/config-provider.js
Normal file
@ -0,0 +1,15 @@
|
||||
'use strict'
|
||||
|
||||
const { env } = require('@strapi/utils');
|
||||
const { isFunction } = require('lodash/fp');
|
||||
const createConfig = require('../domain/config');
|
||||
|
||||
module.exports = async (pluginName, pluginDefaultConfig) => {
|
||||
const defaultConfig = isFunction(pluginDefaultConfig)
|
||||
? await pluginDefaultConfig({ env })
|
||||
: pluginDefaultConfig;
|
||||
|
||||
const userPluginConfig = strapi.config.get(`plugins.${pluginName}`);
|
||||
|
||||
return createConfig(userPluginConfig, defaultConfig);
|
||||
};
|
||||
@ -0,0 +1,23 @@
|
||||
'use strict'
|
||||
|
||||
const { createContentType } = require('@strapi/utils').contentTypes;
|
||||
|
||||
module.exports = (pluginName, contentTypeDefinitions) => {
|
||||
const contentTypes = contentTypeDefinitions.map(ct =>
|
||||
createContentType(ct, { pluginName })
|
||||
);
|
||||
|
||||
const contentTypesMap = contentTypes.reduce((map, ct) => {
|
||||
map[ct.info.singularName] = ct;
|
||||
map[ct.info.pluralName] = ct;
|
||||
}, {});
|
||||
|
||||
return {
|
||||
get(ctName) {
|
||||
return contentTypesMap[ctName];
|
||||
},
|
||||
getAll() {
|
||||
return contentTypes;
|
||||
}
|
||||
}
|
||||
};
|
||||
@ -1,39 +1,29 @@
|
||||
'use strict';
|
||||
|
||||
const { join } = require('path');
|
||||
const { isFunction, defaultsDeep } = require('lodash/fp');
|
||||
const { env } = require('@strapi/utils');
|
||||
const { createContentType } = require('@strapi/utils').contentTypes;
|
||||
const { validateStrapiServer } = require('./validation');
|
||||
const createConfigProvider = require('./config-provider');
|
||||
const createServiceProvider = require('./service-provider');
|
||||
const createContentTypeProvider = require('./content-type-provider');
|
||||
|
||||
const createPlugin = async (strapi, name, path) => {
|
||||
const loadPluginServer = require(join(path, 'strapi-server.js'));
|
||||
const userPluginConfig = strapi.config.get(`plugins.${name}`);
|
||||
const pluginServer = await loadPluginServer(strapi);
|
||||
const cleanPluginServer = await validateStrapiServer(pluginServer);
|
||||
|
||||
await validateStrapiServer(pluginServer);
|
||||
|
||||
const defaultConfig = isFunction(pluginServer.config)
|
||||
? await pluginServer.config({ env })
|
||||
: pluginServer.config;
|
||||
|
||||
const contentTypes = pluginServer.contentTypes.map(ct =>
|
||||
createContentType(ct, { pluginName: name })
|
||||
);
|
||||
const contentTypesMap = contentTypes.reduce((map, ct) => {
|
||||
map[ct.info.singularName] = ct;
|
||||
map[ct.info.pluralName] = ct;
|
||||
}, {});
|
||||
const configProvider = await createConfigProvider(name, cleanPluginServer.config);
|
||||
const serviceProvider = await createServiceProvider(cleanPluginServer.services);
|
||||
const contentTypeProvider = await createContentTypeProvider(name, cleanPluginServer.contentTypes);
|
||||
|
||||
return {
|
||||
bootstrap: pluginServer.bootstrap,
|
||||
destroy: pluginServer.destroy,
|
||||
config: defaultsDeep(defaultConfig, userPluginConfig),
|
||||
// routes: pluginServer.routes,
|
||||
// controller: (name) => pluginServer.controllers[name],
|
||||
service: name => pluginServer.services[name],
|
||||
contentType: name => contentTypesMap[name],
|
||||
getAllContentTypes: () => Object.values(contentTypes),
|
||||
bootstrap: cleanPluginServer.bootstrap,
|
||||
destroy: cleanPluginServer.destroy,
|
||||
config: configProvider,
|
||||
service: serviceProvider.get,
|
||||
contentType: contentTypeProvider.get,
|
||||
getAllContentTypes: contentTypeProvider.getAll,
|
||||
// routes: cleanPluginServer.routes,
|
||||
// controller: (name) => cleanPluginServer.controllers[name],
|
||||
};
|
||||
};
|
||||
|
||||
|
||||
@ -0,0 +1,8 @@
|
||||
'use strict'
|
||||
module.exports = (services) => {
|
||||
return {
|
||||
get(serviceName) {
|
||||
return services[serviceName];
|
||||
}
|
||||
}
|
||||
};
|
||||
@ -5,21 +5,21 @@ const { yup } = require('@strapi/utils');
|
||||
const strapiServerSchema = yup
|
||||
.object()
|
||||
.shape({
|
||||
bootstrap: yup.mixed().isFunction(),
|
||||
destroy: yup.mixed().isFunction(),
|
||||
config: yup.object(),
|
||||
routes: yup.array(), // may be removed later
|
||||
bootstrap: yup.mixed().isFunction().default(() => {}),
|
||||
destroy: yup.mixed().isFunction().default(() => {}),
|
||||
config: yup.object().default({}),
|
||||
routes: yup.array().default([]), // may be removed later
|
||||
controllers: yup.object(), // may be removed later
|
||||
services: yup.object(),
|
||||
policies: yup.object(),
|
||||
middlewares: yup.object(), // may be removed later
|
||||
hooks: yup.object(), // may be removed later
|
||||
contentTypes: yup.array().of(yup.object()),
|
||||
services: yup.object().default({}),
|
||||
policies: yup.object().default({}),
|
||||
middlewares: yup.object().default({}), // may be removed later
|
||||
hooks: yup.object().default({}), // may be removed later
|
||||
contentTypes: yup.array().of(yup.object()).default([]),
|
||||
})
|
||||
.noUnknown();
|
||||
|
||||
const validateStrapiServer = data => {
|
||||
return strapiServerSchema.validate(data, { strict: true, abortEarly: false });
|
||||
return strapiServerSchema.validate(data, { strict: false, abortEarly: false });
|
||||
};
|
||||
|
||||
module.exports = {
|
||||
|
||||
@ -1,15 +1,9 @@
|
||||
'use strict';
|
||||
|
||||
const assert = require('assert');
|
||||
const _ = require('lodash');
|
||||
|
||||
module.exports = (initialConfig = {}) => {
|
||||
assert(
|
||||
typeof initialConfig === 'object' && initialConfig !== null,
|
||||
'Initial config must be an object'
|
||||
);
|
||||
|
||||
const _config = initialConfig;
|
||||
const _config = _.cloneDeep(initialConfig);
|
||||
|
||||
return Object.assign(_config, {
|
||||
get(path, defaultValue) {
|
||||
@ -2,7 +2,6 @@
|
||||
|
||||
const _ = require('lodash');
|
||||
const pluralize = require('pluralize');
|
||||
const { nameToSlug } = require('./string-formatting');
|
||||
|
||||
const SINGLE_TYPE = 'singleType';
|
||||
const COLLECTION_TYPE = 'collectionType';
|
||||
@ -124,76 +123,6 @@ const isMediaAttribute = attr => {
|
||||
return (attr.collection || attr.model) === 'file' && attr.plugin === 'upload';
|
||||
};
|
||||
|
||||
const getKind = obj => obj.kind || 'collectionType';
|
||||
|
||||
const pickSchema = model => {
|
||||
const schema = _.cloneDeep(
|
||||
_.pick(model, [
|
||||
'connection',
|
||||
'collectionName',
|
||||
'info',
|
||||
'options',
|
||||
'pluginOptions',
|
||||
'attributes',
|
||||
])
|
||||
);
|
||||
|
||||
schema.kind = getKind(model);
|
||||
return schema;
|
||||
};
|
||||
|
||||
const createContentType = (model, { apiName, pluginName } = {}) => {
|
||||
// todo : validate schema with yup
|
||||
const createdContentType = _.cloneDeep(model);
|
||||
const singularModelName = nameToSlug(model.singularName);
|
||||
const pluralModelName = nameToSlug(model.pluralName);
|
||||
|
||||
if (apiName) {
|
||||
Object.assign(createdContentType, {
|
||||
uid: `application::${apiName}.${singularModelName}`,
|
||||
apiName,
|
||||
collectionName: model.collectionName || singularModelName,
|
||||
globalId: getGlobalId(createdContentType, singularModelName),
|
||||
});
|
||||
} else if (pluginName) {
|
||||
Object.assign(createdContentType, {
|
||||
uid: `plugins::${pluginName}.${singularModelName}`,
|
||||
plugin: pluginName,
|
||||
collectionName:
|
||||
createdContentType.collectionName || `${pluginName}_${singularModelName}`.toLowerCase(),
|
||||
globalId: getGlobalId(createdContentType, singularModelName, pluginName),
|
||||
});
|
||||
} else {
|
||||
Object.assign(createdContentType, {
|
||||
uid: `strapi::${singularModelName}`,
|
||||
plugin: 'admin',
|
||||
globalId: getGlobalId(createdContentType, singularModelName, 'admin'),
|
||||
});
|
||||
}
|
||||
|
||||
Object.assign(createdContentType, {
|
||||
__schema__: pickSchema(createdContentType),
|
||||
kind: getKind(createdContentType),
|
||||
modelType: 'contentType',
|
||||
modelName: singularModelName,
|
||||
singularName: singularModelName,
|
||||
pluralName: pluralModelName,
|
||||
});
|
||||
Object.defineProperty(createdContentType, 'privateAttributes', {
|
||||
get() {
|
||||
return strapi.getModel(createdContentType.uid).privateAttributes;
|
||||
},
|
||||
});
|
||||
|
||||
return createdContentType;
|
||||
};
|
||||
|
||||
const getGlobalId = (model, modelName, prefix) => {
|
||||
let globalId = prefix ? `${prefix}-${modelName}` : modelName;
|
||||
|
||||
return model.globalId || _.upperFirst(_.camelCase(globalId));
|
||||
};
|
||||
|
||||
const isRelationalAttribute = attribute =>
|
||||
_.has(attribute, 'model') || _.has(attribute, 'collection');
|
||||
|
||||
@ -237,7 +166,5 @@ module.exports = {
|
||||
isSingleType,
|
||||
isCollectionType,
|
||||
isKind,
|
||||
createContentType,
|
||||
getGlobalId,
|
||||
getContentTypeRoutePrefix,
|
||||
};
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user