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