2020-10-27 11:27:17 +01:00
|
|
|
'use strict';
|
|
|
|
|
2020-11-10 14:15:31 +01:00
|
|
|
const { createStrapiInstance } = require('./strapi');
|
|
|
|
|
|
|
|
const createHelpers = async options => {
|
|
|
|
try {
|
|
|
|
console.log(`We're here`)
|
|
|
|
const strapi = await createStrapiInstance(options);
|
|
|
|
const contentTypeService = strapi.plugins['content-type-builder'].services.contenttypes;
|
|
|
|
const componentsService = strapi.plugins['content-type-builder'].services.components;
|
|
|
|
|
|
|
|
return {
|
|
|
|
strapi,
|
|
|
|
contentTypeService,
|
|
|
|
componentsService,
|
|
|
|
};
|
|
|
|
} catch (e) {
|
|
|
|
console.log(e);
|
2019-08-08 11:59:45 +02:00
|
|
|
}
|
2020-11-10 14:15:31 +01:00
|
|
|
};
|
2019-08-08 11:59:45 +02:00
|
|
|
|
2020-11-10 14:15:31 +01:00
|
|
|
const createContentType = async model => {
|
|
|
|
const { contentTypeService, strapi } = await createHelpers();
|
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,
|
|
|
|
}
|
|
|
|
});
|
2019-08-08 10:41:05 +02:00
|
|
|
|
2020-11-10 14:15:31 +01:00
|
|
|
await strapi.destroy();
|
2019-05-06 15:33:25 +02:00
|
|
|
|
2020-11-10 14:15:31 +01:00
|
|
|
return contentType;
|
|
|
|
};
|
|
|
|
|
|
|
|
const createContentTypes = async models => {
|
|
|
|
const { contentTypeService, strapi } = await createHelpers();
|
|
|
|
|
|
|
|
const contentTypes = await contentTypeService.createContentTypes(models.map(model => ({
|
|
|
|
contentType: {
|
|
|
|
connection: 'default',
|
|
|
|
...model,
|
2019-05-06 15:33:25 +02:00
|
|
|
}
|
2020-11-10 14:15:31 +01:00
|
|
|
})));
|
2019-05-06 15:33:25 +02:00
|
|
|
|
2020-11-10 14:15:31 +01:00
|
|
|
await strapi.destroy();
|
2020-09-07 15:04:20 +02:00
|
|
|
|
2020-11-10 14:15:31 +01:00
|
|
|
return contentTypes;
|
|
|
|
};
|
|
|
|
|
|
|
|
const createComponent = async component => {
|
|
|
|
const { componentsService, strapi } = await createHelpers();
|
|
|
|
|
|
|
|
const createdComponent = await componentsService.createComponent({
|
|
|
|
component: {
|
|
|
|
category: 'default',
|
|
|
|
icon: 'default',
|
|
|
|
connection: 'default',
|
|
|
|
...component,
|
2020-09-07 15:04:20 +02:00
|
|
|
}
|
2020-11-10 14:15:31 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
await strapi.destroy();
|
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-10 14:15:31 +01:00
|
|
|
const createComponents = async components => {
|
|
|
|
const createdComponents = [];
|
2020-09-07 15:04:20 +02:00
|
|
|
|
2020-11-10 14:15:31 +01:00
|
|
|
for (const component of components) {
|
|
|
|
createdComponents.push(await createComponent(component));
|
2020-09-07 15:04:20 +02:00
|
|
|
}
|
|
|
|
|
2020-11-10 14:15:31 +01:00
|
|
|
return createdComponents;
|
|
|
|
};
|
|
|
|
|
|
|
|
const deleteComponent = async componentUID => {
|
|
|
|
const { componentsService, strapi } = await createHelpers();
|
|
|
|
|
|
|
|
const component = await componentsService.deleteComponent(componentUID);
|
|
|
|
|
|
|
|
await strapi.destroy();
|
|
|
|
|
|
|
|
return component;
|
|
|
|
};
|
|
|
|
|
|
|
|
const deleteComponents = async componentsUID => {
|
|
|
|
const deletedComponents = [];
|
2019-05-06 15:33:25 +02:00
|
|
|
|
2020-11-10 14:15:31 +01:00
|
|
|
for (const componentUID of componentsUID) {
|
|
|
|
deletedComponents.push(await deleteComponent(componentUID));
|
2019-05-06 15:33:25 +02:00
|
|
|
}
|
|
|
|
|
2020-11-10 14:15:31 +01:00
|
|
|
return deletedComponents;
|
|
|
|
};
|
|
|
|
|
|
|
|
const deleteContentType = async modelName => {
|
|
|
|
const { contentTypeService, strapi } = await createHelpers();
|
|
|
|
const uid = `application::${modelName}.${modelName}`;
|
|
|
|
|
|
|
|
const contentType = await contentTypeService.deleteContentType(uid);
|
|
|
|
|
|
|
|
await strapi.destroy();
|
|
|
|
|
|
|
|
return contentType;
|
|
|
|
};
|
|
|
|
|
|
|
|
const deleteContentTypes = async modelsName => {
|
|
|
|
const { contentTypeService, strapi } = await createHelpers();
|
|
|
|
const toUID = name => `application::${name}.${name}`;
|
|
|
|
|
|
|
|
console.log('before', Object.keys(strapi.contentTypes));
|
|
|
|
const contentTypes = await contentTypeService.deleteContentTypes(modelsName.map(toUID));
|
|
|
|
|
|
|
|
await strapi.destroy();
|
|
|
|
|
|
|
|
return contentTypes;
|
|
|
|
};
|
|
|
|
|
|
|
|
async function cleanupModels(models) {
|
|
|
|
for (const model of models) {
|
|
|
|
await cleanupModel(model);
|
2019-05-06 15:33:25 +02:00
|
|
|
}
|
2020-11-10 14:15:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
async function cleanupModel(model) {
|
|
|
|
const { strapi } = await createHelpers();
|
|
|
|
|
|
|
|
await strapi.query(model).delete();
|
|
|
|
await strapi.destroy();
|
|
|
|
}
|
|
|
|
|
|
|
|
async function createFixtures(dataMap) {
|
|
|
|
const { strapi } = await createHelpers();
|
|
|
|
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-10 14:15:31 +01:00
|
|
|
await strapi.destroy();
|
|
|
|
|
|
|
|
return resultMap;
|
|
|
|
}
|
|
|
|
|
|
|
|
async function createFixturesFor(model, entries) {
|
|
|
|
const { strapi } = await createHelpers();
|
|
|
|
const results = [];
|
|
|
|
|
|
|
|
for (const entry of entries) {
|
|
|
|
results.push(await strapi.query(model).create(entry));
|
2020-09-16 10:08:18 +02:00
|
|
|
}
|
|
|
|
|
2020-11-10 14:15:31 +01:00
|
|
|
await strapi.destroy();
|
|
|
|
|
|
|
|
return results;
|
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
2019-05-06 15:33:25 +02:00
|
|
|
};
|