@akemona-org/strapi-plugin-content-type-builder 3.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +22 -0
- package/README.md +19 -0
- package/admin/src/InjectedComponents/ContentManager/EditSettingViewButton.js +76 -0
- package/admin/src/InjectedComponents/ContentManager/EditViewLink.js +44 -0
- package/admin/src/assets/images/logo.svg +1 -0
- package/admin/src/components/AllowedTypesSelect/MenuList.js +115 -0
- package/admin/src/components/AllowedTypesSelect/Text.js +26 -0
- package/admin/src/components/AllowedTypesSelect/index.js +55 -0
- package/admin/src/components/AttributeOption/Button.js +50 -0
- package/admin/src/components/AttributeOption/Card.js +26 -0
- package/admin/src/components/AttributeOption/index.js +127 -0
- package/admin/src/components/BooleanBox/CT.js +21 -0
- package/admin/src/components/BooleanBox/Enumeration.js +10 -0
- package/admin/src/components/BooleanBox/EnumerationWrapper.js +14 -0
- package/admin/src/components/BooleanBox/Label.js +9 -0
- package/admin/src/components/BooleanBox/ST.js +21 -0
- package/admin/src/components/BooleanBox/Wrapper.js +75 -0
- package/admin/src/components/BooleanBox/icons/CTSelected.js +136 -0
- package/admin/src/components/BooleanBox/icons/CTUnselected.js +136 -0
- package/admin/src/components/BooleanBox/icons/STSelected.js +80 -0
- package/admin/src/components/BooleanBox/icons/STUnselected.js +76 -0
- package/admin/src/components/BooleanBox/index.js +103 -0
- package/admin/src/components/CheckboxWithDescription/Wrapper.js +12 -0
- package/admin/src/components/CheckboxWithDescription/index.js +40 -0
- package/admin/src/components/ComponentCard/Close.js +12 -0
- package/admin/src/components/ComponentCard/Wrapper.js +84 -0
- package/admin/src/components/ComponentCard/index.js +62 -0
- package/admin/src/components/ComponentIconPicker/Cell.js +40 -0
- package/admin/src/components/ComponentIconPicker/CellRenderer.js +36 -0
- package/admin/src/components/ComponentIconPicker/Search.js +12 -0
- package/admin/src/components/ComponentIconPicker/SearchWrapper.js +34 -0
- package/admin/src/components/ComponentIconPicker/Wrapper.js +60 -0
- package/admin/src/components/ComponentIconPicker/index.js +143 -0
- package/admin/src/components/ComponentList/index.js +78 -0
- package/admin/src/components/ComponentSelect/Category.js +20 -0
- package/admin/src/components/ComponentSelect/CategoryName.js +11 -0
- package/admin/src/components/ComponentSelect/MenuList.js +110 -0
- package/admin/src/components/ComponentSelect/MultipleMenuList.js +271 -0
- package/admin/src/components/ComponentSelect/Value.js +83 -0
- package/admin/src/components/ComponentSelect/index.js +83 -0
- package/admin/src/components/ComponentSelect/utils/HasSomeSubArray.js +5 -0
- package/admin/src/components/ComponentSelect/utils/hasSubArray.js +5 -0
- package/admin/src/components/CreatableSelect/index.js +58 -0
- package/admin/src/components/CustomCheckbox/StyledCustomCheckbox.js +25 -0
- package/admin/src/components/CustomCheckbox/index.js +71 -0
- package/admin/src/components/CustomLink/P.js +17 -0
- package/admin/src/components/CustomLink/StyledCustomLink.js +20 -0
- package/admin/src/components/CustomLink/index.js +31 -0
- package/admin/src/components/DynamicZoneList/ComponentButton.js +39 -0
- package/admin/src/components/DynamicZoneList/index.js +124 -0
- package/admin/src/components/HeaderModalNavContainer/index.js +25 -0
- package/admin/src/components/HeaderNavLink/Wrapper.js +21 -0
- package/admin/src/components/HeaderNavLink/index.js +50 -0
- package/admin/src/components/List/List.js +131 -0
- package/admin/src/components/List/index.js +295 -0
- package/admin/src/components/ListButton/index.js +9 -0
- package/admin/src/components/ListHeader/Title.js +19 -0
- package/admin/src/components/ListHeader/Wrapper.js +24 -0
- package/admin/src/components/ListHeader/index.js +37 -0
- package/admin/src/components/ListRow/Wrapper.js +76 -0
- package/admin/src/components/ListRow/index.js +334 -0
- package/admin/src/components/ModalHeader/ComponentIcon.js +21 -0
- package/admin/src/components/ModalHeader/ComponentInfos.js +22 -0
- package/admin/src/components/ModalHeader/ComponentInfosWrapper.js +9 -0
- package/admin/src/components/ModalHeader/DropdownInfos.js +50 -0
- package/admin/src/components/ModalHeader/Icon.js +26 -0
- package/admin/src/components/ModalHeader/IconWrapper.js +11 -0
- package/admin/src/components/ModalHeader/Item.js +19 -0
- package/admin/src/components/ModalHeader/Menu.js +8 -0
- package/admin/src/components/ModalHeader/Toggle.js +14 -0
- package/admin/src/components/ModalHeader/Wrapper.js +12 -0
- package/admin/src/components/ModalHeader/index.js +105 -0
- package/admin/src/components/RelationForm/Wrapper.js +9 -0
- package/admin/src/components/RelationForm/index.js +66 -0
- package/admin/src/components/RelationFormBox/Wrapper.js +34 -0
- package/admin/src/components/RelationFormBox/index.js +85 -0
- package/admin/src/components/RelationFormNaturePicker/Wrapper.js +53 -0
- package/admin/src/components/RelationFormNaturePicker/index.js +112 -0
- package/admin/src/components/RelationTargetPicker/Item.js +62 -0
- package/admin/src/components/RelationTargetPicker/Wrapper.js +89 -0
- package/admin/src/components/RelationTargetPicker/index.js +88 -0
- package/admin/src/components/SelectCheckbox/index.js +8 -0
- package/admin/src/components/SelectMenuSubUl/ToggleUl.js +23 -0
- package/admin/src/components/SelectMenuSubUl/index.js +23 -0
- package/admin/src/components/SelectMenuUl/index.js +66 -0
- package/admin/src/components/Td/index.js +37 -0
- package/admin/src/components/UpperFirst/index.js +14 -0
- package/admin/src/components/WrapperSelect/index.js +89 -0
- package/admin/src/containers/App/Wrapper.js +14 -0
- package/admin/src/containers/App/index.js +39 -0
- package/admin/src/containers/App/utils/icons.json +962 -0
- package/admin/src/containers/DataManagerProvider/constants.js +15 -0
- package/admin/src/containers/DataManagerProvider/index.js +610 -0
- package/admin/src/containers/DataManagerProvider/reducer.js +461 -0
- package/admin/src/containers/DataManagerProvider/selectors.js +25 -0
- package/admin/src/containers/DataManagerProvider/utils/cleanData.js +167 -0
- package/admin/src/containers/DataManagerProvider/utils/createDataObject.js +8 -0
- package/admin/src/containers/DataManagerProvider/utils/createModifiedDataSchema.js +91 -0
- package/admin/src/containers/DataManagerProvider/utils/retrieveComponentsFromSchema.js +55 -0
- package/admin/src/containers/DataManagerProvider/utils/retrieveComponentsThatHaveComponents.js +35 -0
- package/admin/src/containers/DataManagerProvider/utils/retrieveNestedComponents.js +34 -0
- package/admin/src/containers/DataManagerProvider/utils/retrieveSpecificInfoFromComponents.js +12 -0
- package/admin/src/containers/FormModal/CustomButton.js +12 -0
- package/admin/src/containers/FormModal/attributes/advancedForm.js +269 -0
- package/admin/src/containers/FormModal/attributes/attributeOptions.js +99 -0
- package/admin/src/containers/FormModal/attributes/baseForm.js +322 -0
- package/admin/src/containers/FormModal/attributes/commonBaseForm.js +7 -0
- package/admin/src/containers/FormModal/attributes/form.js +9 -0
- package/admin/src/containers/FormModal/attributes/index.js +4 -0
- package/admin/src/containers/FormModal/attributes/nameField.js +19 -0
- package/admin/src/containers/FormModal/attributes/types.js +309 -0
- package/admin/src/containers/FormModal/attributes/uiHelpers.js +11 -0
- package/admin/src/containers/FormModal/attributes/validation/common.js +151 -0
- package/admin/src/containers/FormModal/category/createCategorySchema.js +28 -0
- package/admin/src/containers/FormModal/category/form.js +27 -0
- package/admin/src/containers/FormModal/category/index.js +3 -0
- package/admin/src/containers/FormModal/category/regex.js +3 -0
- package/admin/src/containers/FormModal/component/componentField.js +25 -0
- package/admin/src/containers/FormModal/component/createComponentSchema.js +49 -0
- package/admin/src/containers/FormModal/component/form.js +67 -0
- package/admin/src/containers/FormModal/component/index.js +4 -0
- package/admin/src/containers/FormModal/constants.js +11 -0
- package/admin/src/containers/FormModal/contentType/createContentTypeSchema.js +44 -0
- package/admin/src/containers/FormModal/contentType/form.js +116 -0
- package/admin/src/containers/FormModal/contentType/index.js +2 -0
- package/admin/src/containers/FormModal/dynamicZone/form.js +45 -0
- package/admin/src/containers/FormModal/dynamicZone/index.js +2 -0
- package/admin/src/containers/FormModal/forms/index.js +164 -0
- package/admin/src/containers/FormModal/index.js +1552 -0
- package/admin/src/containers/FormModal/reducer.js +293 -0
- package/admin/src/containers/FormModal/selectors.js +24 -0
- package/admin/src/containers/FormModal/utils/canEditContentType.js +19 -0
- package/admin/src/containers/FormModal/utils/createHeadersArray.js +37 -0
- package/admin/src/containers/FormModal/utils/createHeadersObjectFromArray.js +27 -0
- package/admin/src/containers/FormModal/utils/createUid.js +17 -0
- package/admin/src/containers/FormModal/utils/getAttributesToDisplay.js +35 -0
- package/admin/src/containers/FormModal/utils/getModalTitleSubHeader.js +25 -0
- package/admin/src/containers/FormModal/utils/getNextSearch.js +17 -0
- package/admin/src/containers/FormModal/utils/index.js +6 -0
- package/admin/src/containers/FormModal/utils/relations.js +9 -0
- package/admin/src/containers/FormModal/utils/staticData.js +43 -0
- package/admin/src/containers/Initializer/index.js +26 -0
- package/admin/src/containers/LeftMenu/Wrapper.js +14 -0
- package/admin/src/containers/LeftMenu/index.js +220 -0
- package/admin/src/containers/ListView/Wrapper.js +16 -0
- package/admin/src/containers/ListView/index.js +322 -0
- package/admin/src/containers/NotFoundPage/index.js +20 -0
- package/admin/src/containers/RecursivePath/index.js +22 -0
- package/admin/src/contexts/DataManagerContext.js +5 -0
- package/admin/src/contexts/ListViewContext.js +5 -0
- package/admin/src/hooks/useDataManager.js +6 -0
- package/admin/src/hooks/useListView.js +6 -0
- package/admin/src/icons/Curve.js +26 -0
- package/admin/src/icons/ManyToMany.js +98 -0
- package/admin/src/icons/ManyToOne.js +64 -0
- package/admin/src/icons/ManyWay.js +36 -0
- package/admin/src/icons/OneToMany.js +65 -0
- package/admin/src/icons/OneToOne.js +47 -0
- package/admin/src/icons/OneWay.js +38 -0
- package/admin/src/index.js +83 -0
- package/admin/src/lifecycles.js +13 -0
- package/admin/src/permissions.js +9 -0
- package/admin/src/pluginId.js +5 -0
- package/admin/src/reducers.js +10 -0
- package/admin/src/translations/ar.json +48 -0
- package/admin/src/translations/cs.json +151 -0
- package/admin/src/translations/de.json +187 -0
- package/admin/src/translations/dk.json +181 -0
- package/admin/src/translations/en.json +188 -0
- package/admin/src/translations/es.json +188 -0
- package/admin/src/translations/fr.json +87 -0
- package/admin/src/translations/id.json +187 -0
- package/admin/src/translations/index.js +51 -0
- package/admin/src/translations/it.json +187 -0
- package/admin/src/translations/ja.json +50 -0
- package/admin/src/translations/ko.json +57 -0
- package/admin/src/translations/ms.json +179 -0
- package/admin/src/translations/nl.json +171 -0
- package/admin/src/translations/pl.json +152 -0
- package/admin/src/translations/pt-BR.json +51 -0
- package/admin/src/translations/pt.json +51 -0
- package/admin/src/translations/ru.json +188 -0
- package/admin/src/translations/sk.json +186 -0
- package/admin/src/translations/th.json +181 -0
- package/admin/src/translations/tr.json +51 -0
- package/admin/src/translations/uk.json +180 -0
- package/admin/src/translations/zh-Hans.json +188 -0
- package/admin/src/translations/zh.json +178 -0
- package/admin/src/utils/convertAttrObjToArray.js +7 -0
- package/admin/src/utils/formAPI.js +112 -0
- package/admin/src/utils/getAttributeDisplayedType.js +31 -0
- package/admin/src/utils/getComponents.js +31 -0
- package/admin/src/utils/getTrad.js +5 -0
- package/admin/src/utils/makeSearch.js +15 -0
- package/admin/src/utils/makeUnique.js +3 -0
- package/config/functions/bootstrap.js +14 -0
- package/config/routes.json +144 -0
- package/controllers/Builder.js +7 -0
- package/controllers/ComponentCategories.js +43 -0
- package/controllers/Components.js +152 -0
- package/controllers/Connections.js +9 -0
- package/controllers/ContentTypes.js +143 -0
- package/controllers/validation/common.js +118 -0
- package/controllers/validation/component-category.js +21 -0
- package/controllers/validation/component.js +89 -0
- package/controllers/validation/content-type.js +161 -0
- package/controllers/validation/data-transform.js +32 -0
- package/controllers/validation/model-schema.js +91 -0
- package/controllers/validation/relations.js +56 -0
- package/controllers/validation/types.js +277 -0
- package/package.json +63 -0
- package/services/Builder.js +7 -0
- package/services/ComponentCategories.js +88 -0
- package/services/Components.js +103 -0
- package/services/ContentTypes.js +263 -0
- package/services/api-handler.js +158 -0
- package/services/constants.js +59 -0
- package/services/schema-builder/component-builder.js +137 -0
- package/services/schema-builder/content-type-builder.js +256 -0
- package/services/schema-builder/index.js +196 -0
- package/services/schema-builder/schema-handler.js +297 -0
- package/utils/attributes.js +154 -0
- package/utils/helpers.js +31 -0
|
@@ -0,0 +1,297 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const path = require('path');
|
|
4
|
+
const fse = require('fs-extra');
|
|
5
|
+
const _ = require('lodash');
|
|
6
|
+
const { toUID, isConfigurable } = require('../../utils/attributes');
|
|
7
|
+
|
|
8
|
+
module.exports = function createSchemaHandler(infos) {
|
|
9
|
+
const { category, modelName, plugin, uid, dir, filename, schema } = infos;
|
|
10
|
+
|
|
11
|
+
const initialState = {
|
|
12
|
+
modelName,
|
|
13
|
+
plugin,
|
|
14
|
+
category,
|
|
15
|
+
uid,
|
|
16
|
+
dir,
|
|
17
|
+
filename,
|
|
18
|
+
schema: schema || {
|
|
19
|
+
info: {},
|
|
20
|
+
options: {},
|
|
21
|
+
attributes: {},
|
|
22
|
+
},
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
const state = _.cloneDeep(initialState);
|
|
26
|
+
|
|
27
|
+
// always keep it the same to rollback
|
|
28
|
+
Object.freeze(initialState.schema);
|
|
29
|
+
|
|
30
|
+
let modified = false;
|
|
31
|
+
let deleted = false;
|
|
32
|
+
|
|
33
|
+
return {
|
|
34
|
+
get modelName() {
|
|
35
|
+
return initialState.modelName;
|
|
36
|
+
},
|
|
37
|
+
|
|
38
|
+
get plugin() {
|
|
39
|
+
return initialState.plugin;
|
|
40
|
+
},
|
|
41
|
+
|
|
42
|
+
get category() {
|
|
43
|
+
return initialState.category;
|
|
44
|
+
},
|
|
45
|
+
|
|
46
|
+
get kind() {
|
|
47
|
+
return _.get(state.schema, 'kind', 'collectionType');
|
|
48
|
+
},
|
|
49
|
+
|
|
50
|
+
get uid() {
|
|
51
|
+
return state.uid;
|
|
52
|
+
},
|
|
53
|
+
|
|
54
|
+
get writable() {
|
|
55
|
+
return _.get(state, 'plugin') !== 'admin';
|
|
56
|
+
},
|
|
57
|
+
|
|
58
|
+
setUID(val) {
|
|
59
|
+
modified = true;
|
|
60
|
+
|
|
61
|
+
state.uid = val;
|
|
62
|
+
return this;
|
|
63
|
+
},
|
|
64
|
+
|
|
65
|
+
setDir(val) {
|
|
66
|
+
modified = true;
|
|
67
|
+
|
|
68
|
+
state.dir = val;
|
|
69
|
+
return this;
|
|
70
|
+
},
|
|
71
|
+
|
|
72
|
+
get schema() {
|
|
73
|
+
return _.cloneDeep(state.schema);
|
|
74
|
+
},
|
|
75
|
+
|
|
76
|
+
setSchema(val) {
|
|
77
|
+
modified = true;
|
|
78
|
+
|
|
79
|
+
state.schema = _.cloneDeep(val);
|
|
80
|
+
return this;
|
|
81
|
+
},
|
|
82
|
+
|
|
83
|
+
// get a particuar path inside the schema
|
|
84
|
+
get(path) {
|
|
85
|
+
return _.get(state.schema, path);
|
|
86
|
+
},
|
|
87
|
+
|
|
88
|
+
// set a particuar path inside the schema
|
|
89
|
+
set(path, val) {
|
|
90
|
+
modified = true;
|
|
91
|
+
|
|
92
|
+
const value = _.defaultTo(val, _.get(state.schema, path));
|
|
93
|
+
_.set(state.schema, path, value);
|
|
94
|
+
|
|
95
|
+
return this;
|
|
96
|
+
},
|
|
97
|
+
|
|
98
|
+
// delete a particuar path inside the schema
|
|
99
|
+
unset(path) {
|
|
100
|
+
modified = true;
|
|
101
|
+
|
|
102
|
+
_.unset(state.schema, path);
|
|
103
|
+
|
|
104
|
+
return this;
|
|
105
|
+
},
|
|
106
|
+
|
|
107
|
+
delete() {
|
|
108
|
+
deleted = true;
|
|
109
|
+
return this;
|
|
110
|
+
},
|
|
111
|
+
|
|
112
|
+
getAttribute(key) {
|
|
113
|
+
return this.get(['attributes', key]);
|
|
114
|
+
},
|
|
115
|
+
|
|
116
|
+
setAttribute(key, attribute) {
|
|
117
|
+
return this.set(['attributes', key], attribute);
|
|
118
|
+
},
|
|
119
|
+
|
|
120
|
+
deleteAttribute(key) {
|
|
121
|
+
return this.unset(['attributes', key]);
|
|
122
|
+
},
|
|
123
|
+
|
|
124
|
+
setAttributes(newAttributes) {
|
|
125
|
+
// delete old configurable attributes
|
|
126
|
+
for (let key in this.schema.attributes) {
|
|
127
|
+
if (isConfigurable(this.schema.attributes[key])) {
|
|
128
|
+
this.deleteAttribute(key);
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
// set new Attributes
|
|
133
|
+
for (let key in newAttributes) {
|
|
134
|
+
this.setAttribute(key, newAttributes[key]);
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
return this;
|
|
138
|
+
},
|
|
139
|
+
|
|
140
|
+
removeContentType(uid) {
|
|
141
|
+
const { attributes } = state.schema;
|
|
142
|
+
|
|
143
|
+
Object.keys(attributes).forEach(key => {
|
|
144
|
+
const attr = attributes[key];
|
|
145
|
+
const target = attr.model || attr.collection;
|
|
146
|
+
const plugin = attr.plugin;
|
|
147
|
+
|
|
148
|
+
const relationUID = toUID(target, plugin);
|
|
149
|
+
|
|
150
|
+
if (relationUID === uid) {
|
|
151
|
+
this.deleteAttribute(key);
|
|
152
|
+
}
|
|
153
|
+
});
|
|
154
|
+
|
|
155
|
+
return this;
|
|
156
|
+
},
|
|
157
|
+
|
|
158
|
+
// utils
|
|
159
|
+
removeComponent(uid) {
|
|
160
|
+
const { attributes } = state.schema;
|
|
161
|
+
|
|
162
|
+
Object.keys(attributes).forEach(key => {
|
|
163
|
+
const attr = attributes[key];
|
|
164
|
+
|
|
165
|
+
if (attr.type === 'component' && attr.component === uid) {
|
|
166
|
+
this.deleteAttribute(key);
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
if (
|
|
170
|
+
attr.type === 'dynamiczone' &&
|
|
171
|
+
Array.isArray(attr.components) &&
|
|
172
|
+
attr.components.includes(uid)
|
|
173
|
+
) {
|
|
174
|
+
const updatedComponentList = attributes[key].components.filter(val => val !== uid);
|
|
175
|
+
this.set(['attributes', key, 'components'], updatedComponentList);
|
|
176
|
+
}
|
|
177
|
+
});
|
|
178
|
+
|
|
179
|
+
return this;
|
|
180
|
+
},
|
|
181
|
+
|
|
182
|
+
updateComponent(uid, newUID) {
|
|
183
|
+
const { attributes } = state.schema;
|
|
184
|
+
|
|
185
|
+
Object.keys(attributes).forEach(key => {
|
|
186
|
+
const attr = attributes[key];
|
|
187
|
+
|
|
188
|
+
if (attr.type === 'component' && attr.component === uid) {
|
|
189
|
+
this.set(['attributes', key, 'component'], newUID);
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
if (
|
|
193
|
+
attr.type === 'dynamiczone' &&
|
|
194
|
+
Array.isArray(attr.components) &&
|
|
195
|
+
attr.components.includes(uid)
|
|
196
|
+
) {
|
|
197
|
+
const updatedComponentList = attr.components.map(val => (val === uid ? newUID : val));
|
|
198
|
+
|
|
199
|
+
this.set(['attributes', key, 'components'], updatedComponentList);
|
|
200
|
+
}
|
|
201
|
+
});
|
|
202
|
+
|
|
203
|
+
return this;
|
|
204
|
+
},
|
|
205
|
+
|
|
206
|
+
// save the schema to disk
|
|
207
|
+
async flush() {
|
|
208
|
+
if (!this.writable) {
|
|
209
|
+
return;
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
const initialPath = path.join(initialState.dir, initialState.filename);
|
|
213
|
+
const filePath = path.join(state.dir, state.filename);
|
|
214
|
+
|
|
215
|
+
if (deleted === true) {
|
|
216
|
+
await fse.remove(initialPath);
|
|
217
|
+
|
|
218
|
+
const list = await fse.readdir(initialState.dir);
|
|
219
|
+
if (list.length === 0) {
|
|
220
|
+
await fse.remove(initialState.dir);
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
return;
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
if (modified === true) {
|
|
227
|
+
await fse.ensureFile(filePath);
|
|
228
|
+
|
|
229
|
+
await fse.writeJSON(
|
|
230
|
+
filePath,
|
|
231
|
+
{
|
|
232
|
+
kind: state.schema.kind,
|
|
233
|
+
connection: state.schema.connection,
|
|
234
|
+
collectionName: state.schema.collectionName,
|
|
235
|
+
info: state.schema.info,
|
|
236
|
+
options: state.schema.options,
|
|
237
|
+
pluginOptions: state.schema.pluginOptions,
|
|
238
|
+
attributes: state.schema.attributes,
|
|
239
|
+
},
|
|
240
|
+
{ spaces: 2 }
|
|
241
|
+
);
|
|
242
|
+
|
|
243
|
+
// remove from oldPath
|
|
244
|
+
if (initialPath !== filePath) {
|
|
245
|
+
await fse.remove(initialPath);
|
|
246
|
+
|
|
247
|
+
const list = await fse.readdir(initialState.dir);
|
|
248
|
+
if (list.length === 0) {
|
|
249
|
+
await fse.remove(initialState.dir);
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
return;
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
return Promise.resolve();
|
|
257
|
+
},
|
|
258
|
+
|
|
259
|
+
// reset the schema to its initial value
|
|
260
|
+
async rollback() {
|
|
261
|
+
if (!this.writable) {
|
|
262
|
+
return;
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
const initialPath = path.join(initialState.dir, initialState.filename);
|
|
266
|
+
const filePath = path.join(state.dir, state.filename);
|
|
267
|
+
|
|
268
|
+
// it was a creation so it needs to be deleted
|
|
269
|
+
if (!initialState.uid) {
|
|
270
|
+
await fse.remove(filePath);
|
|
271
|
+
|
|
272
|
+
const list = await fse.readdir(state.dir);
|
|
273
|
+
if (list.length === 0) {
|
|
274
|
+
await fse.remove(state.dir);
|
|
275
|
+
}
|
|
276
|
+
return;
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
if (modified === true || deleted === true) {
|
|
280
|
+
await fse.ensureFile(initialPath);
|
|
281
|
+
await fse.writeJSON(initialPath, initialState.schema, { spaces: 2 });
|
|
282
|
+
|
|
283
|
+
// remove
|
|
284
|
+
if (initialPath !== filePath) {
|
|
285
|
+
await fse.remove(filePath);
|
|
286
|
+
|
|
287
|
+
const list = await fse.readdir(state.dir);
|
|
288
|
+
if (list.length === 0) {
|
|
289
|
+
await fse.remove(state.dir);
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
return Promise.resolve();
|
|
295
|
+
},
|
|
296
|
+
};
|
|
297
|
+
};
|
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const _ = require('lodash');
|
|
4
|
+
const utils = require('@akemona-org/strapi-utils');
|
|
5
|
+
const { isMediaAttribute } = require('@akemona-org/strapi-utils').contentTypes;
|
|
6
|
+
|
|
7
|
+
const toUID = (name, plugin) => {
|
|
8
|
+
const modelUID = Object.keys(strapi.contentTypes).find((key) => {
|
|
9
|
+
const ct = strapi.contentTypes[key];
|
|
10
|
+
if (ct.modelName === name && ct.plugin === plugin) return true;
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
return modelUID;
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
const fromUID = (uid) => {
|
|
17
|
+
const contentType = strapi.contentTypes[uid];
|
|
18
|
+
const { modelName, plugin } = contentType;
|
|
19
|
+
|
|
20
|
+
return { modelName, plugin };
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
const hasComponent = (model) => {
|
|
24
|
+
const compoKeys = Object.keys(model.attributes || {}).filter((key) => {
|
|
25
|
+
return model.attributes[key].type === 'component';
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
return compoKeys.length > 0;
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
const isConfigurable = (attribute) => _.get(attribute, 'configurable', true);
|
|
32
|
+
|
|
33
|
+
const isRelation = (attribute) =>
|
|
34
|
+
_.has(attribute, 'target') || _.has(attribute, 'model') || _.has(attribute, 'collection');
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Formats a component's attributes
|
|
38
|
+
* @param {Object} attributes - the attributes map
|
|
39
|
+
* @param {Object} context - function context
|
|
40
|
+
* @param {Object} context.component - the associated component
|
|
41
|
+
*/
|
|
42
|
+
const formatAttributes = (model) => {
|
|
43
|
+
const { getVisibleAttributes } = utils.contentTypes;
|
|
44
|
+
|
|
45
|
+
// only get attributes that can be seen in the CTB
|
|
46
|
+
return getVisibleAttributes(model).reduce((acc, key) => {
|
|
47
|
+
acc[key] = formatAttribute(key, model.attributes[key], { model });
|
|
48
|
+
return acc;
|
|
49
|
+
}, {});
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Fromats a component attribute
|
|
54
|
+
* @param {string} key - the attribute key
|
|
55
|
+
* @param {Object} attribute - the attribute
|
|
56
|
+
* @param {Object} context - function context
|
|
57
|
+
* @param {Object} context.component - the associated component
|
|
58
|
+
*/
|
|
59
|
+
const formatAttribute = (key, attribute, { model }) => {
|
|
60
|
+
if (_.has(attribute, 'type')) return attribute;
|
|
61
|
+
|
|
62
|
+
// format relations
|
|
63
|
+
const relation = (model.associations || []).find((assoc) => assoc.alias === key);
|
|
64
|
+
const { plugin, configurable } = attribute;
|
|
65
|
+
let targetEntity = attribute.model || attribute.collection;
|
|
66
|
+
|
|
67
|
+
if (isMediaAttribute(attribute)) {
|
|
68
|
+
return {
|
|
69
|
+
type: 'media',
|
|
70
|
+
multiple: attribute.collection ? true : false,
|
|
71
|
+
required: attribute.required ? true : false,
|
|
72
|
+
configurable: configurable === false ? false : undefined,
|
|
73
|
+
allowedTypes: attribute.allowedTypes,
|
|
74
|
+
pluginOptions: attribute.pluginOptions,
|
|
75
|
+
};
|
|
76
|
+
} else {
|
|
77
|
+
return {
|
|
78
|
+
nature: relation.nature,
|
|
79
|
+
target: targetEntity === '*' ? targetEntity : toUID(targetEntity, plugin),
|
|
80
|
+
plugin: plugin || undefined,
|
|
81
|
+
dominant: attribute.dominant ? true : false,
|
|
82
|
+
targetAttribute: attribute.via || undefined,
|
|
83
|
+
columnName: attribute.columnName || undefined,
|
|
84
|
+
configurable: configurable === false ? false : undefined,
|
|
85
|
+
targetColumnName: _.get(
|
|
86
|
+
strapi.getModel(targetEntity, plugin),
|
|
87
|
+
['attributes', attribute.via, 'columnName'],
|
|
88
|
+
undefined
|
|
89
|
+
),
|
|
90
|
+
private: attribute.private ? true : false,
|
|
91
|
+
unique: attribute.unique ? true : false,
|
|
92
|
+
autoPopulate: attribute.autoPopulate,
|
|
93
|
+
pluginOptions: attribute.pluginOptions,
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
};
|
|
97
|
+
|
|
98
|
+
// TODO: move to schema builder
|
|
99
|
+
const replaceTemporaryUIDs = (uidMap) => (schema) => {
|
|
100
|
+
return {
|
|
101
|
+
...schema,
|
|
102
|
+
attributes: Object.keys(schema.attributes).reduce((acc, key) => {
|
|
103
|
+
const attr = schema.attributes[key];
|
|
104
|
+
if (attr.type === 'component') {
|
|
105
|
+
if (_.has(uidMap, attr.component)) {
|
|
106
|
+
acc[key] = {
|
|
107
|
+
...attr,
|
|
108
|
+
component: uidMap[attr.component],
|
|
109
|
+
};
|
|
110
|
+
|
|
111
|
+
return acc;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
if (!_.has(strapi.components, attr.component)) {
|
|
115
|
+
throw new Error('component.notFound');
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
if (
|
|
120
|
+
attr.type === 'dynamiczone' &&
|
|
121
|
+
_.intersection(attr.components, Object.keys(uidMap)).length > 0
|
|
122
|
+
) {
|
|
123
|
+
acc[key] = {
|
|
124
|
+
...attr,
|
|
125
|
+
components: attr.components.map((value) => {
|
|
126
|
+
if (_.has(uidMap, value)) return uidMap[value];
|
|
127
|
+
|
|
128
|
+
if (!_.has(strapi.components, value)) {
|
|
129
|
+
throw new Error('component.notFound');
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
return value;
|
|
133
|
+
}),
|
|
134
|
+
};
|
|
135
|
+
|
|
136
|
+
return acc;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
acc[key] = attr;
|
|
140
|
+
return acc;
|
|
141
|
+
}, {}),
|
|
142
|
+
};
|
|
143
|
+
};
|
|
144
|
+
|
|
145
|
+
module.exports = {
|
|
146
|
+
fromUID,
|
|
147
|
+
toUID,
|
|
148
|
+
hasComponent,
|
|
149
|
+
isRelation,
|
|
150
|
+
isConfigurable,
|
|
151
|
+
replaceTemporaryUIDs,
|
|
152
|
+
formatAttributes,
|
|
153
|
+
formatAttribute,
|
|
154
|
+
};
|
package/utils/helpers.js
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const escapeNewlines = (content = '', placeholder = '\n') => {
|
|
4
|
+
return content.replace(/[\r\n]+/g, placeholder);
|
|
5
|
+
};
|
|
6
|
+
|
|
7
|
+
const deepTrimObject = attribute => {
|
|
8
|
+
if (Array.isArray(attribute)) {
|
|
9
|
+
return attribute.map(deepTrimObject);
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
if (typeof attribute === 'object') {
|
|
13
|
+
return Object.entries(attribute).reduce((acc, [key, value]) => {
|
|
14
|
+
const trimmedObject = deepTrimObject(value);
|
|
15
|
+
|
|
16
|
+
return { ...acc, [key]: trimmedObject };
|
|
17
|
+
}, {});
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
return typeof attribute === 'string' ? attribute.trim() : attribute;
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Converts a name to a slug
|
|
25
|
+
* @param {string} name a name to convert
|
|
26
|
+
*/
|
|
27
|
+
|
|
28
|
+
module.exports = {
|
|
29
|
+
escapeNewlines,
|
|
30
|
+
deepTrimObject,
|
|
31
|
+
};
|