2020-10-27 11:27:17 +01:00
|
|
|
'use strict';
|
|
|
|
|
2020-11-13 16:48:17 +01:00
|
|
|
// eslint-disable-next-line node/no-extraneous-require
|
2020-11-17 15:38:41 +01:00
|
|
|
const { isFunction, isNil } = require('lodash/fp');
|
2020-11-10 14:15:31 +01:00
|
|
|
const { createStrapiInstance } = require('./strapi');
|
|
|
|
|
2020-11-17 15:38:41 +01:00
|
|
|
const createHelpers = async ({ strapi: strapiInstance = null, ...options } = {}) => {
|
|
|
|
const strapi = strapiInstance || (await createStrapiInstance(options));
|
2020-11-13 16:48:17 +01:00
|
|
|
const contentTypeService = strapi.plugins['content-type-builder'].services.contenttypes;
|
|
|
|
const componentsService = strapi.plugins['content-type-builder'].services.components;
|
|
|
|
|
2020-11-17 15:38:41 +01:00
|
|
|
const cleanup = async () => {
|
|
|
|
if (isNil(strapiInstance)) {
|
|
|
|
await strapi.destroy();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-11-13 16:48:17 +01:00
|
|
|
return {
|
|
|
|
strapi,
|
|
|
|
contentTypeService,
|
|
|
|
componentsService,
|
2020-11-17 15:38:41 +01:00
|
|
|
cleanup,
|
2020-11-13 16:48:17 +01:00
|
|
|
};
|
2020-11-10 14:15:31 +01:00
|
|
|
};
|
2019-08-08 11:59:45 +02:00
|
|
|
|
2020-11-17 15:38:41 +01:00
|
|
|
const createContentType = async (model, { strapi } = {}) => {
|
|
|
|
const { contentTypeService, cleanup } = await createHelpers({ strapi });
|
2019-08-08 11:59:45 +02:00
|
|
|
|
2020-11-10 14:15:31 +01:00
|
|
|
const contentType = await contentTypeService.createContentType({
|
|
|
|
contentType: {
|
2019-08-08 10:41:05 +02:00
|
|
|
connection: 'default',
|
2020-11-10 14:15:31 +01:00
|
|
|
...model,
|
2020-11-13 16:48:17 +01:00
|
|
|
},
|
2020-11-10 14:15:31 +01:00
|
|
|
});
|
2019-08-08 10:41:05 +02:00
|
|
|
|
2020-11-17 15:38:41 +01:00
|
|
|
await cleanup();
|
2019-05-06 15:33:25 +02:00
|
|
|
|
2020-11-10 14:15:31 +01:00
|
|
|
return contentType;
|
|
|
|
};
|
|
|
|
|
2020-11-17 15:38:41 +01:00
|
|
|
const createContentTypes = async (models, { strapi } = {}) => {
|
|
|
|
const { contentTypeService, cleanup } = await createHelpers({ strapi });
|
2020-11-10 14:15:31 +01:00
|
|
|
|
2020-11-13 16:48:17 +01:00
|
|
|
const contentTypes = await contentTypeService.createContentTypes(
|
|
|
|
models.map(model => ({
|
|
|
|
contentType: {
|
|
|
|
connection: 'default',
|
|
|
|
...model,
|
|
|
|
},
|
|
|
|
}))
|
|
|
|
);
|
2019-05-06 15:33:25 +02:00
|
|
|
|
2020-11-17 15:38:41 +01:00
|
|
|
await cleanup();
|
2020-09-07 15:04:20 +02:00
|
|
|
|
2020-11-10 14:15:31 +01:00
|
|
|
return contentTypes;
|
|
|
|
};
|
|
|
|
|
2020-11-17 15:38:41 +01:00
|
|
|
const createComponent = async (component, { strapi } = {}) => {
|
|
|
|
const { componentsService, cleanup } = await createHelpers({ strapi });
|
2020-11-10 14:15:31 +01:00
|
|
|
|
|
|
|
const createdComponent = await componentsService.createComponent({
|
|
|
|
component: {
|
|
|
|
category: 'default',
|
|
|
|
icon: 'default',
|
|
|
|
connection: 'default',
|
|
|
|
...component,
|
2020-11-13 16:48:17 +01:00
|
|
|
},
|
2020-11-10 14:15:31 +01:00
|
|
|
});
|
|
|
|
|
2020-11-17 15:38:41 +01:00
|
|
|
await cleanup();
|
2020-09-07 15:04:20 +02:00
|
|
|
|
2020-11-10 14:15:31 +01:00
|
|
|
return createdComponent;
|
|
|
|
};
|
2020-09-07 15:04:20 +02:00
|
|
|
|
2020-11-17 15:38:41 +01:00
|
|
|
const createComponents = async (components, { strapi } = {}) => {
|
2020-11-10 14:15:31 +01:00
|
|
|
const createdComponents = [];
|
2020-09-07 15:04:20 +02:00
|
|
|
|
2020-11-10 14:15:31 +01:00
|
|
|
for (const component of components) {
|
2020-11-17 15:38:41 +01:00
|
|
|
createdComponents.push(await createComponent(component, { strapi }));
|
2020-09-07 15:04:20 +02:00
|
|
|
}
|
|
|
|
|
2020-11-10 14:15:31 +01:00
|
|
|
return createdComponents;
|
|
|
|
};
|
|
|
|
|
2020-11-17 15:38:41 +01:00
|
|
|
const deleteComponent = async (componentUID, { strapi } = {}) => {
|
|
|
|
const { componentsService, cleanup } = await createHelpers({ strapi });
|
2020-11-10 14:15:31 +01:00
|
|
|
|
|
|
|
const component = await componentsService.deleteComponent(componentUID);
|
|
|
|
|
2020-11-17 15:38:41 +01:00
|
|
|
await cleanup();
|
2020-11-10 14:15:31 +01:00
|
|
|
|
|
|
|
return component;
|
|
|
|
};
|
|
|
|
|
2020-11-17 15:38:41 +01:00
|
|
|
const deleteComponents = async (componentsUID, { strapi } = {}) => {
|
2020-11-10 14:15:31 +01:00
|
|
|
const deletedComponents = [];
|
2019-05-06 15:33:25 +02:00
|
|
|
|
2020-11-10 14:15:31 +01:00
|
|
|
for (const componentUID of componentsUID) {
|
2020-11-17 15:38:41 +01:00
|
|
|
deletedComponents.push(await deleteComponent(componentUID, { strapi }));
|
2019-05-06 15:33:25 +02:00
|
|
|
}
|
|
|
|
|
2020-11-10 14:15:31 +01:00
|
|
|
return deletedComponents;
|
|
|
|
};
|
|
|
|
|
2020-11-17 15:38:41 +01:00
|
|
|
const deleteContentType = async (modelName, { strapi } = {}) => {
|
|
|
|
const { contentTypeService, cleanup } = await createHelpers({ strapi });
|
2020-11-10 14:15:31 +01:00
|
|
|
const uid = `application::${modelName}.${modelName}`;
|
|
|
|
|
|
|
|
const contentType = await contentTypeService.deleteContentType(uid);
|
|
|
|
|
2020-11-17 15:38:41 +01:00
|
|
|
await cleanup();
|
2020-11-10 14:15:31 +01:00
|
|
|
|
|
|
|
return contentType;
|
|
|
|
};
|
|
|
|
|
2020-11-17 15:38:41 +01:00
|
|
|
const deleteContentTypes = async (modelsName, { strapi } = {}) => {
|
|
|
|
const { contentTypeService, cleanup } = await createHelpers({ strapi });
|
2020-11-10 14:15:31 +01:00
|
|
|
const toUID = name => `application::${name}.${name}`;
|
|
|
|
|
|
|
|
const contentTypes = await contentTypeService.deleteContentTypes(modelsName.map(toUID));
|
|
|
|
|
2020-11-17 15:38:41 +01:00
|
|
|
await cleanup();
|
2020-11-10 14:15:31 +01:00
|
|
|
|
|
|
|
return contentTypes;
|
|
|
|
};
|
|
|
|
|
2020-11-17 15:38:41 +01:00
|
|
|
async function cleanupModels(models, { strapi } = {}) {
|
2020-11-10 14:15:31 +01:00
|
|
|
for (const model of models) {
|
2020-11-17 15:38:41 +01:00
|
|
|
await cleanupModel(model, { strapi });
|
2019-05-06 15:33:25 +02:00
|
|
|
}
|
2020-11-10 14:15:31 +01:00
|
|
|
}
|
|
|
|
|
2020-11-17 15:38:41 +01:00
|
|
|
async function cleanupModel(model, { strapi: strapiIst } = {}) {
|
|
|
|
const { strapi, cleanup } = await createHelpers({ strapi: strapiIst });
|
2020-11-10 14:15:31 +01:00
|
|
|
|
|
|
|
await strapi.query(model).delete();
|
2020-11-17 15:38:41 +01:00
|
|
|
|
|
|
|
await cleanup();
|
2020-11-10 14:15:31 +01:00
|
|
|
}
|
|
|
|
|
2020-11-17 15:38:41 +01:00
|
|
|
async function createFixtures(dataMap, { strapi: strapiIst } = {}) {
|
|
|
|
const { strapi, cleanup } = await createHelpers({ strapi: strapiIst });
|
2020-11-10 14:15:31 +01:00
|
|
|
const models = Object.keys(dataMap);
|
|
|
|
const resultMap = {};
|
2019-05-06 15:33:25 +02:00
|
|
|
|
2020-11-10 14:15:31 +01:00
|
|
|
for (const model of models) {
|
|
|
|
const entries = [];
|
|
|
|
|
|
|
|
for (const data of dataMap[model]) {
|
|
|
|
entries.push(await strapi.query(model).create(data));
|
2020-09-16 10:08:18 +02:00
|
|
|
}
|
2020-11-10 14:15:31 +01:00
|
|
|
|
|
|
|
resultMap[model] = entries;
|
2020-09-16 10:08:18 +02:00
|
|
|
}
|
|
|
|
|
2020-11-17 15:38:41 +01:00
|
|
|
await cleanup();
|
2020-11-10 14:15:31 +01:00
|
|
|
|
|
|
|
return resultMap;
|
|
|
|
}
|
|
|
|
|
2020-11-17 15:38:41 +01:00
|
|
|
async function createFixturesFor(model, entries, { strapi: strapiIst } = {}) {
|
|
|
|
const { strapi, cleanup } = await createHelpers({ strapi: strapiIst });
|
2020-11-10 14:15:31 +01:00
|
|
|
const results = [];
|
|
|
|
|
|
|
|
for (const entry of entries) {
|
2020-11-13 16:48:17 +01:00
|
|
|
const dataToCreate = isFunction(entry) ? entry(results) : entry;
|
|
|
|
results.push(await strapi.query(model).create(dataToCreate));
|
2020-09-16 10:08:18 +02:00
|
|
|
}
|
|
|
|
|
2020-11-17 15:38:41 +01:00
|
|
|
await cleanup();
|
2020-11-10 14:15:31 +01:00
|
|
|
|
|
|
|
return results;
|
|
|
|
}
|
|
|
|
|
2020-11-17 15:38:41 +01:00
|
|
|
async function modifyContentType(data, { strapi } = {}) {
|
|
|
|
const { contentTypeService, cleanup } = await createHelpers({ strapi });
|
|
|
|
|
|
|
|
const sanitizedData = { ...data };
|
|
|
|
delete sanitizedData.editable;
|
|
|
|
delete sanitizedData.restrictRelationsTo;
|
|
|
|
|
|
|
|
const uid = `application::${sanitizedData.name}.${sanitizedData.name}`;
|
|
|
|
|
|
|
|
const ct = await contentTypeService.editContentType(uid, {
|
|
|
|
contentType: {
|
|
|
|
connection: 'default',
|
|
|
|
...sanitizedData,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
await cleanup();
|
|
|
|
|
|
|
|
return ct;
|
|
|
|
}
|
|
|
|
|
|
|
|
async function getContentTypeSchema(modelName, { strapi: strapiIst } = {}) {
|
|
|
|
const { strapi, contentTypeService, cleanup } = await createHelpers({ strapi: strapiIst });
|
|
|
|
|
|
|
|
const uid = `application::${modelName}.${modelName}`;
|
|
|
|
const ct = contentTypeService.formatContentType(strapi.contentTypes[uid]);
|
|
|
|
|
|
|
|
await cleanup();
|
|
|
|
|
|
|
|
return (ct || {}).schema;
|
|
|
|
}
|
|
|
|
|
2020-11-10 14:15:31 +01:00
|
|
|
module.exports = {
|
|
|
|
// Create Content-Types
|
|
|
|
createContentType,
|
|
|
|
createContentTypes,
|
|
|
|
// Delete Content-Types
|
|
|
|
deleteContentType,
|
|
|
|
deleteContentTypes,
|
|
|
|
// Cleanup Models
|
|
|
|
cleanupModel,
|
|
|
|
cleanupModels,
|
|
|
|
// Create Components
|
|
|
|
createComponent,
|
|
|
|
createComponents,
|
|
|
|
// Delete Components
|
|
|
|
deleteComponent,
|
|
|
|
deleteComponents,
|
|
|
|
// Fixtures
|
|
|
|
createFixtures,
|
|
|
|
createFixturesFor,
|
2020-11-17 15:38:41 +01:00
|
|
|
// Update Content-Types
|
|
|
|
modifyContentType,
|
|
|
|
// Misc
|
|
|
|
getContentTypeSchema,
|
2019-05-06 15:33:25 +02:00
|
|
|
};
|