@contentstack/cli-migration 0.1.1-beta.3 → 1.0.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 +21 -0
- package/README.md +22 -27
- package/oclif.manifest.json +1 -1
- package/package.json +16 -11
- package/src/actions/action-list.js +11 -11
- package/src/actions/index.js +33 -34
- package/src/commands/cm/{migration.js → stacks/migration.js} +102 -74
- package/src/config/api-config.js +8 -9
- package/src/config/default-options.js +2 -2
- package/src/config/index.js +2 -2
- package/src/config/master-locale.js +2 -2
- package/src/modules/base.js +33 -33
- package/src/modules/content-types.js +76 -76
- package/src/modules/fields.js +73 -73
- package/src/modules/index.js +2 -2
- package/src/modules/locale.js +13 -13
- package/src/modules/migration.js +45 -46
- package/src/modules/parser.js +65 -52
- package/src/services/content-types.js +160 -163
- package/src/services/index.js +2 -2
- package/src/services/locales.js +33 -35
- package/src/utils/auto-retry.js +14 -12
- package/src/utils/callsite.js +14 -14
- package/src/utils/constants.js +108 -115
- package/src/utils/contentstack-sdk.js +42 -43
- package/src/utils/error-handler.js +8 -8
- package/src/utils/error-helper.js +41 -40
- package/src/utils/fs-helper.js +12 -12
- package/src/utils/get-batches.js +4 -4
- package/src/utils/get-config.js +6 -6
- package/src/utils/group-by.js +17 -17
- package/src/utils/index.js +2 -2
- package/src/utils/logger.js +42 -52
- package/src/utils/object-helper.js +7 -7
- package/src/utils/safe-promise.js +2 -2
- package/src/utils/schema-helper.js +12 -12
- package/src/utils/success-handler.js +5 -5
- package/src/validators/api-error.js +10 -8
- package/src/validators/base-validator.js +14 -14
- package/src/validators/create-content-type-validator.js +21 -25
- package/src/validators/edit-content-type-validator.js +21 -24
- package/src/validators/field-validator.js +10 -8
- package/src/validators/index.js +2 -2
- package/src/validators/migration-error.js +9 -7
- package/src/validators/schema-validator.js +11 -9
- package/src/validators/type-error.js +11 -10
|
@@ -1,323 +1,320 @@
|
|
|
1
1
|
/* eslint-disable unicorn/explicit-length-check */
|
|
2
2
|
/* eslint-disable no-unused-expressions */
|
|
3
|
-
'use strict'
|
|
3
|
+
'use strict';
|
|
4
4
|
|
|
5
|
-
const Base = require('../modules/base')
|
|
5
|
+
const Base = require('../modules/base');
|
|
6
6
|
// Utils
|
|
7
|
-
const {map: _map, safePromise, successHandler, errorHandler, constants} = require('../utils')
|
|
7
|
+
const { map: _map, safePromise, successHandler, errorHandler, constants } = require('../utils');
|
|
8
8
|
// Map methods
|
|
9
|
-
const {get, getMapInstance, getDataWithAction} = _map
|
|
10
|
-
const mapInstance = getMapInstance()
|
|
11
|
-
const {ContentType, MANAGEMENT_SDK, actions: _actions} = constants
|
|
9
|
+
const { get, getMapInstance, getDataWithAction } = _map;
|
|
10
|
+
const mapInstance = getMapInstance();
|
|
11
|
+
const { ContentType, MANAGEMENT_SDK, actions: _actions } = constants;
|
|
12
12
|
|
|
13
13
|
class ContentTypeService {
|
|
14
14
|
constructor() {
|
|
15
15
|
// Stores actions required for moveField function
|
|
16
|
-
this.moveFieldActions = []
|
|
17
|
-
this.base = new Base()
|
|
18
|
-
this.stackSDKInstance = get(MANAGEMENT_SDK, mapInstance)
|
|
16
|
+
this.moveFieldActions = [];
|
|
17
|
+
this.base = new Base();
|
|
18
|
+
this.stackSDKInstance = get(MANAGEMENT_SDK, mapInstance);
|
|
19
19
|
}
|
|
20
20
|
|
|
21
21
|
async fetchContentType(callsite, id) {
|
|
22
|
-
const method = 'GET'
|
|
22
|
+
const method = 'GET';
|
|
23
23
|
|
|
24
|
-
const [err, result] = await safePromise(this.stackSDKInstance.contentType(id).fetch())
|
|
24
|
+
const [err, result] = await safePromise(this.stackSDKInstance.contentType(id).fetch());
|
|
25
25
|
if (err) {
|
|
26
|
-
errorHandler(id, ContentType, method, err)
|
|
27
|
-
this.base.dispatch(callsite, id, err, 'apiError')
|
|
28
|
-
throw err
|
|
26
|
+
errorHandler(id, ContentType, method, err);
|
|
27
|
+
this.base.dispatch(callsite, id, err, 'apiError');
|
|
28
|
+
throw err;
|
|
29
29
|
}
|
|
30
|
-
successHandler(id, ContentType, method)
|
|
30
|
+
successHandler(id, ContentType, method);
|
|
31
31
|
|
|
32
|
-
return result || {}
|
|
32
|
+
return result || {};
|
|
33
33
|
}
|
|
34
34
|
|
|
35
35
|
async postContentTypes(callsite, id, action) {
|
|
36
|
-
const data = getDataWithAction(id, mapInstance, action)
|
|
37
|
-
const [err, result] = await safePromise(this.stackSDKInstance.contentType().create(data))
|
|
36
|
+
const data = getDataWithAction(id, mapInstance, action);
|
|
37
|
+
const [err, result] = await safePromise(this.stackSDKInstance.contentType().create(data));
|
|
38
38
|
if (err) {
|
|
39
|
-
errorHandler(id, ContentType, 'POST', err)
|
|
40
|
-
this.base.dispatch(callsite, id, err, 'apiError')
|
|
41
|
-
throw err
|
|
39
|
+
errorHandler(id, ContentType, 'POST', err);
|
|
40
|
+
this.base.dispatch(callsite, id, err, 'apiError');
|
|
41
|
+
throw err;
|
|
42
42
|
}
|
|
43
43
|
|
|
44
|
-
successHandler(id, ContentType, 'POST')
|
|
45
|
-
return result.content_type || {}
|
|
44
|
+
successHandler(id, ContentType, 'POST');
|
|
45
|
+
return result.content_type || {};
|
|
46
46
|
}
|
|
47
47
|
|
|
48
48
|
async editContentType(callsite, data) {
|
|
49
|
-
const d = getDataWithAction(data.uid, mapInstance, _actions.EDIT_CT)
|
|
50
|
-
data = {...data, ...d.content_type}
|
|
51
|
-
const method = 'PUT'
|
|
52
|
-
const [err, result] = await safePromise(data.update())
|
|
49
|
+
const d = getDataWithAction(data.uid, mapInstance, _actions.EDIT_CT);
|
|
50
|
+
data = { ...data, ...d.content_type };
|
|
51
|
+
const method = 'PUT';
|
|
52
|
+
const [err, result] = await safePromise(data.update());
|
|
53
53
|
if (err) {
|
|
54
|
-
errorHandler(data.uid, ContentType, method, err)
|
|
55
|
-
this.base.dispatch(callsite, data.uid, err, 'apiError')
|
|
56
|
-
throw err
|
|
54
|
+
errorHandler(data.uid, ContentType, method, err);
|
|
55
|
+
this.base.dispatch(callsite, data.uid, err, 'apiError');
|
|
56
|
+
throw err;
|
|
57
57
|
}
|
|
58
58
|
|
|
59
|
-
successHandler(data.uid, ContentType, method)
|
|
60
|
-
return result.content_type || {}
|
|
59
|
+
successHandler(data.uid, ContentType, method);
|
|
60
|
+
return result.content_type || {};
|
|
61
61
|
}
|
|
62
62
|
|
|
63
63
|
async deleteContentType(callsite) {
|
|
64
|
-
const {id} = this
|
|
65
|
-
const method = 'DELETE'
|
|
66
|
-
const [err, result] = await safePromise(this.stackSDKInstance.contentType(id).delete())
|
|
64
|
+
const { id } = this;
|
|
65
|
+
const method = 'DELETE';
|
|
66
|
+
const [err, result] = await safePromise(this.stackSDKInstance.contentType(id).delete());
|
|
67
67
|
|
|
68
68
|
if (err) {
|
|
69
|
-
errorHandler(id, ContentType, method, err)
|
|
70
|
-
this.base.dispatch(callsite, id, err, 'apiError')
|
|
71
|
-
throw err
|
|
69
|
+
errorHandler(id, ContentType, method, err);
|
|
70
|
+
this.base.dispatch(callsite, id, err, 'apiError');
|
|
71
|
+
throw err;
|
|
72
72
|
}
|
|
73
|
-
successHandler(id, ContentType, method)
|
|
74
|
-
return result.content_type || {}
|
|
73
|
+
successHandler(id, ContentType, method);
|
|
74
|
+
return result.content_type || {};
|
|
75
75
|
}
|
|
76
76
|
|
|
77
77
|
applyActionsOnFields(callsite, data, cb) {
|
|
78
|
-
const {schema} = data
|
|
79
|
-
const {moveFieldActions, mergeEditSchema} = this
|
|
80
|
-
let i = 0
|
|
81
|
-
let finalSchema
|
|
78
|
+
const { schema } = data;
|
|
79
|
+
const { moveFieldActions, mergeEditSchema } = this;
|
|
80
|
+
let i = 0;
|
|
81
|
+
let finalSchema;
|
|
82
82
|
try {
|
|
83
|
-
finalSchema = mergeEditSchema.call(this, schema)
|
|
83
|
+
finalSchema = mergeEditSchema.call(this, schema);
|
|
84
84
|
} catch (error) {
|
|
85
|
-
this.base.dispatch(callsite, null, error, 'field')
|
|
85
|
+
this.base.dispatch(callsite, null, error, 'field');
|
|
86
86
|
// Call the callback with error
|
|
87
|
-
if (typeof cb === 'function') return cb(error)
|
|
87
|
+
if (typeof cb === 'function') return cb(error);
|
|
88
88
|
}
|
|
89
|
-
data.schema = finalSchema
|
|
89
|
+
data.schema = finalSchema;
|
|
90
90
|
// Handle for no move field action required
|
|
91
|
-
if (!moveFieldActions.length) return cb(null, data)
|
|
91
|
+
if (!moveFieldActions.length) return cb(null, data);
|
|
92
92
|
// eslint-disable-next-line
|
|
93
93
|
while (true) {
|
|
94
94
|
/** VALIDATIONS */
|
|
95
|
-
const validResult = this.getValidated(finalSchema, moveFieldActions[i])
|
|
95
|
+
const validResult = this.getValidated(finalSchema, moveFieldActions[i]);
|
|
96
96
|
if (!validResult.isValid) {
|
|
97
|
-
const error = {message: `${validResult.missingField} does not exist in schema.`}
|
|
98
|
-
this.base.dispatch(callsite, null, error, 'field')
|
|
97
|
+
const error = { message: `${validResult.missingField} does not exist in schema.` };
|
|
98
|
+
this.base.dispatch(callsite, null, error, 'field');
|
|
99
99
|
// Call the callback with error
|
|
100
|
-
if (typeof cb === 'function') return cb(error)
|
|
100
|
+
if (typeof cb === 'function') return cb(error);
|
|
101
101
|
}
|
|
102
102
|
|
|
103
|
-
finalSchema = this[moveFieldActions[i].action](finalSchema, moveFieldActions[i])
|
|
104
|
-
i
|
|
105
|
-
if (!moveFieldActions[i]) break
|
|
103
|
+
finalSchema = this[moveFieldActions[i].action](finalSchema, moveFieldActions[i]);
|
|
104
|
+
i++;
|
|
105
|
+
if (!moveFieldActions[i]) break;
|
|
106
106
|
}
|
|
107
|
-
data.schema = finalSchema
|
|
108
|
-
if (cb) return cb(null, data)
|
|
109
|
-
return data
|
|
107
|
+
data.schema = finalSchema;
|
|
108
|
+
if (cb) return cb(null, data);
|
|
109
|
+
return data;
|
|
110
110
|
}
|
|
111
111
|
|
|
112
112
|
getActions(action) {
|
|
113
|
-
this.moveFieldActions.push(action)
|
|
113
|
+
this.moveFieldActions.push(action);
|
|
114
114
|
}
|
|
115
115
|
|
|
116
116
|
// Sets id and action for this instance
|
|
117
117
|
setIdAndAction(id, action) {
|
|
118
|
-
this.id = id
|
|
119
|
-
this.action = action
|
|
118
|
+
this.id = id;
|
|
119
|
+
this.action = action;
|
|
120
120
|
}
|
|
121
121
|
|
|
122
122
|
// Merges the user specified with new fields with existing schema
|
|
123
123
|
mergeEditSchema(schema = []) {
|
|
124
|
-
const
|
|
124
|
+
const _mapInstance = getMapInstance();
|
|
125
125
|
|
|
126
|
-
const {id, action} = this
|
|
126
|
+
const { id, action } = this;
|
|
127
127
|
|
|
128
|
-
const contentType = get(id,
|
|
128
|
+
const contentType = get(id, _mapInstance);
|
|
129
129
|
|
|
130
|
-
let contentTypeSchema = contentType[action].content_type.schema
|
|
131
|
-
contentTypeSchema = contentTypeSchema || []
|
|
130
|
+
let contentTypeSchema = contentType[action].content_type.schema;
|
|
131
|
+
contentTypeSchema = contentTypeSchema || [];
|
|
132
132
|
|
|
133
|
-
const indicesToRemoveFromNewSchema = []
|
|
134
|
-
const indicesToRemoveFromOldSchema = []
|
|
133
|
+
const indicesToRemoveFromNewSchema = [];
|
|
134
|
+
const indicesToRemoveFromOldSchema = [];
|
|
135
135
|
|
|
136
|
-
let isEditFieldValid = false
|
|
137
|
-
let isEditFieldPresent = false
|
|
138
|
-
let isDeleteFieldValid = false
|
|
139
|
-
let isDeleteFieldPresent = false
|
|
140
|
-
let fieldToRaiseExceptionAgainst
|
|
136
|
+
let isEditFieldValid = false;
|
|
137
|
+
let isEditFieldPresent = false;
|
|
138
|
+
let isDeleteFieldValid = false;
|
|
139
|
+
let isDeleteFieldPresent = false;
|
|
140
|
+
let fieldToRaiseExceptionAgainst;
|
|
141
141
|
|
|
142
|
-
if (contentTypeSchema.length > 0
|
|
142
|
+
if (contentTypeSchema.length > 0 && schema.length > 0) {
|
|
143
143
|
// If found a updated field replace the new field with the existing field
|
|
144
144
|
contentTypeSchema.forEach((newSchema, i) => {
|
|
145
145
|
schema.every((oldSchema, j) => {
|
|
146
146
|
/** VALIDATIONS */
|
|
147
147
|
if (newSchema.isEdit) {
|
|
148
|
-
isEditFieldPresent = true
|
|
149
|
-
fieldToRaiseExceptionAgainst = newSchema.uid
|
|
150
|
-
newSchema.uid === oldSchema.uid && (isEditFieldValid = true)
|
|
148
|
+
isEditFieldPresent = true;
|
|
149
|
+
fieldToRaiseExceptionAgainst = newSchema.uid;
|
|
150
|
+
newSchema.uid === oldSchema.uid && (isEditFieldValid = true);
|
|
151
151
|
}
|
|
152
152
|
|
|
153
153
|
if (newSchema.isDelete) {
|
|
154
|
-
isDeleteFieldPresent = true
|
|
155
|
-
fieldToRaiseExceptionAgainst = newSchema.uid
|
|
154
|
+
isDeleteFieldPresent = true;
|
|
155
|
+
fieldToRaiseExceptionAgainst = newSchema.uid;
|
|
156
156
|
|
|
157
|
-
newSchema.uid === oldSchema.uid && (isDeleteFieldValid = true)
|
|
157
|
+
newSchema.uid === oldSchema.uid && (isDeleteFieldValid = true);
|
|
158
158
|
}
|
|
159
159
|
/** VALIDATIONS ENDS */
|
|
160
160
|
|
|
161
161
|
if (newSchema.uid === oldSchema.uid) {
|
|
162
|
-
let tempObj = newSchema
|
|
163
|
-
indicesToRemoveFromNewSchema.push(i)
|
|
162
|
+
let tempObj = newSchema;
|
|
163
|
+
indicesToRemoveFromNewSchema.push(i);
|
|
164
164
|
// Handle delete action here
|
|
165
165
|
if (newSchema.isDelete) {
|
|
166
|
-
indicesToRemoveFromOldSchema.push(j)
|
|
166
|
+
indicesToRemoveFromOldSchema.push(j);
|
|
167
167
|
} else {
|
|
168
|
-
schema.splice(j, 1, tempObj) // Replace the new schema with old schema
|
|
168
|
+
schema.splice(j, 1, tempObj); // Replace the new schema with old schema
|
|
169
169
|
}
|
|
170
170
|
// break
|
|
171
|
-
return false
|
|
171
|
+
return false;
|
|
172
172
|
}
|
|
173
173
|
// continue
|
|
174
|
-
return true
|
|
175
|
-
})
|
|
176
|
-
})
|
|
174
|
+
return true;
|
|
175
|
+
});
|
|
176
|
+
});
|
|
177
177
|
}
|
|
178
178
|
|
|
179
179
|
// Raise exception if any of the following conditions are true
|
|
180
180
|
if ((isEditFieldPresent && !isEditFieldValid) || (isDeleteFieldPresent && !isDeleteFieldValid)) {
|
|
181
|
-
throw {message: `${fieldToRaiseExceptionAgainst} does not exist in the schema. Please check again`}
|
|
181
|
+
throw { message: `${fieldToRaiseExceptionAgainst} does not exist in the schema. Please check again` };
|
|
182
182
|
}
|
|
183
183
|
|
|
184
|
-
contentTypeSchema = contentTypeSchema.filter((_, i) =>
|
|
185
|
-
!indicesToRemoveFromNewSchema.includes(i))
|
|
184
|
+
contentTypeSchema = contentTypeSchema.filter((_, i) => !indicesToRemoveFromNewSchema.includes(i));
|
|
186
185
|
|
|
187
|
-
schema = schema.filter((_, i) =>
|
|
188
|
-
!indicesToRemoveFromOldSchema.includes(i))
|
|
186
|
+
schema = schema.filter((_, i) => !indicesToRemoveFromOldSchema.includes(i));
|
|
189
187
|
|
|
190
|
-
schema = schema.concat(contentTypeSchema)
|
|
191
|
-
contentType[action].content_type.schema = schema
|
|
192
|
-
return schema
|
|
188
|
+
schema = schema.concat(contentTypeSchema);
|
|
189
|
+
contentType[action].content_type.schema = schema;
|
|
190
|
+
return schema;
|
|
193
191
|
}
|
|
194
192
|
|
|
195
193
|
toTheTop(schema, actionObj) {
|
|
196
|
-
const {fieldToMove} = actionObj
|
|
197
|
-
let i = 0
|
|
194
|
+
const { fieldToMove } = actionObj;
|
|
195
|
+
let i = 0;
|
|
198
196
|
// eslint-disable-next-line
|
|
199
197
|
while (true) {
|
|
200
198
|
if (schema[i].uid === fieldToMove) {
|
|
201
|
-
let tempObj = schema[i]
|
|
202
|
-
schema.splice(i, 1)
|
|
203
|
-
schema.unshift(tempObj)
|
|
204
|
-
break
|
|
199
|
+
let tempObj = schema[i];
|
|
200
|
+
schema.splice(i, 1);
|
|
201
|
+
schema.unshift(tempObj);
|
|
202
|
+
break;
|
|
205
203
|
}
|
|
206
|
-
i
|
|
207
|
-
if (!schema[i]) break // Error handling required
|
|
204
|
+
i++;
|
|
205
|
+
if (!schema[i]) break; // Error handling required
|
|
208
206
|
}
|
|
209
|
-
return schema
|
|
207
|
+
return schema;
|
|
210
208
|
}
|
|
211
209
|
|
|
212
210
|
toTheBottom(schema, actionObj) {
|
|
213
|
-
const {fieldToMove} = actionObj
|
|
211
|
+
const { fieldToMove } = actionObj;
|
|
214
212
|
|
|
215
|
-
let i = 0
|
|
213
|
+
let i = 0;
|
|
216
214
|
// eslint-disable-next-line
|
|
217
215
|
while (true) {
|
|
218
216
|
if (schema[i].uid === fieldToMove) {
|
|
219
|
-
let tempObj = schema[i]
|
|
220
|
-
schema.splice(i, 1)
|
|
221
|
-
schema.push(tempObj)
|
|
222
|
-
break
|
|
217
|
+
let tempObj = schema[i];
|
|
218
|
+
schema.splice(i, 1);
|
|
219
|
+
schema.push(tempObj);
|
|
220
|
+
break;
|
|
223
221
|
}
|
|
224
|
-
i
|
|
225
|
-
if (!schema[i]) break
|
|
222
|
+
i++;
|
|
223
|
+
if (!schema[i]) break;
|
|
226
224
|
}
|
|
227
|
-
return schema
|
|
225
|
+
return schema;
|
|
228
226
|
}
|
|
229
227
|
|
|
230
228
|
afterField(schema, actionObj) {
|
|
231
|
-
const {fieldToMove, against} = actionObj
|
|
232
|
-
let i = 0
|
|
233
|
-
let indexToMove = 0
|
|
234
|
-
let tempObj
|
|
235
|
-
let found = 0
|
|
229
|
+
const { fieldToMove, against } = actionObj;
|
|
230
|
+
let i = 0;
|
|
231
|
+
let indexToMove = 0;
|
|
232
|
+
let tempObj;
|
|
233
|
+
let found = 0;
|
|
236
234
|
// eslint-disable-next-line
|
|
237
235
|
while (true) {
|
|
238
236
|
if (schema[i].uid === against) {
|
|
239
|
-
indexToMove = i
|
|
240
|
-
found
|
|
237
|
+
indexToMove = i;
|
|
238
|
+
found++;
|
|
241
239
|
}
|
|
242
240
|
if (schema[i].uid === fieldToMove) {
|
|
243
|
-
tempObj = schema[i]
|
|
244
|
-
schema.splice(i, 1)
|
|
245
|
-
found
|
|
241
|
+
tempObj = schema[i];
|
|
242
|
+
schema.splice(i, 1);
|
|
243
|
+
found++;
|
|
246
244
|
}
|
|
247
|
-
i
|
|
248
|
-
if (found === 2) break
|
|
249
|
-
if (!schema[i]) break
|
|
245
|
+
i++;
|
|
246
|
+
if (found === 2) break;
|
|
247
|
+
if (!schema[i]) break;
|
|
250
248
|
}
|
|
251
249
|
// TODO: Handle error
|
|
252
|
-
found === 2 && schema.splice(indexToMove + 1, null, tempObj)
|
|
253
|
-
return schema
|
|
250
|
+
found === 2 && schema.splice(indexToMove + 1, null, tempObj);
|
|
251
|
+
return schema;
|
|
254
252
|
}
|
|
255
253
|
|
|
256
254
|
beforeField(schema, actionObj) {
|
|
257
|
-
const {fieldToMove, against} = actionObj
|
|
255
|
+
const { fieldToMove, against } = actionObj;
|
|
258
256
|
|
|
259
|
-
let i = 0
|
|
260
|
-
let indexToMove = 0
|
|
261
|
-
let tempObj = 0
|
|
262
|
-
let found = 0
|
|
257
|
+
let i = 0;
|
|
258
|
+
let indexToMove = 0;
|
|
259
|
+
let tempObj = 0;
|
|
260
|
+
let found = 0;
|
|
263
261
|
// eslint-disable-next-line
|
|
264
262
|
while (true) {
|
|
265
263
|
if (schema[i].uid === against) {
|
|
266
|
-
indexToMove = i
|
|
267
|
-
found
|
|
264
|
+
indexToMove = i;
|
|
265
|
+
found++;
|
|
268
266
|
}
|
|
269
267
|
if (schema[i].uid === fieldToMove) {
|
|
270
|
-
tempObj = schema[i]
|
|
271
|
-
schema.splice(i, 1)
|
|
272
|
-
found
|
|
268
|
+
tempObj = schema[i];
|
|
269
|
+
schema.splice(i, 1);
|
|
270
|
+
found++;
|
|
273
271
|
}
|
|
274
|
-
i
|
|
275
|
-
if (found === 2) break
|
|
276
|
-
if (!schema[i]) break
|
|
272
|
+
i++;
|
|
273
|
+
if (found === 2) break;
|
|
274
|
+
if (!schema[i]) break;
|
|
277
275
|
}
|
|
278
|
-
found === 2 && schema.splice(indexToMove, null, tempObj)
|
|
279
|
-
return schema
|
|
276
|
+
found === 2 && schema.splice(indexToMove, null, tempObj);
|
|
277
|
+
return schema;
|
|
280
278
|
}
|
|
281
279
|
|
|
282
280
|
getValidated(schema, actionObj) {
|
|
283
|
-
let isValid = true
|
|
284
|
-
let found = 0
|
|
285
|
-
let missingField = ''
|
|
286
|
-
let i = 0
|
|
281
|
+
let isValid = true;
|
|
282
|
+
let found = 0;
|
|
283
|
+
let missingField = '';
|
|
284
|
+
let i = 0;
|
|
287
285
|
|
|
288
|
-
const {fieldToMove, against} = actionObj
|
|
289
|
-
const uids = []
|
|
286
|
+
const { fieldToMove, against } = actionObj;
|
|
287
|
+
const uids = [];
|
|
290
288
|
// eslint-disable-next-line
|
|
291
289
|
while (true) {
|
|
292
|
-
|
|
293
|
-
uids.push(schema[i].uid)
|
|
290
|
+
uids.push(schema[i].uid);
|
|
294
291
|
|
|
295
292
|
if (schema[i].uid === fieldToMove) {
|
|
296
|
-
found
|
|
293
|
+
found++;
|
|
297
294
|
}
|
|
298
295
|
if (against === schema[i].uid) {
|
|
299
|
-
found
|
|
296
|
+
found++;
|
|
300
297
|
}
|
|
301
|
-
i
|
|
302
|
-
if (!schema[i]) break
|
|
298
|
+
i++;
|
|
299
|
+
if (!schema[i]) break;
|
|
303
300
|
}
|
|
304
301
|
// TODO: Need a better way to handle this
|
|
305
|
-
missingField = uids.includes(fieldToMove) ? null : fieldToMove
|
|
302
|
+
missingField = uids.includes(fieldToMove) ? null : fieldToMove;
|
|
306
303
|
|
|
307
304
|
// against && (missingField = !uids.includes(against) ? against : null);
|
|
308
305
|
if (!missingField && against) {
|
|
309
|
-
missingField = uids.includes(against) ? null : against
|
|
306
|
+
missingField = uids.includes(against) ? null : against;
|
|
310
307
|
}
|
|
311
308
|
|
|
312
309
|
// Handling both the scenarios
|
|
313
310
|
if (found === 0) {
|
|
314
|
-
isValid = false
|
|
311
|
+
isValid = false;
|
|
315
312
|
} else if (against && found === 1) {
|
|
316
|
-
isValid = false
|
|
313
|
+
isValid = false;
|
|
317
314
|
}
|
|
318
315
|
|
|
319
|
-
return {isValid, missingField}
|
|
316
|
+
return { isValid, missingField };
|
|
320
317
|
}
|
|
321
318
|
}
|
|
322
319
|
|
|
323
|
-
module.exports = ContentTypeService
|
|
320
|
+
module.exports = ContentTypeService;
|
package/src/services/index.js
CHANGED
package/src/services/locales.js
CHANGED
|
@@ -1,74 +1,72 @@
|
|
|
1
|
-
'use strict'
|
|
1
|
+
'use strict';
|
|
2
2
|
|
|
3
3
|
// Utils
|
|
4
|
-
const {safePromise, constants, map: _map} = require('../utils')
|
|
5
|
-
const {MANAGEMENT_SDK} = constants
|
|
6
|
-
const {get, getMapInstance} = _map
|
|
7
|
-
const mapInstance = getMapInstance()
|
|
8
|
-
this.stackSDKInstance = get(MANAGEMENT_SDK, mapInstance)
|
|
4
|
+
const { safePromise, constants, map: _map } = require('../utils');
|
|
5
|
+
const { MANAGEMENT_SDK } = constants;
|
|
6
|
+
const { get, getMapInstance } = _map;
|
|
7
|
+
const mapInstance = getMapInstance();
|
|
8
|
+
this.stackSDKInstance = get(MANAGEMENT_SDK, mapInstance);
|
|
9
9
|
|
|
10
10
|
class LocaleService {
|
|
11
11
|
constructor() {
|
|
12
|
-
this.stackSDKInstance = get(MANAGEMENT_SDK, mapInstance)
|
|
12
|
+
this.stackSDKInstance = get(MANAGEMENT_SDK, mapInstance);
|
|
13
13
|
}
|
|
14
14
|
|
|
15
15
|
async getLocale() {
|
|
16
|
-
const [err, result] = await safePromise(
|
|
17
|
-
|
|
18
|
-
)
|
|
19
|
-
|
|
20
|
-
let orderedResult = this.getOrderedResult(result)
|
|
21
|
-
return orderedResult
|
|
16
|
+
const [err, result] = await safePromise(this.stackSDKInstance.locale().query().find());
|
|
17
|
+
if (err) throw err;
|
|
18
|
+
let orderedResult = this.getOrderedResult(result);
|
|
19
|
+
return orderedResult;
|
|
22
20
|
}
|
|
23
21
|
|
|
24
22
|
getOrderedResult(result) {
|
|
25
23
|
if (result && result.items) {
|
|
26
|
-
const locales = result.items
|
|
24
|
+
const locales = result.items;
|
|
27
25
|
|
|
28
|
-
let i = 0
|
|
29
|
-
let noEventTookPlace = 0 // counter which tracks if the list is sorted by fallback language
|
|
30
|
-
let len = locales.length
|
|
26
|
+
let i = 0;
|
|
27
|
+
let noEventTookPlace = 0; // counter which tracks if the list is sorted by fallback language
|
|
28
|
+
let len = locales.length;
|
|
31
29
|
|
|
32
30
|
// Circular loop (Time complexity => Order of n^2, complexity of splice op is ignored)
|
|
33
31
|
do {
|
|
34
|
-
i = i % len + 1
|
|
35
|
-
noEventTookPlace
|
|
32
|
+
i = (i % len) + 1;
|
|
33
|
+
noEventTookPlace++;
|
|
36
34
|
|
|
37
|
-
let correctedI = i - 1
|
|
35
|
+
let correctedI = i - 1;
|
|
38
36
|
|
|
39
|
-
let a = locales[correctedI]
|
|
37
|
+
let a = locales[correctedI];
|
|
40
38
|
|
|
41
39
|
if (a.fallback_locale) {
|
|
42
|
-
let fallbackLangIndex = 0
|
|
43
|
-
let currentLangIndex = 0
|
|
40
|
+
let fallbackLangIndex = 0;
|
|
41
|
+
let currentLangIndex = 0;
|
|
44
42
|
|
|
45
43
|
for (let x = 0; x < len; x++) {
|
|
46
44
|
if (locales[x].code === a.code) {
|
|
47
|
-
currentLangIndex = x
|
|
45
|
+
currentLangIndex = x;
|
|
48
46
|
}
|
|
49
47
|
if (locales[x].code === a.fallback_locale) {
|
|
50
|
-
fallbackLangIndex = x
|
|
48
|
+
fallbackLangIndex = x;
|
|
51
49
|
}
|
|
52
50
|
}
|
|
53
51
|
|
|
54
52
|
// if index of fallback langauge is smaller no operation is required, it might be sorted
|
|
55
53
|
if (currentLangIndex > fallbackLangIndex) {
|
|
56
|
-
continue
|
|
54
|
+
continue;
|
|
57
55
|
}
|
|
58
|
-
let temp = a
|
|
56
|
+
let temp = a;
|
|
59
57
|
// remove the object
|
|
60
|
-
locales.splice(correctedI, 1)
|
|
58
|
+
locales.splice(correctedI, 1);
|
|
61
59
|
// add the object at fallbackLangIndex cus size of locales is decremented
|
|
62
|
-
locales.splice(fallbackLangIndex, 0, temp)
|
|
63
|
-
i
|
|
64
|
-
noEventTookPlace
|
|
60
|
+
locales.splice(fallbackLangIndex, 0, temp);
|
|
61
|
+
i--;
|
|
62
|
+
noEventTookPlace--;
|
|
65
63
|
}
|
|
66
|
-
} while (noEventTookPlace < len)
|
|
64
|
+
} while (noEventTookPlace < len);
|
|
67
65
|
|
|
68
|
-
return locales
|
|
66
|
+
return locales;
|
|
69
67
|
}
|
|
70
|
-
throw {message: 'Something went wrong.'}
|
|
68
|
+
throw { message: 'Something went wrong.' };
|
|
71
69
|
}
|
|
72
70
|
}
|
|
73
71
|
|
|
74
|
-
module.exports = LocaleService
|
|
72
|
+
module.exports = LocaleService;
|
package/src/utils/auto-retry.js
CHANGED
|
@@ -1,10 +1,12 @@
|
|
|
1
|
-
'use strict'
|
|
1
|
+
'use strict';
|
|
2
2
|
|
|
3
|
-
const {MAX_RETRY} = require('./constants')
|
|
3
|
+
const { MAX_RETRY } = require('./constants');
|
|
4
4
|
|
|
5
5
|
const __safePromise = (promise, data) => {
|
|
6
|
-
return promise(data)
|
|
7
|
-
|
|
6
|
+
return promise(data)
|
|
7
|
+
.then((res) => [null, res])
|
|
8
|
+
.catch((err) => [err]);
|
|
9
|
+
};
|
|
8
10
|
|
|
9
11
|
async function autoRetry(promise, retryCount = 0) {
|
|
10
12
|
/**
|
|
@@ -12,19 +14,19 @@ async function autoRetry(promise, retryCount = 0) {
|
|
|
12
14
|
* whereas for content types it fetches request params from global map object,
|
|
13
15
|
* thus the handling
|
|
14
16
|
*/
|
|
15
|
-
let data
|
|
16
|
-
this && (data = this.data)
|
|
17
|
+
let data;
|
|
18
|
+
this && (data = this.data);
|
|
17
19
|
|
|
18
|
-
const [error, result] = await __safePromise(promise, data)
|
|
20
|
+
const [error, result] = await __safePromise(promise, data);
|
|
19
21
|
|
|
20
22
|
if (error) {
|
|
21
|
-
retryCount
|
|
23
|
+
retryCount++;
|
|
22
24
|
if (retryCount === MAX_RETRY) {
|
|
23
|
-
throw error
|
|
25
|
+
throw error;
|
|
24
26
|
}
|
|
25
|
-
return await autoRetry(promise, retryCount)
|
|
27
|
+
return await autoRetry(promise, retryCount);
|
|
26
28
|
}
|
|
27
|
-
return result
|
|
29
|
+
return result;
|
|
28
30
|
}
|
|
29
31
|
|
|
30
|
-
module.exports = autoRetry
|
|
32
|
+
module.exports = autoRetry;
|