@contentstack/cli-cm-import 1.2.3 → 1.3.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.
@@ -3,6 +3,7 @@
3
3
  * Copyright (c) 2019 Contentstack LLC
4
4
  * MIT Licensed
5
5
  */
6
+
6
7
  const Promise = require('bluebird');
7
8
  const fs = require('fs');
8
9
  const path = require('path');
@@ -14,119 +15,111 @@ const util = require('../util');
14
15
  const helper = require('../util/fs');
15
16
  const { addlogs } = require('../util/log');
16
17
  const suppress = require('../util/supress-mandatory-fields');
17
- const stack = require('../util/contentstack-management-sdk');
18
18
  const extension_suppress = require('../util/extensionsUidReplace');
19
19
  const lookupReplaceAssets = require('../util/lookupReplaceAssets');
20
20
  const lookupReplaceEntries = require('../util/lookupReplaceEntries');
21
- const { getInstalledExtensions } = require('../util/marketplace-app-helper');
22
-
23
- let client;
24
- let config = util.getConfig();
25
-
26
- let reqConcurrency = config.concurrency;
27
- let eConfig = config.modules.entries;
28
- let ePath = path.resolve(config.data, eConfig.dirName);
29
- let ctPath = path.resolve(config.data, config.modules.content_types.dirName);
30
- let lPath = path.resolve(config.data, config.modules.locales.dirName, config.modules.locales.fileName);
31
-
32
- let mappedAssetUidPath;
33
- let mappedAssetUrlPath;
34
- let entryMapperPath;
35
- let environmentPath;
36
- let entryUidMapperPath;
37
- let uniqueUidMapperPath;
38
- let modifiedSchemaPath;
39
- let createdEntriesWOUidPath;
40
- let failedWOPath;
41
- let masterLanguage;
42
-
43
- let skipFiles = ['__master.json', '__priority.json', 'schema.json'];
44
- let entryBatchLimit = config.rateLimit || 10;
45
- const importConcurrency = eConfig.importConcurrency || config.importConcurrency;
46
- const writeConcurrency = eConfig.writeConcurrency || config.writeConcurrency;
47
-
48
- function EntriesImport() {
49
- let self = this;
50
- mappedAssetUidPath = path.resolve(config.data, 'mapper', 'assets', 'uid-mapping.json');
51
- mappedAssetUrlPath = path.resolve(config.data, 'mapper', 'assets', 'url-mapping.json');
52
-
53
- entryMapperPath = path.resolve(config.data, 'mapper', 'entries');
54
- environmentPath = path.resolve(config.data, 'environments', 'environments.json');
55
- mkdirp.sync(entryMapperPath);
56
-
57
- entryUidMapperPath = path.join(entryMapperPath, 'uid-mapping.json');
58
- uniqueUidMapperPath = path.join(entryMapperPath, 'unique-mapping.json');
59
- modifiedSchemaPath = path.join(entryMapperPath, 'modified-schemas.json');
60
-
61
- createdEntriesWOUidPath = path.join(entryMapperPath, 'created-entries-wo-uid.json');
62
- failedWOPath = path.join(entryMapperPath, 'failedWO.json');
63
-
64
- // Object of Schemas, referred to by their content type uid
65
- this.ctSchemas = {};
66
- // Array of content type uids, that have reference fields
67
- this.refSchemas = [];
68
- // map of content types uids and their json-rte fields
69
- this.ctJsonRte = [];
70
- // map of content types uids and their json-rte fields
71
- this.ctJsonRteWithEntryRefs = [];
72
- // Entry refs that are held back to resolve after all entries have been created
73
- this.jsonRteEntryRefs = {};
74
- // Collection of entries, that were not created, as they already exist on Stack
75
- this.createdEntriesWOUid = [];
76
- // Collection of entry uids, mapped to the language they exist in
77
- this.uniqueUids = {};
78
- // Map of old entry uid to new
79
- this.mappedUids = {};
80
- // Entries that were created successfully
81
- this.success = [];
82
- // Entries that failed to get created OR updated
83
- this.fails = [];
84
- // List of installed extensions to replace uid
85
- this.installedExtensions = [];
86
-
87
- let files = fs.readdirSync(ctPath);
88
- this.environment = helper.readFileSync(environmentPath);
89
- for (let index in files) {
90
- if (index) {
91
- try {
92
- if (skipFiles.indexOf(files[index]) === -1) {
93
- if (files[index] != 'field_rules_uid.json') {
94
- let schema = require(path.resolve(path.join(ctPath, files[index])));
95
- self.ctSchemas[schema.uid] = schema;
21
+ const config = require('../../config/default');
22
+
23
+ module.exports = class ImportEntries {
24
+ mappedAssetUidPath;
25
+ mappedAssetUrlPath;
26
+ entryMapperPath;
27
+ environmentPath;
28
+ entryUidMapperPath;
29
+ uniqueUidMapperPath;
30
+ modifiedSchemaPath;
31
+ createdEntriesWOUidPath;
32
+ failedWOPath;
33
+ masterLanguage;
34
+ reqConcurrency;
35
+ eConfig;
36
+ ePath;
37
+ ctPath;
38
+ lPath;
39
+ importConcurrency;
40
+ skipFiles = ['__master.json', '__priority.json', 'schema.json'];
41
+
42
+ constructor(importConfig, stackAPIClient) {
43
+ this.config = _.merge(config, importConfig);
44
+ this.stackAPIClient = stackAPIClient;
45
+ this.mappedAssetUidPath = path.resolve(this.config.data, 'mapper', 'assets', 'uid-mapping.json');
46
+ this.mappedAssetUrlPath = path.resolve(this.config.data, 'mapper', 'assets', 'url-mapping.json');
47
+
48
+ this.entryMapperPath = path.resolve(this.config.data, 'mapper', 'entries');
49
+ this.environmentPath = path.resolve(this.config.data, 'environments', 'environments.json');
50
+ mkdirp.sync(this.entryMapperPath);
51
+
52
+ this.entryUidMapperPath = path.join(this.entryMapperPath, 'uid-mapping.json');
53
+ this.uniqueUidMapperPath = path.join(this.entryMapperPath, 'unique-mapping.json');
54
+ this.modifiedSchemaPath = path.join(this.entryMapperPath, 'modified-schemas.json');
55
+
56
+ this.createdEntriesWOUidPath = path.join(this.entryMapperPath, 'created-entries-wo-uid.json');
57
+ this.failedWOPath = path.join(this.entryMapperPath, 'failedWO.json');
58
+
59
+ this.reqConcurrency = this.config.concurrency;
60
+ this.eConfig = this.config.modules.entries;
61
+ this.ePath = path.resolve(this.config.data, this.eConfig.dirName);
62
+ this.ctPath = path.resolve(this.config.data, this.config.modules.content_types.dirName);
63
+ this.lPath = path.resolve(
64
+ this.config.data,
65
+ this.config.modules.locales.dirName,
66
+ this.config.modules.locales.fileName,
67
+ );
68
+
69
+ this.importConcurrency = this.eConfig.importConcurrency || this.config.importConcurrency;
70
+
71
+ // Object of Schemas, referred to by their content type uid
72
+ this.ctSchemas = {};
73
+ // Array of content type uids, that have reference fields
74
+ this.refSchemas = [];
75
+ // map of content types uids and their json-rte fields
76
+ this.ctJsonRte = [];
77
+ // map of content types uids and their json-rte fields
78
+ this.ctJsonRteWithEntryRefs = [];
79
+ // Entry refs that are held back to resolve after all entries have been created
80
+ this.jsonRteEntryRefs = {};
81
+ // Collection of entries, that were not created, as they already exist on Stack
82
+ this.createdEntriesWOUid = [];
83
+ // Collection of entry uids, mapped to the language they exist in
84
+ this.uniqueUids = {};
85
+ // Map of old entry uid to new
86
+ this.mappedUids = {};
87
+ // Entries that were created successfully
88
+ this.success = [];
89
+ // Entries that failed to get created OR updated
90
+ this.fails = [];
91
+ // List of installed extensions to replace uid
92
+ this.installedExtensions = [];
93
+
94
+ let files = fs.readdirSync(this.ctPath);
95
+ this.environment = helper.readFileSync(this.environmentPath);
96
+ for (let index in files) {
97
+ if (index) {
98
+ try {
99
+ if (this.skipFiles.indexOf(files[index]) === -1) {
100
+ if (files[index] != 'field_rules_uid.json') {
101
+ let schema = require(path.resolve(path.join(this.ctPath, files[index])));
102
+ this.ctSchemas[schema.uid] = schema;
103
+ }
96
104
  }
105
+ } catch (error) {
106
+ addlogs(this.config, `Failed to read the content types to import entries ${util.formatError(error)}`);
107
+ process.exit(0);
97
108
  }
98
- } catch (error) {
99
- addlogs(config, `Failed to read the content types to import entries ${util.formatError(error)}`);
100
- process.exit(0);
101
109
  }
102
110
  }
103
111
  }
104
- }
105
-
106
- EntriesImport.prototype = {
107
- /**
108
- * Start point for entry import
109
- * @return promise
110
- */
111
- start: async function (credentialConfig) {
112
- let self = this;
113
- config = credentialConfig;
114
- client = stack.Client(config);
115
- masterLanguage = config.master_locale;
116
- addlogs(config, 'Migrating entries', 'success');
117
- let languages = helper.readFileSync(lPath);
118
- const appMapperFolderPath = path.join(config.data, 'mapper', 'marketplace_apps');
119
-
120
- if (fs.existsSync(path.join(appMapperFolderPath, 'marketplace-apps.json'))) {
121
- self.installedExtensions = helper.readFileSync(path.join(appMapperFolderPath, 'marketplace-apps.json')) || {};
122
- }
123
112
 
124
- if (_.isEmpty(self.installedExtensions)) {
125
- self.installedExtensions = await getInstalledExtensions(config);
126
- }
127
-
128
- return new Promise(function (resolve, reject) {
129
- let langs = [masterLanguage.code];
113
+ async start() {
114
+ let self = this;
115
+ this.masterLanguage = this.config.master_locale;
116
+ addlogs(this.config, 'Migrating entries', 'success');
117
+ let languages = helper.readFileSync(this.lPath);
118
+ const appMapperPath = path.join(this.config.data, 'mapper', 'marketplace_apps', 'uid-mapping.json');
119
+ this.installedExtensions = ((await helper.readFileSync(appMapperPath)) || { extension_uid: {} }).extension_uid;
120
+
121
+ return new Promise((resolve, reject) => {
122
+ let langs = [self.masterLanguage.code];
130
123
  for (let i in languages) {
131
124
  if (i) {
132
125
  langs.push(languages[i].code);
@@ -139,118 +132,114 @@ EntriesImport.prototype = {
139
132
  // Also remove field visibility rules
140
133
  return self
141
134
  .supressFields()
142
- .then(async function () {
143
- addlogs(config, 'Completed suppressing content type reference fields', 'success');
135
+ .then(async () => {
136
+ addlogs(self.config, 'Completed suppressing content type reference fields', 'success');
144
137
 
145
- let mappedAssetUids = helper.readFileSync(mappedAssetUidPath) || {};
146
- let mappedAssetUrls = helper.readFileSync(mappedAssetUrlPath) || {};
138
+ let mappedAssetUids = helper.readFileSync(self.mappedAssetUidPath) || {};
139
+ let mappedAssetUrls = helper.readFileSync(self.mappedAssetUrlPath) || {};
147
140
 
148
141
  // Step 2: Iterate over available languages to create entries in each.
149
142
  let counter = 0;
150
143
  return Promise.map(
151
144
  langs,
152
- async function () {
145
+ async () => {
153
146
  let lang = langs[counter];
154
147
  if (
155
- (config.hasOwnProperty('onlylocales') && config.onlylocales.indexOf(lang) !== -1) ||
156
- !config.hasOwnProperty('onlylocales')
148
+ (self.config.hasOwnProperty('onlylocales') && self.config.onlylocales.indexOf(lang) !== -1) ||
149
+ !self.config.hasOwnProperty('onlylocales')
157
150
  ) {
158
- addlogs(config, `Starting to create entries ${lang} locale`, 'info');
151
+ addlogs(self.config, `Starting to create entries ${lang} locale`, 'info');
159
152
  await self.createEntries(lang, mappedAssetUids, mappedAssetUrls);
160
- addlogs(config, 'Entries created successfully', 'info');
153
+ addlogs(self.config, 'Entries created successfully', 'info');
161
154
  try {
162
155
  await self.getCreatedEntriesWOUid();
163
156
  } catch (error) {
164
157
  addlogs(
165
- config,
158
+ self.config,
166
159
  `Failed get the existing entries to update the mapper ${util.formatError(error)}, 'error`,
167
160
  );
168
161
  }
169
- addlogs(config, 'Starting to update entries with references', 'info');
162
+ addlogs(self.config, 'Starting to update entries with references', 'info');
170
163
  await self.repostEntries(lang);
171
- addlogs(config, "Successfully imported '" + lang + "' entries!", 'success');
164
+ addlogs(self.config, "Successfully imported '" + lang + "' entries!", 'success');
172
165
  counter++;
173
166
  } else {
174
- addlogs(config, lang + ' has not been configured for import, thus skipping it', 'success');
167
+ addlogs(self.config, lang + ' has not been configured for import, thus skipping it', 'success');
175
168
  counter++;
176
169
  }
177
170
  },
178
171
  {
179
172
  concurrency: 1,
180
173
  },
181
- ).then(async function () {
174
+ ).then(async () => {
182
175
  // Step 3: Revert all the changes done in content type in step 1
183
- addlogs(config, 'Restoring content type changes', 'info');
176
+ addlogs(self.config, 'Restoring content type changes', 'info');
184
177
  await self.unSuppressFields();
185
- addlogs(config, 'Removing entries from master language which got created by default', 'info');
178
+ addlogs(self.config, 'Removing entries from master language which got created by default', 'info');
186
179
  await self.removeBuggedEntries();
187
- addlogs(config, 'Updating the field rules of content type', 'info');
188
- let ct_field_visibility_uid = helper.readFileSync(path.join(ctPath + '/field_rules_uid.json'));
189
- let ct_files = fs.readdirSync(ctPath);
180
+ addlogs(self.config, 'Updating the field rules of content type', 'info');
181
+ let ct_field_visibility_uid = helper.readFileSync(path.join(self.ctPath + '/field_rules_uid.json'));
182
+ let ct_files = fs.readdirSync(self.ctPath);
190
183
  if (ct_field_visibility_uid && ct_field_visibility_uid != 'undefined') {
191
184
  for (const element of ct_field_visibility_uid) {
192
185
  if (ct_files.indexOf(element + '.json') > -1) {
193
- let schema = require(path.resolve(ctPath, element));
186
+ let schema = require(path.resolve(self.ctPath, element));
194
187
  try {
195
188
  await self.field_rules_update(schema);
196
189
  } catch (error) {
197
190
  addlogs(
198
- config,
191
+ self.config,
199
192
  `Failed to update the field rules for content type ${schema.uid} ${util.formatError(error)}`,
200
193
  );
201
194
  }
202
195
  }
203
196
  }
204
197
  }
205
- addlogs(config, chalk.green('Entries have been imported successfully!'), 'success');
206
- if (config.entriesPublish) {
207
- addlogs(config, chalk.green('Publishing entries'), 'success');
198
+ addlogs(self.config, chalk.green('Entries have been imported successfully!'), 'success');
199
+ if (self.config.entriesPublish) {
200
+ addlogs(self.config, chalk.green('Publishing entries'), 'success');
208
201
  return self
209
202
  .publish(langs)
210
- .then(function () {
211
- addlogs(config, chalk.green('All the entries have been published successfully'), 'success');
203
+ .then(() => {
204
+ addlogs(self.config, chalk.green('All the entries have been published successfully'), 'success');
212
205
  return resolve();
213
206
  })
214
207
  .catch((error) => {
215
- addlogs(config, `Error in publishing entries ${util.formatError(error)}`, 'error');
208
+ addlogs(self.config, `Error in publishing entries ${util.formatError(error)}`, 'error');
216
209
  });
217
210
  }
218
211
  return resolve();
219
212
  });
220
213
  })
221
- .catch(function (error) {
222
- addlogs.log(config, util.formatError(error), 'error');
214
+ .catch((error) => {
215
+ addlogs(self.config, util.formatError(error), 'error');
223
216
  reject('Failed import entries');
224
217
  });
225
218
  });
226
- },
219
+ }
227
220
 
228
- createEntries: function (lang, mappedAssetUids, mappedAssetUrls) {
221
+ createEntries(lang, mappedAssetUids, mappedAssetUrls) {
229
222
  let self = this;
230
- return new Promise(async function (resolve, reject) {
223
+ return new Promise(async (resolve, reject) => {
231
224
  let contentTypeUids = Object.keys(self.ctSchemas);
232
- if (fs.existsSync(entryUidMapperPath)) {
233
- self.mappedUids = await helper.readLargeFile(entryUidMapperPath);
225
+ if (fs.existsSync(this.entryUidMapperPath)) {
226
+ self.mappedUids = await helper.readLargeFile(this.entryUidMapperPath);
234
227
  }
235
228
  self.mappedUids = self.mappedUids || {};
236
229
  return Promise.map(
237
230
  contentTypeUids,
238
- async function (ctUid) {
239
- let eLangFolderPath = path.join(entryMapperPath, lang);
240
- let eLogFolderPath = path.join(entryMapperPath, lang, ctUid);
231
+ async (ctUid) => {
232
+ let eLangFolderPath = path.join(this.entryMapperPath, lang);
233
+ let eLogFolderPath = path.join(this.entryMapperPath, lang, ctUid);
241
234
  mkdirp.sync(eLogFolderPath);
242
235
  // entry file path
243
- let eFilePath = path.resolve(ePath, ctUid, lang + '.json');
236
+ let eFilePath = path.resolve(this.ePath, ctUid, lang + '.json');
244
237
 
245
238
  // log created/updated entries
246
239
  let successEntryLogPath = path.join(eLogFolderPath, 'success.json');
247
240
  let failedEntryLogPath = path.join(eLogFolderPath, 'fails.json');
248
241
  let createdEntriesPath = path.join(eLogFolderPath, 'created-entries.json');
249
242
  let createdEntries = {};
250
- let stackForEntries = client.stack({
251
- api_key: config.target_stack,
252
- management_token: config.management_token,
253
- });
254
243
 
255
244
  if (fs.existsSync(createdEntriesPath)) {
256
245
  createdEntries = await helper.readLargeFile(createdEntriesPath);
@@ -260,12 +249,12 @@ EntriesImport.prototype = {
260
249
  let entries = await helper.readLargeFile(eFilePath);
261
250
  if (!_.isPlainObject(entries) || _.isEmpty(entries)) {
262
251
  addlogs(
263
- config,
252
+ this.config,
264
253
  chalk.white("No entries were found for Content type:'" + ctUid + "' in '" + lang + "' language!"),
265
254
  'success',
266
255
  );
267
256
  } else {
268
- addlogs(config, `Creating entries for content type ${ctUid} in language ${lang} ...`, 'success');
257
+ addlogs(this.config, `Creating entries for content type ${ctUid} in language ${lang} ...`, 'success');
269
258
  for (let eUid in entries) {
270
259
  if (eUid) {
271
260
  try {
@@ -292,15 +281,15 @@ EntriesImport.prototype = {
292
281
  self.installedExtensions,
293
282
  );
294
283
  } catch (error) {
295
- addlogs(config, 'Failed to update entry while creating entry id ' + eUid);
296
- addlogs(config, util.formatError(error), 'error');
284
+ addlogs(this.config, 'Failed to update entry while creating entry id ' + eUid);
285
+ addlogs(this.config, util.formatError(error), 'error');
297
286
  }
298
287
  }
299
288
  }
300
289
  let eUids = Object.keys(entries);
301
290
  let batches = [];
302
291
 
303
- let entryBatchLimit = eConfig.batchLimit || 10;
292
+ let entryBatchLimit = this.eConfig.batchLimit || 10;
304
293
  let batchSize = Math.round(entryBatchLimit / 3);
305
294
 
306
295
  // Run entry creation in batches of ~16~ entries
@@ -309,14 +298,14 @@ EntriesImport.prototype = {
309
298
  }
310
299
  return Promise.map(
311
300
  batches,
312
- async function (batch) {
301
+ async (batch) => {
313
302
  return Promise.map(
314
303
  batch,
315
- async function (eUid, batchIndex) {
304
+ async (eUid, batchIndex) => {
316
305
  // if entry is already created
317
306
  if (createdEntries.hasOwnProperty(eUid)) {
318
307
  addlogs(
319
- config,
308
+ this.config,
320
309
  'Skipping ' +
321
310
  JSON.stringify({
322
311
  content_type: ctUid,
@@ -329,7 +318,7 @@ EntriesImport.prototype = {
329
318
  );
330
319
  self.success[ctUid] = createdEntries[eUid];
331
320
  // if its a non-master language, i.e. the entry isn't present in the master language
332
- if (lang !== masterLanguage.code) {
321
+ if (lang !== this.masterLanguage.code) {
333
322
  self.uniqueUids[eUid] = self.uniqueUids[eUid] || {};
334
323
  if (self.uniqueUids[eUid].locales) {
335
324
  self.uniqueUids[eUid].locales.push(lang);
@@ -349,7 +338,7 @@ EntriesImport.prototype = {
349
338
  },
350
339
  };
351
340
  if (self.mappedUids.hasOwnProperty(eUid)) {
352
- let entryToUpdate = stackForEntries.contentType(ctUid).entry(self.mappedUids[eUid]);
341
+ let entryToUpdate = self.stackAPIClient.contentType(ctUid).entry(self.mappedUids[eUid]);
353
342
  Object.assign(entryToUpdate, _.cloneDeep(entries[eUid]));
354
343
  return entryToUpdate
355
344
  .update({ locale: entryToUpdate.locale })
@@ -360,7 +349,7 @@ EntriesImport.prototype = {
360
349
  self.mappedUids[eUid] = entryResponse.uid;
361
350
  createdEntries = entryResponse;
362
351
  // if its a non-master language, i.e. the entry isn't present in the master language
363
- if (lang !== masterLanguage.code) {
352
+ if (lang !== this.masterLanguage.code) {
364
353
  self.uniqueUids[eUid] = self.uniqueUids[eUid] || {};
365
354
  if (self.uniqueUids[eUid].locales) {
366
355
  self.uniqueUids[eUid].locales.push(lang);
@@ -371,8 +360,12 @@ EntriesImport.prototype = {
371
360
  }
372
361
  }
373
362
  })
374
- .catch(function (error) {
375
- addlogs(config, `Failed to update an entry ${eUid} ${util.formatError(error)}`, 'error');
363
+ .catch((error) => {
364
+ addlogs(
365
+ this.config,
366
+ `Failed to update an entry ${eUid} ${util.formatError(error)}`,
367
+ 'error',
368
+ );
376
369
  self.fails.push({
377
370
  content_type: ctUid,
378
371
  locale: lang,
@@ -383,8 +376,7 @@ EntriesImport.prototype = {
383
376
  });
384
377
  }
385
378
  delete requestObject.json.entry.publish_details;
386
- return client
387
- .stack({ api_key: config.target_stack, management_token: config.management_token })
379
+ return self.stackAPIClient
388
380
  .contentType(ctUid)
389
381
  .entry()
390
382
  .create(requestObject.json, { locale: lang })
@@ -395,7 +387,7 @@ EntriesImport.prototype = {
395
387
  self.mappedUids[eUid] = entryResponse.uid;
396
388
  createdEntries = entryResponse;
397
389
  // if its a non-master language, i.e. the entry isn't present in the master language
398
- if (lang !== masterLanguage.code) {
390
+ if (lang !== this.masterLanguage.code) {
399
391
  self.uniqueUids[eUid] = self.uniqueUids[eUid] || {};
400
392
  if (self.uniqueUids[eUid].locales) {
401
393
  self.uniqueUids[eUid].locales.push(lang);
@@ -406,16 +398,20 @@ EntriesImport.prototype = {
406
398
  }
407
399
  }
408
400
  })
409
- .catch(function (error) {
401
+ .catch((error) => {
410
402
  if (error.hasOwnProperty('error_code') && error.error_code === 119) {
411
403
  if (error.errors.title) {
412
404
  addlogs(
413
- config,
405
+ this.config,
414
406
  'Entry ' + eUid + ' already exist, skip to avoid creating a duplicate entry',
415
407
  'error',
416
408
  );
417
409
  } else {
418
- addlogs(config, `Failed to create an entry ${eUid} ${util.formatError(error)}`, 'error');
410
+ addlogs(
411
+ this.config,
412
+ `Failed to create an entry ${eUid} ${util.formatError(error)}`,
413
+ 'error',
414
+ );
419
415
  }
420
416
  self.createdEntriesWOUid.push({
421
417
  content_type: ctUid,
@@ -423,12 +419,12 @@ EntriesImport.prototype = {
423
419
  entry: entries[eUid],
424
420
  error: error,
425
421
  });
426
- helper.writeFileSync(createdEntriesWOUidPath, self.createdEntriesWOUid);
422
+ helper.writeFileSync(this.createdEntriesWOUidPath, self.createdEntriesWOUid);
427
423
  return;
428
424
  }
429
425
  // TODO: if status code: 422, check the reason
430
426
  // 429 for rate limit
431
- addlogs(config, `Failed to create an entry ${eUid} ${util.formatError(error)}`, 'error');
427
+ addlogs(this.config, `Failed to create an entry ${eUid} ${util.formatError(error)}`, 'error');
432
428
  self.fails.push({
433
429
  content_type: ctUid,
434
430
  locale: lang,
@@ -439,13 +435,13 @@ EntriesImport.prototype = {
439
435
  // create/update 5 entries at a time
440
436
  },
441
437
  {
442
- concurrency: importConcurrency,
438
+ concurrency: this.importConcurrency,
443
439
  },
444
- ).then(function () {
440
+ ).then(() => {
445
441
  helper.writeFileSync(successEntryLogPath, self.success[ctUid]);
446
442
  helper.writeFileSync(failedEntryLogPath, self.fails[ctUid]);
447
- helper.writeFileSync(entryUidMapperPath, self.mappedUids);
448
- helper.writeFileSync(uniqueUidMapperPath, self.uniqueUids);
443
+ helper.writeFileSync(this.entryUidMapperPath, self.mappedUids);
444
+ helper.writeFileSync(this.uniqueUidMapperPath, self.uniqueUids);
449
445
  helper.writeFileSync(createdEntriesPath, createdEntries);
450
446
  });
451
447
  // process one batch at a time
@@ -453,10 +449,10 @@ EntriesImport.prototype = {
453
449
  {
454
450
  concurrency: 1,
455
451
  },
456
- ).then(function () {
452
+ ).then(() => {
457
453
  if (self.success && self.success[ctUid] && self.success[ctUid].length > 0)
458
454
  addlogs(
459
- config,
455
+ this.config,
460
456
  self.success[ctUid].length +
461
457
  ' entries created successfully in ' +
462
458
  ctUid +
@@ -467,7 +463,7 @@ EntriesImport.prototype = {
467
463
  );
468
464
  if (self.fails && self.fails[ctUid] && self.fails[ctUid].length > 0)
469
465
  addlogs(
470
- config,
466
+ this.config,
471
467
  self.fails[ctUid].length +
472
468
  ' entries failed to create in ' +
473
469
  ctUid +
@@ -482,7 +478,7 @@ EntriesImport.prototype = {
482
478
  }
483
479
  } else {
484
480
  addlogs(
485
- config,
481
+ this.config,
486
482
  chalk.white(
487
483
  'Unable to find entry file path for ' +
488
484
  ctUid +
@@ -498,64 +494,64 @@ EntriesImport.prototype = {
498
494
  concurrency: 1,
499
495
  },
500
496
  )
501
- .then(function () {
502
- addlogs(config, chalk.green("Entries created successfully in '" + lang + "' language"), 'success');
497
+ .then(() => {
498
+ addlogs(this.config, chalk.green("Entries created successfully in '" + lang + "' language"), 'success');
503
499
  return resolve();
504
500
  })
505
- .catch(function (error) {
506
- addlogs(config, chalk.red("Failed to create entries in '" + lang + "' language"), 'error');
501
+ .catch((error) => {
502
+ addlogs(this.config, chalk.red("Failed to create entries in '" + lang + "' language"), 'error');
507
503
  return reject(error);
508
504
  });
509
505
  });
510
- },
511
- getCreatedEntriesWOUid: function () {
506
+ }
507
+ getCreatedEntriesWOUid() {
512
508
  let self = this;
513
- return new Promise(function (resolve) {
514
- self.createdEntriesWOUid = helper.readFileSync(createdEntriesWOUidPath);
509
+ return new Promise((resolve) => {
510
+ self.createdEntriesWOUid = helper.readFileSync(this.createdEntriesWOUidPath);
515
511
  self.failedWO = [];
516
512
  if (_.isArray(self.createdEntriesWOUid) && self.createdEntriesWOUid.length > 0) {
517
513
  return Promise.map(
518
514
  self.createdEntriesWOUid,
519
- function (entry) {
515
+ (entry) => {
520
516
  return self.fetchEntry(entry);
521
517
  },
522
518
  {
523
- concurrency: importConcurrency,
519
+ concurrency: this.importConcurrency,
524
520
  },
525
- ).then(function () {
526
- helper.writeFileSync(failedWOPath, self.failedWO);
527
- addlogs(config, 'Mapped entries without mapped uid successfully!', 'success');
521
+ ).then(() => {
522
+ helper.writeFileSync(this.failedWOPath, self.failedWO);
523
+ addlogs(this.config, 'Mapped entries without mapped uid successfully!', 'success');
528
524
  return resolve();
529
525
  });
530
526
  }
531
- addlogs(config, 'No entries without mapped uid found!', 'success');
527
+ addlogs(this.config, 'No entries without mapped uid found!', 'success');
532
528
  return resolve();
533
529
  });
534
- },
535
- repostEntries: function (lang) {
530
+ }
531
+ repostEntries(lang) {
536
532
  let self = this;
537
- return new Promise(async function (resolve, reject) {
538
- let _mapped_ = await helper.readLargeFile(path.join(entryMapperPath, 'uid-mapping.json'));
533
+ return new Promise(async (resolve, reject) => {
534
+ let _mapped_ = await helper.readLargeFile(path.join(this.entryMapperPath, 'uid-mapping.json'));
539
535
  if (_.isPlainObject(_mapped_)) {
540
536
  self.mappedUids = _.merge(_mapped_, self.mappedUids);
541
537
  }
542
538
  return Promise.map(
543
539
  self.refSchemas,
544
- async function (ctUid) {
545
- let eFolderPath = path.join(entryMapperPath, lang, ctUid);
540
+ async (ctUid) => {
541
+ let eFolderPath = path.join(this.entryMapperPath, lang, ctUid);
546
542
  let eSuccessFilePath = path.join(eFolderPath, 'success.json');
547
- let eFilePath = path.resolve(ePath, ctUid, lang + '.json');
543
+ let eFilePath = path.resolve(this.ePath, ctUid, lang + '.json');
548
544
  let sourceStackEntries = await helper.readLargeFile(eFilePath);
549
545
 
550
546
  if (!fs.existsSync(eSuccessFilePath)) {
551
- addlogs(config, 'Success file was not found at: ' + eSuccessFilePath, 'success');
547
+ addlogs(this.config, 'Success file was not found at: ' + eSuccessFilePath, 'success');
552
548
  return;
553
549
  }
554
550
 
555
551
  let entries = await helper.readLargeFile(eSuccessFilePath, { type: 'array' }); // TBD LARGE
556
552
  entries = entries || [];
557
553
  if (entries.length === 0) {
558
- addlogs(config, "No entries were created to be updated in '" + lang + "' language!", 'success');
554
+ addlogs(this.config, "No entries were created to be updated in '" + lang + "' language!", 'success');
559
555
  return;
560
556
  }
561
557
 
@@ -570,11 +566,11 @@ EntriesImport.prototype = {
570
566
 
571
567
  // map reference uids @mapper/language/mapped-uids.json
572
568
  // map failed reference uids @mapper/language/unmapped-uids.json
573
- let refUidMapperPath = path.join(entryMapperPath, lang);
569
+ let refUidMapperPath = path.join(this.entryMapperPath, lang);
574
570
 
575
- addlogs(config, 'staring to update the entry for reposting');
571
+ addlogs(this.config, 'staring to update the entry for reposting');
576
572
 
577
- entries = _.map(entries, function (entry) {
573
+ entries = _.map(entries, (entry) => {
578
574
  try {
579
575
  let uid = entry.uid;
580
576
  let updatedEntry;
@@ -600,15 +596,15 @@ EntriesImport.prototype = {
600
596
  return _entry;
601
597
  } catch (error) {
602
598
  addlogs(
603
- config,
599
+ this.config,
604
600
  `Failed to update the entry ${uid} references while reposting ${util.formatError(error)}`,
605
601
  );
606
602
  }
607
603
  });
608
604
 
609
- addlogs(config, 'Starting the reposting process for entries');
605
+ addlogs(this.config, 'Starting the reposting process for entries');
610
606
 
611
- const entryBatchLimit = eConfig.batchLimit || 10;
607
+ const entryBatchLimit = this.eConfig.batchLimit || 10;
612
608
  const batchSize = Math.round(entryBatchLimit / 3);
613
609
  // Run entry creation in batches
614
610
  for (let i = 0; i < entries.length; i += batchSize) {
@@ -616,14 +612,14 @@ EntriesImport.prototype = {
616
612
  }
617
613
  return Promise.map(
618
614
  batches,
619
- async function (batch, index) {
615
+ async (batch, index) => {
620
616
  return Promise.map(
621
617
  batch,
622
- async function (entry) {
618
+ async (entry) => {
623
619
  entry.uid = self.mappedUids[entry.uid];
624
620
  if (refsUpdatedUids.indexOf(entry.uid) !== -1) {
625
621
  addlogs(
626
- config,
622
+ this.config,
627
623
  'Entry: ' +
628
624
  entry.uid +
629
625
  ' in Content Type: ' +
@@ -637,27 +633,18 @@ EntriesImport.prototype = {
637
633
  }
638
634
 
639
635
  let promiseResult = new Promise((resolveUpdatedUids, rejectUpdatedUids) => {
640
- let entryResponse = client
641
- .stack({ api_key: config.target_stack, management_token: config.management_token })
642
- .contentType(ctUid)
643
- .entry(entry.uid);
636
+ let entryResponse = self.stackAPIClient.contentType(ctUid).entry(entry.uid);
644
637
  Object.assign(entryResponse, entry);
645
638
  delete entryResponse.publish_details;
646
639
  return entryResponse
647
640
  .update({ locale: lang })
648
641
  .then((response) => {
649
- // for (let j = 0; j < entries.length; j++) {
650
- // if (entries[j].uid === response.uid) {
651
- // entries[j] = response;
652
- // break;
653
- // }
654
- // }
655
642
  refsUpdatedUids.push(response.uid);
656
643
  return resolveUpdatedUids();
657
644
  })
658
- .catch(function (error) {
645
+ .catch((error) => {
659
646
  addlogs(
660
- config,
647
+ this.config,
661
648
  chalk.red(
662
649
  'Entry Uid: ' +
663
650
  entry.uid +
@@ -669,7 +656,7 @@ EntriesImport.prototype = {
669
656
  'error',
670
657
  );
671
658
 
672
- addlogs(config, util.formatError(error), 'error');
659
+ addlogs(this.config, util.formatError(error), 'error');
673
660
  refsUpdateFailed.push({
674
661
  content_type: ctUid,
675
662
  entry: entry,
@@ -682,20 +669,24 @@ EntriesImport.prototype = {
682
669
  await promiseResult;
683
670
  },
684
671
  {
685
- concurrency: importConcurrency,
672
+ concurrency: this.importConcurrency,
686
673
  },
687
674
  )
688
- .then(function () {
675
+ .then(() => {
689
676
  // batch completed successfully
690
677
  helper.writeFileSync(path.join(eFolderPath, 'success.json'), entries);
691
678
  helper.writeFileSync(path.join(eFolderPath, 'refsUpdatedUids.json'), refsUpdatedUids);
692
679
  helper.writeFileSync(path.join(eFolderPath, 'refsUpdateFailed.json'), refsUpdateFailed);
693
- addlogs(config, 'Completed re-post entries batch no: ' + (index + 1) + ' successfully!', 'success');
680
+ addlogs(
681
+ this.config,
682
+ 'Completed re-post entries batch no: ' + (index + 1) + ' successfully!',
683
+ 'success',
684
+ );
694
685
  })
695
- .catch(function (error) {
686
+ .catch((error) => {
696
687
  // error while executing entry in batch
697
- addlogs(config, chalk.red('Failed re-post entries at batch no: ' + (index + 1)), 'error');
698
- addlogs(config, util.formatError(error), 'error');
688
+ addlogs(this.config, chalk.red('Failed re-post entries at batch no: ' + (index + 1)), 'error');
689
+ addlogs(this.config, util.formatError(error), 'error');
699
690
  // throw error;
700
691
  });
701
692
  },
@@ -703,18 +694,21 @@ EntriesImport.prototype = {
703
694
  concurrency: 1,
704
695
  },
705
696
  )
706
- .then(function () {
697
+ .then(() => {
707
698
  // finished updating entries with references
708
699
  addlogs(
709
- config,
700
+ this.config,
710
701
  "Imported entries of Content Type: '" + ctUid + "' in language: '" + lang + "' successfully!",
711
702
  'success',
712
703
  );
713
704
  })
714
- .catch(function (error) {
705
+ .catch((error) => {
715
706
  // error while updating entries with references
716
- addlogs(config, chalk.red(`Failed re-post entries of content type ${ctUid} locale ${lang}`, 'error'));
717
- addlogs(config, util.formatError(error), 'error');
707
+ addlogs(
708
+ this.config,
709
+ chalk.red(`Failed re-post entries of content type ${ctUid} locale ${lang}`, 'error'),
710
+ );
711
+ addlogs(this.config, util.formatError(error), 'error');
718
712
  // throw error;
719
713
  });
720
714
  },
@@ -722,23 +716,23 @@ EntriesImport.prototype = {
722
716
  concurrency: 1,
723
717
  },
724
718
  )
725
- .then(function () {
719
+ .then(() => {
726
720
  // completed updating entry references
727
- addlogs(config, chalk.green("Imported entries in '" + lang + "' language successfully!"), 'success');
721
+ addlogs(this.config, chalk.green("Imported entries in '" + lang + "' language successfully!"), 'success');
728
722
  return resolve();
729
723
  })
730
- .catch(function (error) {
724
+ .catch((error) => {
731
725
  // error while updating entry references
732
- addlogs(config, chalk.red('Failed to re post entries in ' + lang + ' language'), 'error');
726
+ addlogs(this.config, chalk.red('Failed to re post entries in ' + lang + ' language'), 'error');
733
727
  return reject(error);
734
728
  });
735
729
  });
736
- },
737
- supressFields: async function () {
730
+ }
731
+ supressFields() {
738
732
  // it should be spelled as suppressFields
739
- addlogs(config, 'Suppressing content type reference fields', 'success');
733
+ addlogs(this.config, 'Suppressing content type reference fields', 'success');
740
734
  let self = this;
741
- return new Promise(async function (resolve, reject) {
735
+ return new Promise(async (resolve, reject) => {
742
736
  let modifiedSchemas = [];
743
737
  let suppressedSchemas = [];
744
738
 
@@ -781,48 +775,46 @@ EntriesImport.prototype = {
781
775
  }
782
776
 
783
777
  // Replace extensions with new UID
784
- extension_suppress(contentTypeSchema.schema, config.preserveStackVersion, self.installedExtensions);
778
+ extension_suppress(contentTypeSchema.schema, this.config.preserveStackVersion, self.installedExtensions);
785
779
  }
786
780
  }
787
781
 
788
782
  // write modified schema in backup file
789
- helper.writeFileSync(modifiedSchemaPath, modifiedSchemas);
783
+ helper.writeFileSync(this.modifiedSchemaPath, modifiedSchemas);
790
784
 
791
785
  return Promise.map(
792
786
  suppressedSchemas,
793
- async function (schema) {
794
- let contentTypeResponse = client
795
- .stack({ api_key: config.target_stack, management_token: config.management_token })
796
- .contentType(schema.uid);
787
+ async (schema) => {
788
+ let contentTypeResponse = self.stackAPIClient.contentType(schema.uid);
797
789
  Object.assign(contentTypeResponse, _.cloneDeep(schema));
798
790
  return contentTypeResponse
799
791
  .update()
800
792
  .then((_updatedcontentType) => {
801
793
  // empty function
802
794
  })
803
- .catch(function (_error) {
804
- addlogs(config, util.formatError(error), 'error');
795
+ .catch((_error) => {
796
+ addlogs(this.config, util.formatError(error), 'error');
805
797
  reject(`Failed suppress content type ${schema.uid} reference fields`);
806
798
  });
807
799
  // update 5 content types at a time
808
800
  },
809
801
  {
810
802
  // update reqConcurrency content types at a time
811
- concurrency: importConcurrency,
803
+ concurrency: this.importConcurrency,
812
804
  },
813
805
  )
814
- .then(function () {
806
+ .then(() => {
815
807
  return resolve();
816
808
  })
817
- .catch(function (error) {
818
- addlogs(config, util.formatError(error), 'error');
809
+ .catch((error) => {
810
+ addlogs(this.config, util.formatError(error), 'error');
819
811
  return reject('Failed to suppress reference fields in content type');
820
812
  });
821
813
  });
822
- },
823
- fetchEntry: function (query) {
814
+ }
815
+ fetchEntry(query) {
824
816
  let self = this;
825
- return new Promise(function (resolve, _reject) {
817
+ return new Promise((resolve, _reject) => {
826
818
  let requestObject = {
827
819
  qs: {
828
820
  query: {
@@ -832,39 +824,38 @@ EntriesImport.prototype = {
832
824
  },
833
825
  };
834
826
 
835
- return client
836
- .stack({ api_key: config.target_stack, management_token: config.management_token })
827
+ return self.stackAPIClient
837
828
  .contentType(query.content_type)
838
829
  .entry()
839
830
  .query(requestObject.qs)
840
831
  .find()
841
- .then(function (response) {
832
+ .then((response) => {
842
833
  if (response.body.entries.length <= 0) {
843
- addlogs(config, 'Unable to map entry WO uid: ' + query.entry.uid, 'error');
834
+ addlogs(this.config, 'Unable to map entry WO uid: ' + query.entry.uid, 'error');
844
835
  self.failedWO.push(query);
845
836
  return resolve();
846
837
  }
847
838
  self.mappedUids[query.entry.uid] = response.body.entries[0].uid;
848
- let _ePath = path.join(entryMapperPath, query.locale, query.content_type, 'success.json');
839
+ let _ePath = path.join(this.entryMapperPath, query.locale, query.content_type, 'success.json');
849
840
  let entries = helper.readFileSync(_ePath);
850
841
  entries.push(query.entry);
851
842
  helper.writeFileSync(_ePath, entries);
852
843
  addlogs(
853
- config,
844
+ this.config,
854
845
  'Completed mapping entry wo uid: ' + query.entry.uid + ': ' + response.body.entries[0].uid,
855
846
  'clientsuccess',
856
847
  );
857
848
  return resolve();
858
849
  })
859
- .catch(function (_error) {
850
+ .catch((_error) => {
860
851
  return resolve();
861
852
  });
862
853
  });
863
- },
864
- unSuppressFields: function () {
854
+ }
855
+ unSuppressFields() {
865
856
  let self = this;
866
- return new Promise(async function (resolve, reject) {
867
- let modifiedSchemas = helper.readFileSync(modifiedSchemaPath);
857
+ return new Promise(async (resolve, reject) => {
858
+ let modifiedSchemas = helper.readFileSync(this.modifiedSchemaPath);
868
859
  let modifiedSchemasUids = [];
869
860
  let updatedExtensionUidsSchemas = [];
870
861
  for (let uid in modifiedSchemas) {
@@ -874,17 +865,16 @@ EntriesImport.prototype = {
874
865
  delete _contentTypeSchema.field_rules;
875
866
  }
876
867
 
877
- extension_suppress(_contentTypeSchema.schema, config.preserveStackVersion, self.installedExtensions);
868
+ extension_suppress(_contentTypeSchema.schema, this.config.preserveStackVersion, self.installedExtensions);
878
869
  updatedExtensionUidsSchemas.push(_contentTypeSchema);
879
870
  }
880
871
  }
881
872
 
882
873
  return Promise.map(
883
874
  updatedExtensionUidsSchemas,
884
- async function (schema) {
875
+ async (schema) => {
885
876
  let promise = new Promise((resolveContentType, rejectContentType) => {
886
- client
887
- .stack({ api_key: config.target_stack, management_token: config.management_token })
877
+ self.stackAPIClient
888
878
  .contentType(schema.uid)
889
879
  .fetch()
890
880
  .then((contentTypeResponse) => {
@@ -894,29 +884,29 @@ EntriesImport.prototype = {
894
884
  .then((_updatedcontentType) => {
895
885
  modifiedSchemasUids.push(schema.uid);
896
886
  addlogs(
897
- config,
887
+ this.config,
898
888
  chalk.white("Content type: '" + schema.uid + "' has been restored to its previous glory!"),
899
889
  );
900
890
  return resolveContentType();
901
891
  })
902
- .catch(function (error) {
903
- addlogs(config, chalk.red('Failed to re-update ' + schema.uid), 'error');
904
- addlogs(config, error, 'error');
892
+ .catch((error) => {
893
+ addlogs(this.config, chalk.red('Failed to re-update ' + schema.uid), 'error');
894
+ addlogs(this.config, error, 'error');
905
895
  return rejectContentType(error);
906
896
  });
907
897
  })
908
- .catch(function (error) {
909
- addlogs(config, error, 'error');
898
+ .catch((error) => {
899
+ addlogs(this.config, error, 'error');
910
900
  return rejectContentType(error);
911
901
  });
912
902
  });
913
903
  await promise;
914
904
  },
915
905
  {
916
- concurrency: reqConcurrency,
906
+ concurrency: this.reqConcurrency,
917
907
  },
918
908
  )
919
- .then(function () {
909
+ .then(() => {
920
910
  for (let i = 0; i < modifiedSchemas.length; i++) {
921
911
  if (modifiedSchemasUids.indexOf(modifiedSchemas[i].uid) !== -1) {
922
912
  modifiedSchemas.splice(i, 1);
@@ -924,24 +914,24 @@ EntriesImport.prototype = {
924
914
  }
925
915
  }
926
916
  // re-write, in case some schemas failed to update
927
- helper.writeFileSync(modifiedSchemaPath, _.compact(modifiedSchemas));
928
- addlogs(config, 'Re-modified content type schemas to their original form!', 'success');
917
+ helper.writeFileSync(this.modifiedSchemaPath, _.compact(modifiedSchemas));
918
+ addlogs(this.config, 'Re-modified content type schemas to their original form!', 'success');
929
919
  return resolve();
930
920
  })
931
- .catch(function (error) {
921
+ .catch((error) => {
932
922
  // failed to update modified schemas back to their original form
933
923
  return reject(error);
934
924
  });
935
925
  });
936
- },
937
- removeBuggedEntries: function () {
926
+ }
927
+ removeBuggedEntries() {
938
928
  let self = this;
939
- return new Promise(function (resolve, reject) {
940
- let entries = helper.readFileSync(uniqueUidMapperPath);
929
+ return new Promise((resolve, reject) => {
930
+ let entries = helper.readFileSync(this.uniqueUidMapperPath);
941
931
  let bugged = [];
942
932
  let removed = [];
943
933
  for (let uid in entries) {
944
- if (entries[uid].locales.indexOf(masterLanguage.code) === -1) {
934
+ if (entries[uid].locales.indexOf(this.masterLanguage.code) === -1) {
945
935
  bugged.push({
946
936
  content_type: entries[uid].content_type,
947
937
  uid: uid,
@@ -951,26 +941,25 @@ EntriesImport.prototype = {
951
941
 
952
942
  return Promise.map(
953
943
  bugged,
954
- function (entry) {
955
- return client
956
- .stack({ api_key: config.target_stack, management_token: config.management_token })
944
+ (entry) => {
945
+ return self.stackAPIClient
957
946
  .contentType(entry.content_type)
958
947
  .entry(self.mappedUids[entry.uid])
959
- .delete({ locale: masterLanguage.code })
960
- .then(function () {
948
+ .delete({ locale: this.masterLanguage.code })
949
+ .then(() => {
961
950
  removed.push(self.mappedUids[entry.uid]);
962
- addlogs(config, 'Removed bugged entry from master ' + JSON.stringify(entry), 'success');
951
+ addlogs(this.config, 'Removed bugged entry from master ' + JSON.stringify(entry), 'success');
963
952
  })
964
- .catch(function (error) {
965
- addlogs(config, chalk.red('Failed to remove bugged entry from master language'), 'error');
966
- addlogs(config, util.formatError(error), 'error');
953
+ .catch((error) => {
954
+ addlogs(this.config, chalk.red('Failed to remove bugged entry from master language'), 'error');
955
+ addlogs(this.config, util.formatError(error), 'error');
967
956
  });
968
957
  },
969
958
  {
970
- concurrency: importConcurrency,
959
+ concurrency: this.importConcurrency,
971
960
  },
972
961
  )
973
- .then(function () {
962
+ .then(() => {
974
963
  for (let i = 0; i < bugged.length; i++) {
975
964
  if (removed.indexOf(bugged[i].uid) !== -1) {
976
965
  bugged.splice(i, 1);
@@ -978,20 +967,20 @@ EntriesImport.prototype = {
978
967
  }
979
968
  }
980
969
 
981
- helper.writeFileSync(path.join(entryMapperPath, 'removed-uids.json'), removed);
982
- helper.writeFileSync(path.join(entryMapperPath, 'pending-uids.json'), bugged);
970
+ helper.writeFileSync(path.join(this.entryMapperPath, 'removed-uids.json'), removed);
971
+ helper.writeFileSync(path.join(this.entryMapperPath, 'pending-uids.json'), bugged);
983
972
 
984
- addlogs(config, chalk.green('The stack has been eradicated from bugged entries!'), 'success');
973
+ addlogs(this.config, chalk.green('The stack has been eradicated from bugged entries!'), 'success');
985
974
  return resolve();
986
975
  })
987
- .catch(function (error) {
976
+ .catch((error) => {
988
977
  // error while removing bugged entries from stack
989
- addlogs(config, util.formatError(error), 'error');
978
+ addlogs(this.config, util.formatError(error), 'error');
990
979
  });
991
980
  });
992
- },
993
- field_rules_update: function (schema) {
994
- return new Promise(function (resolve, reject) {
981
+ }
982
+ field_rules_update(schema) {
983
+ return new Promise((resolve, reject) => {
995
984
  if (schema.field_rules) {
996
985
  let fieldRuleLength = schema.field_rules.length;
997
986
  for (let k = 0; k < fieldRuleLength; k++) {
@@ -1003,7 +992,7 @@ EntriesImport.prototype = {
1003
992
  let updatedValue = [];
1004
993
  for (const element of fieldRulesArray) {
1005
994
  let splitedFieldRulesValue = element;
1006
- let oldUid = helper.readFileSync(path.join(entryUidMapperPath));
995
+ let oldUid = helper.readFileSync(path.join(this.entryUidMapperPath));
1007
996
  if (oldUid.hasOwnProperty(splitedFieldRulesValue)) {
1008
997
  updatedValue.push(oldUid[splitedFieldRulesValue]);
1009
998
  } else {
@@ -1015,11 +1004,10 @@ EntriesImport.prototype = {
1015
1004
  }
1016
1005
  }
1017
1006
  } else {
1018
- addlogs(config, 'field_rules is not available', 'error');
1007
+ addlogs(this.config, 'field_rules is not available', 'error');
1019
1008
  }
1020
1009
 
1021
- client
1022
- .stack({ api_key: config.target_stack, management_token: config.management_token })
1010
+ self.stackAPIClient
1023
1011
  .contentType(schema.uid)
1024
1012
  .fetch()
1025
1013
  .then((contentTypeResponse) => {
@@ -1030,29 +1018,29 @@ EntriesImport.prototype = {
1030
1018
  .then(() => {
1031
1019
  return resolve();
1032
1020
  })
1033
- .catch(function (error) {
1034
- addlogs(config, `failed to update the field rules ${util.formatError(error)}`);
1021
+ .catch((error) => {
1022
+ addlogs(this.config, `failed to update the field rules ${util.formatError(error)}`);
1035
1023
  });
1036
1024
  });
1037
- },
1038
- publish: function (langs) {
1025
+ }
1026
+ publish(langs) {
1039
1027
  let self = this;
1040
1028
  let requestObject = {
1041
1029
  entry: {},
1042
1030
  };
1043
1031
 
1044
1032
  let contentTypeUids = Object.keys(self.ctSchemas);
1045
- let entryMapper = helper.readFileSync(entryUidMapperPath);
1033
+ let entryMapper = helper.readFileSync(this.entryUidMapperPath);
1046
1034
 
1047
- return new Promise(function (resolve, reject) {
1035
+ return new Promise((resolve, reject) => {
1048
1036
  return Promise.map(
1049
1037
  langs,
1050
- function (_lang, counter) {
1038
+ (_lang, counter) => {
1051
1039
  let lang = langs[counter];
1052
1040
  return Promise.map(
1053
1041
  contentTypeUids,
1054
- async function (ctUid) {
1055
- let eFilePath = path.resolve(ePath, ctUid, lang + '.json');
1042
+ async (ctUid) => {
1043
+ let eFilePath = path.resolve(this.ePath, ctUid, lang + '.json');
1056
1044
  let entries = await helper.readLargeFile(eFilePath);
1057
1045
 
1058
1046
  let eUids = Object.keys(entries);
@@ -1060,7 +1048,7 @@ EntriesImport.prototype = {
1060
1048
  let batchSize;
1061
1049
 
1062
1050
  if (eUids.length > 0) {
1063
- let entryBatchLimit = eConfig.batchLimit || 10;
1051
+ let entryBatchLimit = this.eConfig.batchLimit || 10;
1064
1052
  batchSize = Math.round(entryBatchLimit / 3);
1065
1053
  // Run entry creation in batches
1066
1054
  for (let i = 0; i < eUids.length; i += batchSize) {
@@ -1072,15 +1060,15 @@ EntriesImport.prototype = {
1072
1060
 
1073
1061
  return Promise.map(
1074
1062
  batches,
1075
- async function (batch, index) {
1063
+ async (batch, index) => {
1076
1064
  return Promise.map(
1077
1065
  batch,
1078
- async function (eUid) {
1066
+ async (eUid) => {
1079
1067
  let entry = entries[eUid];
1080
1068
  let envId = [];
1081
1069
  let locales = [];
1082
1070
  if (entry.publish_details && entry.publish_details.length > 0) {
1083
- _.forEach(entries[eUid].publish_details, function (pubObject) {
1071
+ _.forEach(entries[eUid].publish_details, (pubObject) => {
1084
1072
  if (
1085
1073
  self.environment.hasOwnProperty(pubObject.environment) &&
1086
1074
  _.indexOf(envId, self.environment[pubObject.environment].name) === -1
@@ -1100,25 +1088,24 @@ EntriesImport.prototype = {
1100
1088
  requestObject.entry.environments = envId;
1101
1089
  requestObject.entry.locales = locales;
1102
1090
  return new Promise((resolveEntryPublished, rejectEntryPublished) => {
1103
- client
1104
- .stack({ api_key: config.target_stack, management_token: config.management_token })
1091
+ self.stackAPIClient
1105
1092
  .contentType(ctUid)
1106
1093
  .entry(entryUid)
1107
1094
  .publish({ publishDetails: requestObject.entry, locale: lang })
1108
1095
  // eslint-disable-next-line max-nested-callbacks
1109
1096
  .then((result) => {
1110
- // addlogs(config, 'Entry ' + eUid + ' published successfully in ' + ctUid + ' content type', 'success')
1097
+ // addlogs(this.config, 'Entry ' + eUid + ' published successfully in ' + ctUid + ' content type', 'success')
1111
1098
  addlogs(
1112
- config,
1099
+ this.config,
1113
1100
  'Entry ' + eUid + ' published successfully in ' + ctUid + ' content type',
1114
1101
  'success',
1115
1102
  );
1116
1103
  return resolveEntryPublished(result);
1117
1104
  // eslint-disable-next-line max-nested-callbacks
1118
1105
  })
1119
- .catch(function (err) {
1106
+ .catch((err) => {
1120
1107
  addlogs(
1121
- config,
1108
+ this.config,
1122
1109
  `failed to publish entry ${eUid} content type ${ctUid} ${util.formatError(err)}`,
1123
1110
  );
1124
1111
  return resolveEntryPublished('');
@@ -1133,35 +1120,35 @@ EntriesImport.prototype = {
1133
1120
  concurrency: 1,
1134
1121
  },
1135
1122
  )
1136
- .then(function () {
1123
+ .then(() => {
1137
1124
  // empty function
1138
1125
  })
1139
- .catch(function (error) {
1126
+ .catch((error) => {
1140
1127
  // error while executing entry in batch
1141
- addlogs(config, util.formatError(error), 'error');
1128
+ addlogs(this.config, util.formatError(error), 'error');
1142
1129
  });
1143
1130
  },
1144
1131
  {
1145
1132
  concurrency: 1,
1146
1133
  },
1147
1134
  )
1148
- .then(function () {
1149
- // addlogs(config, 'Entries published successfully in ' + ctUid + ' content type', 'success')
1135
+ .then(() => {
1136
+ // addlogs(this.config, 'Entries published successfully in ' + ctUid + ' content type', 'success')
1150
1137
  addlogs('Entries published successfully in ' + ctUid + ' content type');
1151
1138
  })
1152
- .catch(function (error) {
1153
- addlogs(config, `failed to publish entry in content type ${ctUid} ${util.formatError(error)}`);
1139
+ .catch((error) => {
1140
+ addlogs(this.config, `failed to publish entry in content type ${ctUid} ${util.formatError(error)}`);
1154
1141
  });
1155
1142
  },
1156
1143
  {
1157
1144
  concurrency: 1,
1158
1145
  },
1159
1146
  )
1160
- .then(function () {
1147
+ .then(() => {
1161
1148
  // empty function
1162
1149
  // addlogs('Published entries successfully in ' +);
1163
1150
  })
1164
- .catch(function (error) {
1151
+ .catch((error) => {
1165
1152
  addlogs(`Failed to publish few entries in ${lang} ${util.formatError(error)}`);
1166
1153
  });
1167
1154
  },
@@ -1169,16 +1156,15 @@ EntriesImport.prototype = {
1169
1156
  concurrency: 1,
1170
1157
  },
1171
1158
  )
1172
- .then(function () {
1159
+ .then(() => {
1173
1160
  return resolve();
1174
1161
  })
1175
1162
  .catch((error) => {
1176
1163
  addlogs(`Failed to publish entries ${util.formatError(error)}`);
1177
- // return reject(error);
1178
1164
  });
1179
1165
  });
1180
- },
1181
- removeEntryRefsFromJSONRTE: function (entry, ctSchema) {
1166
+ }
1167
+ removeEntryRefsFromJSONRTE(entry, ctSchema) {
1182
1168
  for (const element of ctSchema) {
1183
1169
  switch (element.data_type) {
1184
1170
  case 'blocks': {
@@ -1235,8 +1221,8 @@ EntriesImport.prototype = {
1235
1221
  }
1236
1222
  }
1237
1223
  return entry;
1238
- },
1239
- doEntryReferencesExist: function (element) {
1224
+ }
1225
+ doEntryReferencesExist(element) {
1240
1226
  // checks if the children of p element contain any references
1241
1227
  // only checking one level deep, not recursive
1242
1228
 
@@ -1258,10 +1244,10 @@ EntriesImport.prototype = {
1258
1244
  }
1259
1245
  }
1260
1246
  return false;
1261
- },
1262
- restoreJsonRteEntryRefs: function (entry, sourceStackEntry, ctSchema) {
1263
- let mappedAssetUids = helper.readFileSync(mappedAssetUidPath) || {};
1264
- let mappedAssetUrls = helper.readFileSync(mappedAssetUrlPath) || {};
1247
+ }
1248
+ restoreJsonRteEntryRefs(entry, sourceStackEntry, ctSchema) {
1249
+ let mappedAssetUids = helper.readFileSync(this.mappedAssetUidPath) || {};
1250
+ let mappedAssetUrls = helper.readFileSync(this.mappedAssetUrlPath) || {};
1265
1251
  for (const element of ctSchema) {
1266
1252
  switch (element.data_type) {
1267
1253
  case 'blocks': {
@@ -1355,11 +1341,11 @@ EntriesImport.prototype = {
1355
1341
  }
1356
1342
  }
1357
1343
  return entry;
1358
- },
1359
- isEntryRef: function (element) {
1344
+ }
1345
+ isEntryRef(element) {
1360
1346
  return element.type === 'reference' && element.attrs.type === 'entry';
1361
- },
1362
- removeUidsFromJsonRteFields: function (entry, ctSchema) {
1347
+ }
1348
+ removeUidsFromJsonRteFields(entry, ctSchema) {
1363
1349
  for (const element of ctSchema) {
1364
1350
  switch (element.data_type) {
1365
1351
  case 'blocks': {
@@ -1407,7 +1393,7 @@ EntriesImport.prototype = {
1407
1393
  });
1408
1394
  } else {
1409
1395
  delete entry[element.uid].uid; // remove uid
1410
- if (entry[element.uid] && _.isObject(entry[element.uid].attrs)) {
1396
+ if (entry[element.uid] && _.isObject(entry[element.uid].attrs)) {
1411
1397
  entry[element.uid].attrs.dirty = true;
1412
1398
  }
1413
1399
  if (entry[element.uid] && !_.isEmpty(entry[element.uid].children)) {
@@ -1422,8 +1408,8 @@ EntriesImport.prototype = {
1422
1408
  }
1423
1409
  }
1424
1410
  return entry;
1425
- },
1426
- removeUidsFromChildren: function (children) {
1411
+ }
1412
+ removeUidsFromChildren(children) {
1427
1413
  if (children.length && children.length > 0) {
1428
1414
  return children.map((child) => {
1429
1415
  if (child.type && child.type.length > 0) {
@@ -1450,8 +1436,8 @@ EntriesImport.prototype = {
1450
1436
  }
1451
1437
  return children;
1452
1438
  }
1453
- },
1454
- setDirtyTrue: function (jsonRteChild) {
1439
+ }
1440
+ setDirtyTrue(jsonRteChild) {
1455
1441
  // also removing uids in this function
1456
1442
  if (jsonRteChild.type) {
1457
1443
  if (_.isObject(jsonRteChild.attrs)) {
@@ -1464,8 +1450,8 @@ EntriesImport.prototype = {
1464
1450
  }
1465
1451
  }
1466
1452
  return jsonRteChild;
1467
- },
1468
- resolveAssetRefsInEntryRefsForJsonRte: function (jsonRteChild, mappedAssetUids, mappedAssetUrls) {
1453
+ }
1454
+ resolveAssetRefsInEntryRefsForJsonRte(jsonRteChild, mappedAssetUids, mappedAssetUrls) {
1469
1455
  if (jsonRteChild.type) {
1470
1456
  if (jsonRteChild.attrs.type === 'asset') {
1471
1457
  let assetUrl;
@@ -1496,7 +1482,5 @@ EntriesImport.prototype = {
1496
1482
  }
1497
1483
 
1498
1484
  return jsonRteChild;
1499
- },
1485
+ }
1500
1486
  };
1501
-
1502
- module.exports = EntriesImport;