@contentstack/cli-cm-import 0.1.1-beta.1 → 0.1.1-beta.10
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/README.md +10 -11
- package/oclif.manifest.json +1 -1
- package/package.json +12 -9
- package/src/app.js +108 -75
- package/src/commands/cm/import.js +82 -38
- package/src/config/default.js +238 -211
- package/src/lib/import/assets.js +42 -38
- package/src/lib/import/content-types.js +90 -95
- package/src/lib/import/entries.js +426 -399
- package/src/lib/import/environments.js +1 -1
- package/src/lib/import/global-fields.js +3 -0
- package/src/lib/import/labels.js +43 -37
- package/src/lib/import/locales.js +32 -21
- package/src/lib/import/workflows.js +118 -0
- package/src/lib/util/contentstack-management-sdk.js +21 -9
- package/src/lib/util/extensionsUidReplace.js +27 -29
- package/src/lib/util/import-flags.js +129 -75
- package/src/lib/util/index.js +77 -5
- package/src/lib/util/log.js +4 -2
- package/src/lib/util/login.js +2 -2
- package/src/lib/util/lookupReplaceEntries.js +92 -94
- package/src/lib/util/supress-mandatory-fields.js +1 -1
|
@@ -10,13 +10,12 @@ const _ = require('lodash')
|
|
|
10
10
|
const mkdirp = require('mkdirp')
|
|
11
11
|
const chalk = require('chalk')
|
|
12
12
|
|
|
13
|
-
const request = require('../util/request')
|
|
14
13
|
const helper = require('../util/fs')
|
|
15
14
|
const {addlogs} = require('../util/log')
|
|
16
15
|
const lookupReplaceAssets = require('../util/lookupReplaceAssets')
|
|
17
16
|
const lookupReplaceEntries = require('../util/lookupReplaceEntries')
|
|
18
|
-
const
|
|
19
|
-
const
|
|
17
|
+
const suppress = require('../util/supress-mandatory-fields')
|
|
18
|
+
const extension_suppress = require('../util/extensionsUidReplace')
|
|
20
19
|
const util = require('../util')
|
|
21
20
|
let config = util.getConfig()
|
|
22
21
|
const stack = require('../util/contentstack-management-sdk')
|
|
@@ -37,12 +36,10 @@ let uniqueUidMapperPath
|
|
|
37
36
|
let modifiedSchemaPath
|
|
38
37
|
let createdEntriesWOUidPath
|
|
39
38
|
let failedWOPath
|
|
40
|
-
let
|
|
39
|
+
let masterLanguage
|
|
41
40
|
|
|
42
|
-
|
|
43
|
-
let masterLanguage = config.master_locale
|
|
44
41
|
let skipFiles = ['__master.json', '__priority.json', 'schema.json']
|
|
45
|
-
let entryBatchLimit =
|
|
42
|
+
let entryBatchLimit = config.rateLimit || 10
|
|
46
43
|
|
|
47
44
|
function importEntries() {
|
|
48
45
|
let self = this
|
|
@@ -74,38 +71,21 @@ function importEntries() {
|
|
|
74
71
|
// Entries that failed to get created OR updated
|
|
75
72
|
this.fails = []
|
|
76
73
|
|
|
77
|
-
this.languages = helper.readFile(lPath)
|
|
78
|
-
|
|
79
74
|
let files = fs.readdirSync(ctPath)
|
|
80
|
-
|
|
81
75
|
this.environment = helper.readFile(environmentPath)
|
|
82
|
-
|
|
83
76
|
for (let index in files) {
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
if (files[index]
|
|
87
|
-
|
|
88
|
-
|
|
77
|
+
if (index) {
|
|
78
|
+
try {
|
|
79
|
+
if (skipFiles.indexOf(files[index]) === -1) {
|
|
80
|
+
if (files[index] != 'field_rules_uid.json') {
|
|
81
|
+
let schema = require(path.resolve(path.join(ctPath, files[index])))
|
|
82
|
+
self.ctSchemas[schema.uid] = schema
|
|
83
|
+
}
|
|
89
84
|
}
|
|
85
|
+
} catch (error) {
|
|
86
|
+
console.error(error)
|
|
87
|
+
process.exit(0)
|
|
90
88
|
}
|
|
91
|
-
} catch (error) {
|
|
92
|
-
console.error(error)
|
|
93
|
-
process.exit(0)
|
|
94
|
-
}
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
this.mappedAssetUids = helper.readFile(mappedAssetUidPath)
|
|
98
|
-
this.mappedAssetUrls = helper.readFile(mappedAssetUrlPath)
|
|
99
|
-
|
|
100
|
-
this.mappedAssetUids = this.mappedAssetUids || {}
|
|
101
|
-
this.mappedAssetUrls = this.mappedAssetUrls || {}
|
|
102
|
-
|
|
103
|
-
this.requestOptionTemplate = {
|
|
104
|
-
// /v3/content_types/
|
|
105
|
-
uri: config.host + config.apis.content_types,
|
|
106
|
-
headers: config.headers,
|
|
107
|
-
json: {
|
|
108
|
-
entry: {}
|
|
109
89
|
}
|
|
110
90
|
}
|
|
111
91
|
}
|
|
@@ -114,71 +94,81 @@ importEntries.prototype = {
|
|
|
114
94
|
/**
|
|
115
95
|
* Start point for entry import
|
|
116
96
|
* @return promise
|
|
117
|
-
|
|
97
|
+
*/
|
|
118
98
|
start: async function (credentialConfig) {
|
|
119
99
|
let self = this
|
|
120
100
|
config = credentialConfig
|
|
121
101
|
client = stack.Client(config)
|
|
122
|
-
|
|
123
|
-
|
|
102
|
+
masterLanguage = config.master_locale
|
|
103
|
+
addlogs(config, 'Migrating entries', 'success')
|
|
104
|
+
let languages = helper.readFile(lPath)
|
|
105
|
+
return new Promise(function (resolve, reject) {
|
|
124
106
|
let langs = [masterLanguage.code]
|
|
125
|
-
for (let i in
|
|
126
|
-
|
|
107
|
+
for (let i in languages) {
|
|
108
|
+
if (i) {
|
|
109
|
+
langs.push(languages[i].code)
|
|
110
|
+
}
|
|
127
111
|
}
|
|
128
112
|
|
|
113
|
+
// Step 1: Removes filed rules from content type
|
|
114
|
+
// This allows to handle cases like self references and circular reference
|
|
115
|
+
// if mandatory reference fields are not filed in entries then avoids the error
|
|
116
|
+
// Also remove field visibility rules
|
|
129
117
|
return self.supressFields().then(async function () {
|
|
118
|
+
let mappedAssetUids = helper.readFile(mappedAssetUidPath) || {}
|
|
119
|
+
let mappedAssetUrls = helper.readFile(mappedAssetUrlPath) || {}
|
|
120
|
+
|
|
121
|
+
// Step 2: Iterate over available languages to create entries in each.
|
|
130
122
|
let counter = 0
|
|
131
123
|
return Promise.map(langs, async function () {
|
|
132
124
|
let lang = langs[counter]
|
|
133
|
-
if ((config.hasOwnProperty('onlylocales') && config.onlylocales.indexOf(lang) !== -1) || !
|
|
134
|
-
|
|
135
|
-
await self.
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
await self.repostEntries(lang)
|
|
140
|
-
// .then(function () {
|
|
141
|
-
addlogs(config, 'Successfully imported \'' + lang + '\' entries!', 'success')
|
|
142
|
-
counter++
|
|
143
|
-
// })
|
|
144
|
-
// })
|
|
145
|
-
// })
|
|
125
|
+
if ((config.hasOwnProperty('onlylocales') && config.onlylocales.indexOf(lang) !== -1) || !config.hasOwnProperty('onlylocales')) {
|
|
126
|
+
await self.createEntries(lang, mappedAssetUids, mappedAssetUrls)
|
|
127
|
+
await self.getCreatedEntriesWOUid()
|
|
128
|
+
await self.repostEntries(lang)
|
|
129
|
+
addlogs(config, 'Successfully imported \'' + lang + '\' entries!', 'success')
|
|
130
|
+
counter++
|
|
146
131
|
} else {
|
|
147
132
|
addlogs(config, lang + ' has not been configured for import, thus skipping it', 'success')
|
|
148
|
-
counter++
|
|
133
|
+
counter++
|
|
149
134
|
}
|
|
150
135
|
}, {
|
|
151
|
-
concurrency: 1
|
|
136
|
+
concurrency: 1,
|
|
152
137
|
}).then(async function () {
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
await self.field_rules_update(schema)
|
|
164
|
-
}
|
|
165
|
-
}
|
|
138
|
+
// Step 3: Revert all the changes done in content type in step 1
|
|
139
|
+
await self.unSuppressFields()
|
|
140
|
+
await self.removeBuggedEntries()
|
|
141
|
+
let ct_field_visibility_uid = helper.readFile(path.join(ctPath + '/field_rules_uid.json'))
|
|
142
|
+
let ct_files = fs.readdirSync(ctPath)
|
|
143
|
+
if (ct_field_visibility_uid && ct_field_visibility_uid != 'undefined') {
|
|
144
|
+
for (let index = 0; index < ct_field_visibility_uid.length; index++) {
|
|
145
|
+
if (ct_files.indexOf(ct_field_visibility_uid[index] + '.json') > -1) {
|
|
146
|
+
let schema = require(path.resolve(ctPath, ct_field_visibility_uid[index]))
|
|
147
|
+
await self.field_rules_update(schema)
|
|
166
148
|
}
|
|
167
|
-
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
addlogs(config, chalk.green('Entries have been imported successfully!'), 'success')
|
|
152
|
+
if (config.entriesPublish) {
|
|
153
|
+
return self.publish(langs).then(function () {
|
|
154
|
+
addlogs(config, chalk.green('All the entries have been published successfully'), 'success')
|
|
168
155
|
return resolve()
|
|
169
|
-
|
|
170
|
-
|
|
156
|
+
}).catch(errors => {
|
|
157
|
+
addlogs(config, chalk.error('Some entries might have failed to publish.'), 'error')
|
|
158
|
+
return reject(errors)
|
|
159
|
+
})
|
|
160
|
+
}
|
|
161
|
+
return resolve()
|
|
171
162
|
})
|
|
172
163
|
}).catch(function (error) {
|
|
173
164
|
return reject(error)
|
|
174
165
|
})
|
|
175
|
-
}).catch(error => {
|
|
176
166
|
})
|
|
177
167
|
},
|
|
178
168
|
|
|
179
|
-
createEntries: function (lang) {
|
|
169
|
+
createEntries: function (lang, mappedAssetUids, mappedAssetUrls) {
|
|
180
170
|
let self = this
|
|
181
|
-
return new Promise(
|
|
171
|
+
return new Promise(function (resolve, reject) {
|
|
182
172
|
let contentTypeUids = Object.keys(self.ctSchemas)
|
|
183
173
|
if (fs.existsSync(entryUidMapperPath)) {
|
|
184
174
|
self.mappedUids = helper.readFile(entryUidMapperPath)
|
|
@@ -196,84 +186,118 @@ importEntries.prototype = {
|
|
|
196
186
|
let failedEntryLogPath = path.join(eLogFolderPath, 'fails.json')
|
|
197
187
|
let createdEntriesPath = path.join(eLogFolderPath, 'created-entries.json')
|
|
198
188
|
let createdEntries = {}
|
|
189
|
+
let stack = client.stack({api_key: config.target_stack, management_token: config.management_token})
|
|
190
|
+
|
|
199
191
|
if (fs.existsSync(createdEntriesPath)) {
|
|
200
192
|
createdEntries = helper.readFile(createdEntriesPath)
|
|
201
193
|
createdEntries = createdEntries || {}
|
|
202
194
|
}
|
|
203
195
|
if (fs.existsSync(eFilePath)) {
|
|
204
196
|
let entries = helper.readFile(eFilePath)
|
|
205
|
-
if (!_.isPlainObject(entries)) {
|
|
197
|
+
if (!_.isPlainObject(entries) || _.isEmpty(entries)) {
|
|
206
198
|
addlogs(config, chalk.white('No entries were found for Content type:\'' + ctUid + '\' in \'' + lang +
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
for (let i = 0; i < eUids.length; i += entryBatchLimit) {
|
|
222
|
-
batches.push(eUids.slice(i, i + entryBatchLimit))
|
|
223
|
-
}
|
|
199
|
+
'\' language!'), 'success')
|
|
200
|
+
} else {
|
|
201
|
+
addlogs(config, `Creating entries for content type ${ctUid} in language ${lang} ...`, 'success')
|
|
202
|
+
for (let eUid in entries) {
|
|
203
|
+
if (eUid) {
|
|
204
|
+
// will replace all old asset uid/urls with new ones
|
|
205
|
+
entries[eUid] = lookupReplaceAssets({
|
|
206
|
+
content_type: self.ctSchemas[ctUid],
|
|
207
|
+
entry: entries[eUid],
|
|
208
|
+
}, mappedAssetUids, mappedAssetUrls, eLangFolderPath)
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
let eUids = Object.keys(entries)
|
|
212
|
+
let batches = []
|
|
224
213
|
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
self.uniqueUids[eUid]
|
|
214
|
+
// Run entry creation in batches of ~16~ entries
|
|
215
|
+
for (let i = 0; i < eUids.length; i += Math.round(entryBatchLimit / 3)) {
|
|
216
|
+
batches.push(eUids.slice(i, i + Math.round(entryBatchLimit / 3)))
|
|
217
|
+
}
|
|
218
|
+
return Promise.map(batches, async function (batch) {
|
|
219
|
+
return Promise.map(batch, async function (eUid) {
|
|
220
|
+
// if entry is already created
|
|
221
|
+
if (createdEntries.hasOwnProperty(eUid)) {
|
|
222
|
+
addlogs(config, ('Skipping ' + JSON.stringify({
|
|
223
|
+
content_type: ctUid,
|
|
224
|
+
locale: lang,
|
|
225
|
+
oldEntryUid: eUid,
|
|
226
|
+
newEntryUid: createdEntries[eUid],
|
|
227
|
+
}) + ' as it is already created'), 'success')
|
|
228
|
+
self.success[ctUid] = createdEntries[eUid]
|
|
229
|
+
// if its a non-master language, i.e. the entry isn't present in the master language
|
|
230
|
+
if (lang !== masterLanguage) {
|
|
231
|
+
self.uniqueUids[eUid] = self.uniqueUids[eUid] || {}
|
|
232
|
+
if (self.uniqueUids[eUid].locales) {
|
|
233
|
+
self.uniqueUids[eUid].locales.push(lang)
|
|
234
|
+
} else {
|
|
235
|
+
self.uniqueUids[eUid].locales = [lang]
|
|
236
|
+
}
|
|
237
|
+
self.uniqueUids[eUid].content_type = ctUid
|
|
243
238
|
}
|
|
244
|
-
|
|
239
|
+
return
|
|
245
240
|
}
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
entry: entries[eUid]
|
|
241
|
+
let requestObject = {
|
|
242
|
+
qs: {
|
|
243
|
+
locale: lang,
|
|
244
|
+
},
|
|
245
|
+
json: {
|
|
246
|
+
entry: entries[eUid],
|
|
247
|
+
},
|
|
254
248
|
}
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
if
|
|
265
|
-
|
|
249
|
+
if (self.mappedUids.hasOwnProperty(eUid)) {
|
|
250
|
+
let entryToUpdate = stack.contentType(ctUid).entry(self.mappedUids[eUid])
|
|
251
|
+
Object.assign(entryToUpdate, _.cloneDeep(entries[eUid]))
|
|
252
|
+
return entryToUpdate.update({locale: entryToUpdate.locale}).then(async entryResponse => {
|
|
253
|
+
self.success[ctUid] = self.success[ctUid] || []
|
|
254
|
+
self.success[ctUid].push(entries[eUid])
|
|
255
|
+
if (!self.mappedUids.hasOwnProperty(eUid)) {
|
|
256
|
+
self.mappedUids[eUid] = entryResponse.uid
|
|
257
|
+
createdEntries = entryResponse
|
|
258
|
+
// if its a non-master language, i.e. the entry isn't present in the master language
|
|
259
|
+
if (lang !== masterLanguage) {
|
|
260
|
+
self.uniqueUids[eUid] = self.uniqueUids[eUid] || {}
|
|
261
|
+
if (self.uniqueUids[eUid].locales) {
|
|
262
|
+
self.uniqueUids[eUid].locales.push(lang)
|
|
263
|
+
} else {
|
|
264
|
+
self.uniqueUids[eUid].locales = [lang]
|
|
265
|
+
}
|
|
266
|
+
self.uniqueUids[eUid].content_type = ctUid
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
}).catch(function (err) {
|
|
270
|
+
let error = JSON.parse(err.message)
|
|
271
|
+
addlogs(config, chalk.red('Error updating entry', JSON.stringify(error)), 'error')
|
|
272
|
+
self.fails.push({
|
|
273
|
+
content_type: ctUid,
|
|
274
|
+
locale: lang,
|
|
275
|
+
entry: entries[eUid],
|
|
276
|
+
error: error,
|
|
277
|
+
})
|
|
278
|
+
return err
|
|
279
|
+
})
|
|
280
|
+
}
|
|
281
|
+
delete requestObject.json.entry.publish_details
|
|
282
|
+
return client.stack({api_key: config.target_stack, management_token: config.management_token}).contentType(ctUid).entry().create(requestObject.json)
|
|
283
|
+
.then(async entryResponse => {
|
|
284
|
+
self.success[ctUid] = self.success[ctUid] || []
|
|
285
|
+
self.success[ctUid].push(entries[eUid])
|
|
286
|
+
if (!self.mappedUids.hasOwnProperty(eUid)) {
|
|
287
|
+
self.mappedUids[eUid] = entryResponse.uid
|
|
288
|
+
createdEntries = entryResponse
|
|
289
|
+
// if its a non-master language, i.e. the entry isn't present in the master language
|
|
290
|
+
if (lang !== masterLanguage) {
|
|
291
|
+
self.uniqueUids[eUid] = self.uniqueUids[eUid] || {}
|
|
292
|
+
if (self.uniqueUids[eUid].locales) {
|
|
293
|
+
self.uniqueUids[eUid].locales.push(lang)
|
|
266
294
|
} else {
|
|
267
|
-
|
|
295
|
+
self.uniqueUids[eUid].locales = [lang]
|
|
268
296
|
}
|
|
269
|
-
|
|
270
|
-
// .then(function () {
|
|
271
|
-
// })
|
|
297
|
+
self.uniqueUids[eUid].content_type = ctUid
|
|
272
298
|
}
|
|
273
299
|
}
|
|
274
|
-
|
|
275
|
-
}).catch(function (err) {
|
|
276
|
-
let error = JSON.parse(err.message)
|
|
300
|
+
}).catch(function (error) {
|
|
277
301
|
if (error.hasOwnProperty('error_code') && error.error_code === 119) {
|
|
278
302
|
if (error.errors.title) {
|
|
279
303
|
addlogs(config, 'Entry ' + eUid + ' already exist, skip to avoid creating a duplicate entry', 'error')
|
|
@@ -284,10 +308,10 @@ importEntries.prototype = {
|
|
|
284
308
|
content_type: ctUid,
|
|
285
309
|
locale: lang,
|
|
286
310
|
entry: entries[eUid],
|
|
287
|
-
error: error
|
|
311
|
+
error: error,
|
|
288
312
|
})
|
|
289
313
|
helper.writeFile(createdEntriesWOUidPath, self.createdEntriesWOUid)
|
|
290
|
-
return
|
|
314
|
+
return
|
|
291
315
|
}
|
|
292
316
|
// TODO: if status code: 422, check the reason
|
|
293
317
|
// 429 for rate limit
|
|
@@ -296,98 +320,40 @@ importEntries.prototype = {
|
|
|
296
320
|
content_type: ctUid,
|
|
297
321
|
locale: lang,
|
|
298
322
|
entry: entries[eUid],
|
|
299
|
-
error: error
|
|
323
|
+
error: error,
|
|
300
324
|
})
|
|
301
325
|
})
|
|
302
|
-
})
|
|
303
|
-
} else {
|
|
304
|
-
return client.stack({api_key: config.target_stack, management_token: config.management_token}).contentType(ctUid).entry().create(requestObject.json)
|
|
305
|
-
.then(async entryResponse => {
|
|
306
|
-
self.success[ctUid] = self.success[ctUid] || []
|
|
307
|
-
self.success[ctUid].push(entries[eUid])
|
|
308
|
-
if (!self.mappedUids.hasOwnProperty(eUid)) {
|
|
309
|
-
self.mappedUids[eUid] = entryResponse.uid
|
|
310
|
-
createdEntries = entryResponse
|
|
311
|
-
// if its a non-master language, i.e. the entry isn't present in the master language
|
|
312
|
-
if (lang !== masterLanguage) {
|
|
313
|
-
self.uniqueUids[eUid] = self.uniqueUids[eUid] || {}
|
|
314
|
-
if (self.uniqueUids[eUid].locales) {
|
|
315
|
-
self.uniqueUids[eUid].locales.push(lang)
|
|
316
|
-
} else {
|
|
317
|
-
self.uniqueUids[eUid].locales = [lang]
|
|
318
|
-
}
|
|
319
|
-
self.uniqueUids[eUid].content_type = ctUid
|
|
320
|
-
}
|
|
321
|
-
}
|
|
322
326
|
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
// })
|
|
333
|
-
}
|
|
334
|
-
}
|
|
335
|
-
return;
|
|
336
|
-
}).catch(function (error) {
|
|
337
|
-
// let error = JSON.parse(err.message)
|
|
338
|
-
if (error.hasOwnProperty('error_code') && error.error_code === 119) {
|
|
339
|
-
if (error.errors.title) {
|
|
340
|
-
addlogs(config, 'Entry ' + eUid + ' already exist, skip to avoid creating a duplicate entry', 'error')
|
|
341
|
-
} else {
|
|
342
|
-
addlogs(config, chalk.red('Error creating entry due to: ' + JSON.stringify(error)), 'error')
|
|
343
|
-
}
|
|
344
|
-
self.createdEntriesWOUid.push({
|
|
345
|
-
content_type: ctUid,
|
|
346
|
-
locale: lang,
|
|
347
|
-
entry: entries[eUid],
|
|
348
|
-
error: error
|
|
349
|
-
})
|
|
350
|
-
helper.writeFile(createdEntriesWOUidPath, self.createdEntriesWOUid)
|
|
351
|
-
return;
|
|
352
|
-
}
|
|
353
|
-
// TODO: if status code: 422, check the reason
|
|
354
|
-
// 429 for rate limit
|
|
355
|
-
addlogs(config, chalk.red('Error creating entry', JSON.stringify(error)), 'error')
|
|
356
|
-
self.fails.push({
|
|
357
|
-
content_type: ctUid,
|
|
358
|
-
locale: lang,
|
|
359
|
-
entry: entries[eUid],
|
|
360
|
-
error: error
|
|
361
|
-
})
|
|
327
|
+
// create/update 5 entries at a time
|
|
328
|
+
}, {
|
|
329
|
+
concurrency: 1,
|
|
330
|
+
}).then(function () {
|
|
331
|
+
helper.writeFile(successEntryLogPath, self.success[ctUid])
|
|
332
|
+
helper.writeFile(failedEntryLogPath, self.fails[ctUid])
|
|
333
|
+
helper.writeFile(entryUidMapperPath, self.mappedUids)
|
|
334
|
+
helper.writeFile(uniqueUidMapperPath, self.uniqueUids)
|
|
335
|
+
helper.writeFile(createdEntriesPath, createdEntries)
|
|
362
336
|
})
|
|
363
|
-
|
|
364
|
-
// create/update 5 entries at a time
|
|
337
|
+
// process one batch at a time
|
|
365
338
|
}, {
|
|
366
|
-
concurrency:
|
|
339
|
+
concurrency: 1,
|
|
367
340
|
}).then(function () {
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
341
|
+
if (self.success && self.success[ctUid] && self.success[ctUid].length > 0)
|
|
342
|
+
addlogs(config, self.success[ctUid].length + ' entries created successfully in ' + ctUid + ' content type in ' + lang +
|
|
343
|
+
' locale!', 'success')
|
|
344
|
+
if (self.fails && self.fails[ctUid] && self.fails[ctUid].length > 0)
|
|
345
|
+
addlogs(config, self.fails[ctUid].length + ' entries failed to create in ' + ctUid + ' content type in ' + lang +
|
|
346
|
+
' locale!', 'error')
|
|
347
|
+
self.success[ctUid] = []
|
|
348
|
+
self.fails[ctUid] = []
|
|
373
349
|
})
|
|
374
|
-
|
|
375
|
-
}, {
|
|
376
|
-
concurrency: reqConcurrency
|
|
377
|
-
}).then(function () {
|
|
378
|
-
addlogs(config, 'Entries created successfully in ' + ctUid + ' content type in ' + lang +
|
|
379
|
-
' locale!', 'success')
|
|
380
|
-
self.success[ctUid] = []
|
|
381
|
-
self.fails[ctUid] = []
|
|
382
|
-
|
|
383
|
-
})
|
|
350
|
+
}
|
|
384
351
|
} else {
|
|
385
|
-
addlogs(config, chalk.
|
|
386
|
-
|
|
387
|
-
|
|
352
|
+
addlogs(config, chalk.white('Unable to find entry file path for ' + ctUid + ' content type!\nThe file \'' +
|
|
353
|
+
eFilePath + '\' does not exist!'), 'error')
|
|
388
354
|
}
|
|
389
355
|
}, {
|
|
390
|
-
concurrency: reqConcurrency
|
|
356
|
+
concurrency: reqConcurrency,
|
|
391
357
|
}).then(function () {
|
|
392
358
|
addlogs(config, chalk.green('Entries created successfully in \'' + lang + '\' language'), 'success')
|
|
393
359
|
return resolve()
|
|
@@ -402,20 +368,19 @@ importEntries.prototype = {
|
|
|
402
368
|
return new Promise(function (resolve) {
|
|
403
369
|
self.createdEntriesWOUid = helper.readFile(createdEntriesWOUidPath)
|
|
404
370
|
self.failedWO = []
|
|
405
|
-
if (_.isArray(self.createdEntriesWOUid) && self.createdEntriesWOUid.length) {
|
|
371
|
+
if (_.isArray(self.createdEntriesWOUid) && self.createdEntriesWOUid.length > 0) {
|
|
406
372
|
return Promise.map(self.createdEntriesWOUid, function (entry) {
|
|
407
373
|
return self.fetchEntry(entry)
|
|
408
374
|
}, {
|
|
409
|
-
concurrency: reqConcurrency
|
|
375
|
+
concurrency: reqConcurrency,
|
|
410
376
|
}).then(function () {
|
|
411
377
|
helper.writeFile(failedWOPath, self.failedWO)
|
|
412
378
|
addlogs(config, 'Mapped entries without mapped uid successfully!', 'success')
|
|
413
379
|
return resolve()
|
|
414
380
|
})
|
|
415
|
-
} else {
|
|
416
|
-
addlogs(config, 'No entries without mapped uid found!', 'success')
|
|
417
|
-
return resolve()
|
|
418
381
|
}
|
|
382
|
+
addlogs(config, 'No entries without mapped uid found!', 'success')
|
|
383
|
+
return resolve()
|
|
419
384
|
})
|
|
420
385
|
},
|
|
421
386
|
repostEntries: function (lang) {
|
|
@@ -431,14 +396,14 @@ importEntries.prototype = {
|
|
|
431
396
|
|
|
432
397
|
if (!fs.existsSync(eSuccessFilePath)) {
|
|
433
398
|
addlogs(config, 'Success file was not found at: ' + eSuccessFilePath, 'success')
|
|
434
|
-
return
|
|
399
|
+
return
|
|
435
400
|
}
|
|
436
401
|
|
|
437
402
|
let entries = helper.readFile(eSuccessFilePath)
|
|
438
403
|
entries = entries || []
|
|
439
404
|
if (entries.length === 0) {
|
|
440
405
|
addlogs(config, 'No entries were created to be updated in \'' + lang + '\' language!', 'success')
|
|
441
|
-
return
|
|
406
|
+
return
|
|
442
407
|
}
|
|
443
408
|
|
|
444
409
|
// Keep track of entries that have their references updated
|
|
@@ -459,185 +424,181 @@ importEntries.prototype = {
|
|
|
459
424
|
let uid = entry.uid
|
|
460
425
|
let _entry = lookupReplaceEntries({
|
|
461
426
|
content_type: schema,
|
|
462
|
-
entry: entry
|
|
427
|
+
entry: entry,
|
|
463
428
|
}, _.clone(self.mappedUids), refUidMapperPath)
|
|
464
429
|
// if there's self references, the uid gets replaced
|
|
465
430
|
_entry.uid = uid
|
|
466
431
|
return _entry
|
|
467
432
|
} catch (error) {
|
|
468
433
|
console.error(error)
|
|
434
|
+
return error
|
|
469
435
|
}
|
|
470
436
|
})
|
|
471
437
|
|
|
472
438
|
// Run entry creation in batches of ~16~ entries
|
|
473
|
-
for (let i = 0; i < entries.length; i += entryBatchLimit) {
|
|
474
|
-
batches.push(entries.slice(i, i + entryBatchLimit))
|
|
439
|
+
for (let i = 0; i < entries.length; i += Math.round(entryBatchLimit / 3)) {
|
|
440
|
+
batches.push(entries.slice(i, i + Math.round(entryBatchLimit / 3)))
|
|
475
441
|
}
|
|
476
|
-
return Promise.map(batches, function (batch, index) {
|
|
477
|
-
return Promise.map(batch, function (entry) {
|
|
442
|
+
return Promise.map(batches, async function (batch, index) {
|
|
443
|
+
return Promise.map(batch, async function (entry) {
|
|
478
444
|
entry.uid = self.mappedUids[entry.uid]
|
|
479
445
|
if (refsUpdatedUids.indexOf(entry.uid) !== -1) {
|
|
480
446
|
addlogs(config, 'Entry: ' + entry.uid + ' in Content Type: ' + ctUid + ' in lang: ' +
|
|
481
|
-
|
|
482
|
-
return
|
|
447
|
+
lang + ' references fields are already updated.', 'success')
|
|
448
|
+
return
|
|
483
449
|
}
|
|
484
450
|
|
|
485
451
|
let requestObject = {
|
|
486
452
|
qs: {
|
|
487
|
-
locale: lang
|
|
453
|
+
locale: lang,
|
|
488
454
|
},
|
|
489
455
|
json: {
|
|
490
|
-
entry: entry
|
|
491
|
-
}
|
|
456
|
+
entry: entry,
|
|
457
|
+
},
|
|
492
458
|
}
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
Object.assign(entryResponse, _.cloneDeep(entry))
|
|
459
|
+
let promiseResult = new Promise((resolve, reject) => {
|
|
460
|
+
let entryResponse = client.stack({api_key: config.target_stack, management_token: config.management_token}).contentType(ctUid).entry(entry.uid)
|
|
461
|
+
Object.assign(entryResponse, entry)
|
|
497
462
|
delete entryResponse.publish_details
|
|
498
|
-
return entryResponse.update()
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
}
|
|
506
|
-
}
|
|
507
|
-
if (config.entriesPublish) {
|
|
508
|
-
if (entry.publish_details.length > 0) {
|
|
509
|
-
let entryUid = response
|
|
510
|
-
return self.publish(entryUid.uid, ctUid, lang, entry).then(function () {
|
|
511
|
-
return
|
|
512
|
-
})
|
|
463
|
+
return entryResponse.update({locale: lang})
|
|
464
|
+
.then(response => {
|
|
465
|
+
for (let j = 0; j < entries.length; j++) {
|
|
466
|
+
if (entries[j].uid === response.uid) {
|
|
467
|
+
entries[j] = response
|
|
468
|
+
break
|
|
469
|
+
}
|
|
513
470
|
}
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
})
|
|
517
|
-
.catch(function (error) {
|
|
518
|
-
addlogs(config, chalk.red('Entry Uid: ' + entry.uid + ' of Content Type: ' + ctUid +
|
|
519
|
-
' failed to update in locale: ' + lang), 'error')
|
|
520
|
-
|
|
521
|
-
addlogs(config, error, 'error')
|
|
522
|
-
refsUpdateFailed.push({
|
|
523
|
-
content_type: ctUid,
|
|
524
|
-
entry: entry,
|
|
525
|
-
locale: lang,
|
|
526
|
-
error: error
|
|
471
|
+
refsUpdatedUids.push(response.uid)
|
|
472
|
+
return resolve()
|
|
527
473
|
})
|
|
528
|
-
|
|
474
|
+
.catch(function (error) {
|
|
475
|
+
addlogs(config, chalk.red('Entry Uid: ' + entry.uid + ' of Content Type: ' + ctUid +
|
|
476
|
+
' failed to update in locale: ' + lang), 'error')
|
|
477
|
+
|
|
478
|
+
addlogs(config, error, 'error')
|
|
479
|
+
refsUpdateFailed.push({
|
|
480
|
+
content_type: ctUid,
|
|
481
|
+
entry: entry,
|
|
482
|
+
locale: lang,
|
|
483
|
+
error: error,
|
|
484
|
+
})
|
|
485
|
+
return reject()
|
|
486
|
+
})
|
|
487
|
+
})
|
|
488
|
+
await promiseResult
|
|
529
489
|
}, {
|
|
530
|
-
concurrency: reqConcurrency
|
|
490
|
+
concurrency: reqConcurrency,
|
|
531
491
|
}).then(function () {
|
|
532
492
|
// batch completed successfully
|
|
533
493
|
helper.writeFile(path.join(eFolderPath, 'success.json'), entries)
|
|
534
494
|
helper.writeFile(path.join(eFolderPath, 'refsUpdatedUids.json'), refsUpdatedUids)
|
|
535
495
|
helper.writeFile(path.join(eFolderPath, 'refsUpdateFailed.json'), refsUpdateFailed)
|
|
536
|
-
addlogs(config, 'Completed batch no: ' + (index + 1) + ' successfully!', 'success')
|
|
537
|
-
|
|
496
|
+
addlogs(config, 'Completed re-post entries batch no: ' + (index + 1) + ' successfully!', 'success')
|
|
538
497
|
}).catch(function (error) {
|
|
539
498
|
// error while executing entry in batch
|
|
540
|
-
addlogs(config, chalk.red('Failed at batch no: ' + (index + 1)), 'error')
|
|
499
|
+
addlogs(config, chalk.red('Failed re-post entries at batch no: ' + (index + 1)), 'error')
|
|
541
500
|
throw error
|
|
542
501
|
})
|
|
543
502
|
}, {
|
|
544
|
-
concurrency: reqConcurrency
|
|
503
|
+
concurrency: reqConcurrency,
|
|
545
504
|
}).then(function () {
|
|
546
505
|
// finished updating entries with references
|
|
547
506
|
addlogs(config, 'Imported entries of Content Type: \'' + ctUid + '\' in language: \'' + lang +
|
|
548
|
-
|
|
549
|
-
|
|
507
|
+
'\' successfully!', 'success')
|
|
550
508
|
}).catch(function (error) {
|
|
551
509
|
// error while updating entries with references
|
|
552
510
|
addlogs(config, chalk.red('Failed while importing entries of Content Type: \'' + ctUid + '\' in language: \'' +
|
|
553
|
-
|
|
511
|
+
lang + '\' successfully!'), 'error')
|
|
554
512
|
throw error
|
|
555
513
|
})
|
|
556
514
|
}, {
|
|
557
|
-
concurrency: reqConcurrency
|
|
515
|
+
concurrency: reqConcurrency,
|
|
558
516
|
}).then(function () {
|
|
559
517
|
// completed updating entry references
|
|
560
518
|
addlogs(config, chalk.green('Imported entries in \'' + lang + '\' language successfully!'), 'success')
|
|
561
519
|
return resolve()
|
|
562
520
|
}).catch(function (error) {
|
|
563
521
|
// error while updating entry references
|
|
564
|
-
addlogs(config, chalk.red('Failed to
|
|
522
|
+
addlogs(config, chalk.red('Failed to re post entries in ' + lang + ' language'), 'error')
|
|
565
523
|
return reject(error)
|
|
566
524
|
})
|
|
567
525
|
})
|
|
568
526
|
},
|
|
569
|
-
supressFields: function () {
|
|
527
|
+
supressFields: async function () {
|
|
528
|
+
addlogs(config, chalk.white('Suppressing content type fields...'), 'success')
|
|
570
529
|
let self = this
|
|
571
530
|
return new Promise(function (resolve, reject) {
|
|
572
531
|
let modifiedSchemas = []
|
|
573
|
-
let
|
|
532
|
+
let suppressedSchemas = []
|
|
574
533
|
|
|
575
534
|
for (let uid in self.ctSchemas) {
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
// check if supress modified flag
|
|
586
|
-
if (flag.supressed) {
|
|
587
|
-
supressedSchemas.push(contentTypeSchema)
|
|
588
|
-
modifiedSchemas.push(self.ctSchemas[uid])
|
|
589
|
-
}
|
|
535
|
+
if (uid) {
|
|
536
|
+
let contentTypeSchema = _.cloneDeep(self.ctSchemas[uid])
|
|
537
|
+
let flag = {
|
|
538
|
+
suppressed: false,
|
|
539
|
+
references: false,
|
|
540
|
+
}
|
|
541
|
+
if (contentTypeSchema.field_rules) {
|
|
542
|
+
delete contentTypeSchema.field_rules
|
|
543
|
+
}
|
|
590
544
|
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
545
|
+
// Set mandatory or unique flag to false
|
|
546
|
+
suppress(contentTypeSchema.schema, flag)
|
|
547
|
+
// Check if suppress modified flag
|
|
548
|
+
if (flag.suppressed) {
|
|
549
|
+
suppressedSchemas.push(contentTypeSchema)
|
|
550
|
+
modifiedSchemas.push(self.ctSchemas[uid])
|
|
551
|
+
}
|
|
552
|
+
|
|
553
|
+
if (flag.references) {
|
|
554
|
+
self.refSchemas.push(uid)
|
|
555
|
+
}
|
|
594
556
|
|
|
595
|
-
|
|
557
|
+
// Replace extensions with new UID
|
|
558
|
+
extension_suppress(contentTypeSchema.schema, config.preserveStackVersion)
|
|
559
|
+
}
|
|
596
560
|
}
|
|
597
561
|
|
|
562
|
+
// write modified schema in backup file
|
|
598
563
|
helper.writeFile(modifiedSchemaPath, modifiedSchemas)
|
|
599
564
|
|
|
600
|
-
return Promise.map(
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
.
|
|
604
|
-
Object.assign(contentTypeResponse, _.cloneDeep(schema))
|
|
605
|
-
return contentTypeResponse.update()
|
|
606
|
-
})
|
|
565
|
+
return Promise.map(suppressedSchemas, async function (schema) {
|
|
566
|
+
let contentTypeResponse = client.stack({api_key: config.target_stack, management_token: config.management_token}).contentType(schema.uid)
|
|
567
|
+
Object.assign(contentTypeResponse, _.cloneDeep(schema))
|
|
568
|
+
return contentTypeResponse.update()
|
|
607
569
|
.then(UpdatedcontentType => {
|
|
608
|
-
|
|
570
|
+
|
|
609
571
|
}).catch(function (error) {
|
|
610
572
|
addlogs(config, chalk.red('Failed to modify mandatory field of \'' + schema.uid + '\' content type'), 'error')
|
|
611
|
-
throw error
|
|
612
573
|
})
|
|
613
574
|
// update 5 content types at a time
|
|
614
575
|
}, {
|
|
615
|
-
|
|
576
|
+
// update reqConcurrency content types at a time
|
|
577
|
+
concurrency: reqConcurrency,
|
|
616
578
|
}).then(function () {
|
|
617
579
|
return resolve()
|
|
618
580
|
}).catch(function (error) {
|
|
619
|
-
addlogs(config, chalk.red('Error while
|
|
581
|
+
addlogs(config, chalk.red('Error while suppressing mandatory field schemas'), 'error')
|
|
620
582
|
return reject(error)
|
|
621
583
|
})
|
|
622
584
|
})
|
|
623
585
|
},
|
|
624
586
|
fetchEntry: function (query) {
|
|
625
587
|
let self = this
|
|
626
|
-
return new Promise(function (resolve) {
|
|
588
|
+
return new Promise(function (resolve, reject) {
|
|
627
589
|
let requestObject = {
|
|
628
590
|
qs: {
|
|
629
591
|
query: {
|
|
630
|
-
title: query.entry.title
|
|
592
|
+
title: query.entry.title,
|
|
631
593
|
},
|
|
632
|
-
locale: query.locale
|
|
633
|
-
}
|
|
594
|
+
locale: query.locale,
|
|
595
|
+
},
|
|
634
596
|
}
|
|
635
597
|
|
|
636
|
-
return client.stack({
|
|
598
|
+
return client.stack({api_key: config.target_stack, management_token: config.management_token}).contentType(query.content_type).entry().query(requestObject.qs).find()
|
|
637
599
|
.then(function (response) {
|
|
638
|
-
if (
|
|
600
|
+
if (response.body.entries.length <= 0) {
|
|
639
601
|
addlogs(config, 'Unable to map entry WO uid: ' + query.entry.uid, 'error')
|
|
640
|
-
// log.debug('Request:\n' + JSON.stringify(requestObject))
|
|
641
602
|
self.failedWO.push(query)
|
|
642
603
|
return resolve()
|
|
643
604
|
}
|
|
@@ -646,46 +607,53 @@ importEntries.prototype = {
|
|
|
646
607
|
let entries = helper.readFile(_ePath)
|
|
647
608
|
entries.push(query.entry)
|
|
648
609
|
helper.writeFile(_ePath, entries)
|
|
649
|
-
addlogs(config, 'Completed mapping entry wo uid: ' + query.entry.uid + ': ' + response.body.entries[0].uid, '
|
|
610
|
+
addlogs(config, 'Completed mapping entry wo uid: ' + query.entry.uid + ': ' + response.body.entries[0].uid, 'clientsuccess')
|
|
650
611
|
return resolve()
|
|
651
|
-
}).catch(function () {
|
|
612
|
+
}).catch(function (error) {
|
|
652
613
|
return resolve()
|
|
653
614
|
})
|
|
654
615
|
})
|
|
655
616
|
},
|
|
656
|
-
|
|
617
|
+
unSuppressFields: function () {
|
|
657
618
|
let self = this
|
|
658
|
-
return new Promise(
|
|
619
|
+
return new Promise(function (resolve, reject) {
|
|
659
620
|
let modifiedSchemas = helper.readFile(modifiedSchemaPath)
|
|
660
621
|
let modifiedSchemasUids = []
|
|
661
622
|
let updatedExtensionUidsSchemas = []
|
|
662
623
|
for (let uid in modifiedSchemas) {
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
624
|
+
if (uid) {
|
|
625
|
+
let _contentTypeSchema = _.cloneDeep(modifiedSchemas[uid])
|
|
626
|
+
if (_contentTypeSchema.field_rules) {
|
|
627
|
+
delete _contentTypeSchema.field_rules
|
|
628
|
+
}
|
|
629
|
+
extension_suppress(_contentTypeSchema.schema, config.preserveStackVersion)
|
|
630
|
+
updatedExtensionUidsSchemas.push(_contentTypeSchema)
|
|
666
631
|
}
|
|
667
|
-
extension_supress(_contentTypeSchema.schema, config.preserveStackVersion)
|
|
668
|
-
updatedExtensionUidsSchemas.push(_contentTypeSchema)
|
|
669
632
|
}
|
|
670
633
|
|
|
671
|
-
return Promise.map(updatedExtensionUidsSchemas, function (schema) {
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
contentTypeResponse
|
|
634
|
+
return Promise.map(updatedExtensionUidsSchemas, async function (schema) {
|
|
635
|
+
let promise = new Promise((resolve, reject) => {
|
|
636
|
+
client.stack({api_key: config.target_stack, management_token: config.management_token}).contentType(schema.uid).fetch()
|
|
637
|
+
.then(contentTypeResponse => {
|
|
638
|
+
contentTypeResponse.schema = schema.schema
|
|
675
639
|
contentTypeResponse.update()
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
640
|
+
.then(UpdatedcontentType => {
|
|
641
|
+
modifiedSchemasUids.push(schema.uid)
|
|
642
|
+
addlogs(config, (chalk.white('Content type: \'' + schema.uid + '\' has been restored to its previous glory!')))
|
|
643
|
+
return resolve()
|
|
644
|
+
}).catch(function (error) {
|
|
645
|
+
addlogs(config, chalk.red('Failed to re-update ' + schema.uid), 'error')
|
|
646
|
+
addlogs(config, error, 'error')
|
|
647
|
+
return reject(error)
|
|
648
|
+
})
|
|
680
649
|
}).catch(function (error) {
|
|
681
|
-
addlogs(config, chalk.red('Failed to re-update ' + schema.uid), 'error')
|
|
682
650
|
addlogs(config, error, 'error')
|
|
651
|
+
return reject(error)
|
|
683
652
|
})
|
|
684
|
-
}).catch(function (error) {
|
|
685
|
-
addlogs(config, error, 'error')
|
|
686
653
|
})
|
|
654
|
+
await promise
|
|
687
655
|
}, {
|
|
688
|
-
concurrency: reqConcurrency
|
|
656
|
+
concurrency: reqConcurrency,
|
|
689
657
|
}).then(function () {
|
|
690
658
|
for (let i = 0; i < modifiedSchemas.length; i++) {
|
|
691
659
|
if (modifiedSchemasUids.indexOf(modifiedSchemas[i].uid) !== -1) {
|
|
@@ -713,39 +681,25 @@ importEntries.prototype = {
|
|
|
713
681
|
if (entries[uid].locales.indexOf(masterLanguage.code) === -1) {
|
|
714
682
|
bugged.push({
|
|
715
683
|
content_type: entries[uid].content_type,
|
|
716
|
-
uid: uid
|
|
684
|
+
uid: uid,
|
|
717
685
|
})
|
|
718
686
|
}
|
|
719
687
|
}
|
|
720
688
|
|
|
721
689
|
return Promise.map(bugged, function (entry) {
|
|
722
|
-
|
|
723
|
-
// uri: self.requestOptionTemplate.uri + entry.content_type + config.apis.entries + self.mappedUids[
|
|
724
|
-
// entry.uid],
|
|
725
|
-
// method: 'DELETE',
|
|
726
|
-
// qs: {
|
|
727
|
-
// locale: masterLanguage.code
|
|
728
|
-
// },
|
|
729
|
-
// headers: self.requestOptionTemplate.headers,
|
|
730
|
-
// json: true
|
|
731
|
-
// }
|
|
732
|
-
|
|
733
|
-
return client.stack({api_key: config.source_stack, management_token: config.management_token}).contentType(entry.content_type).entry(self.mappedUids[entry.uid]).delete({locale: masterLanguage.code})
|
|
690
|
+
return client.stack({api_key: config.target_stack, management_token: config.management_token}).contentType(entry.content_type).entry(self.mappedUids[entry.uid]).delete({locale: masterLanguage.code})
|
|
734
691
|
.then(function () {
|
|
735
692
|
removed.push(self.mappedUids[entry.uid])
|
|
736
|
-
addlogs(config, 'Removed bugged entry from master ' + JSON.stringify(entry), 'success')
|
|
693
|
+
addlogs(config, 'Removed bugged entry from master ' + JSON.stringify(entry), 'success')
|
|
737
694
|
})
|
|
738
695
|
.catch(function (error) {
|
|
739
696
|
addlogs(config, chalk.red('Failed to remove bugged entry from master language'), 'error')
|
|
740
697
|
addlogs(config, error, 'error')
|
|
741
698
|
addlogs(config, JSON.stringify(entry), 'error')
|
|
742
|
-
|
|
743
699
|
})
|
|
744
|
-
|
|
745
700
|
}, {
|
|
746
|
-
concurrency: reqConcurrency
|
|
701
|
+
concurrency: reqConcurrency,
|
|
747
702
|
}).then(function () {
|
|
748
|
-
|
|
749
703
|
for (let i = 0; i < bugged.length; i++) {
|
|
750
704
|
if (removed.indexOf(bugged[i].uid) !== -1) {
|
|
751
705
|
bugged.splice(i, 1)
|
|
@@ -768,72 +722,145 @@ importEntries.prototype = {
|
|
|
768
722
|
let self = this
|
|
769
723
|
return new Promise(function (resolve, reject) {
|
|
770
724
|
if (schema.field_rules) {
|
|
771
|
-
let
|
|
772
|
-
for (let k = 0; k <
|
|
773
|
-
|
|
725
|
+
let fieldRuleLength = schema.field_rules.length
|
|
726
|
+
for (let k = 0; k < fieldRuleLength; k++) {
|
|
727
|
+
let fieldRuleConditionLength = schema.field_rules[k].conditions.length
|
|
728
|
+
for (let i = 0; i < fieldRuleConditionLength; i++) {
|
|
774
729
|
if (schema.field_rules[k].conditions[i].operand_field === 'reference') {
|
|
775
|
-
let
|
|
776
|
-
|
|
777
|
-
let
|
|
778
|
-
for (let j = 0; j <
|
|
779
|
-
let
|
|
780
|
-
let
|
|
781
|
-
if (
|
|
782
|
-
|
|
730
|
+
let fieldRulesValue = schema.field_rules[k].conditions[i].value
|
|
731
|
+
let fieldRulesArray = fieldRulesValue.split('.')
|
|
732
|
+
let updatedValue = []
|
|
733
|
+
for (let j = 0; j < fieldRulesArray.length; j++) {
|
|
734
|
+
let splitedFieldRulesValue = fieldRulesArray[j]
|
|
735
|
+
let oldUid = helper.readFile(path.join(entryUidMapperPath))
|
|
736
|
+
if (oldUid.hasOwnProperty(splitedFieldRulesValue)) {
|
|
737
|
+
updatedValue.push(oldUid[splitedFieldRulesValue])
|
|
783
738
|
} else {
|
|
784
|
-
|
|
739
|
+
updatedValue.push(fieldRulesArray[j])
|
|
785
740
|
}
|
|
786
741
|
}
|
|
787
|
-
|
|
788
|
-
schema.field_rules[k].conditions[i]['value'] = append_all_values
|
|
742
|
+
schema.field_rules[k].conditions[i].value = updatedValue.join('.')
|
|
789
743
|
}
|
|
790
744
|
}
|
|
791
745
|
}
|
|
792
746
|
} else {
|
|
793
|
-
|
|
747
|
+
addlogs(config, 'field_rules is not available...', 'error')
|
|
794
748
|
}
|
|
795
749
|
|
|
796
|
-
|
|
797
|
-
.then(contentTypeResponse => {
|
|
798
|
-
|
|
750
|
+
client.stack({ api_key: config.target_stack, management_token: config.management_token }).contentType(schema.uid).fetch()
|
|
751
|
+
.then((contentTypeResponse) => {
|
|
752
|
+
// Object.assign(ctObj, _.cloneDeep(schema))
|
|
753
|
+
contentTypeResponse.field_rules = schema.field_rules
|
|
799
754
|
contentTypeResponse.update()
|
|
800
|
-
|
|
755
|
+
})
|
|
756
|
+
.then(() => {
|
|
757
|
+
return resolve()
|
|
801
758
|
}).catch(function (error) {
|
|
759
|
+
return reject(error)
|
|
802
760
|
})
|
|
803
761
|
})
|
|
804
762
|
},
|
|
805
|
-
publish: function (
|
|
763
|
+
publish: function (langs) {
|
|
806
764
|
let self = this
|
|
807
|
-
let envId = []
|
|
808
|
-
let locales = []
|
|
809
765
|
let requestObject = {
|
|
810
|
-
|
|
766
|
+
entry: {},
|
|
811
767
|
}
|
|
812
|
-
|
|
768
|
+
|
|
769
|
+
let contentTypeUids = Object.keys(self.ctSchemas)
|
|
770
|
+
let entryMapper = helper.readFile(entryUidMapperPath)
|
|
771
|
+
|
|
813
772
|
return new Promise(function (resolve, reject) {
|
|
814
|
-
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
let
|
|
818
|
-
|
|
819
|
-
|
|
773
|
+
return Promise.map(langs, function (_lang, counter) {
|
|
774
|
+
let lang = langs[counter]
|
|
775
|
+
return Promise.map(contentTypeUids, function (ctUid) {
|
|
776
|
+
let eFilePath = path.resolve(ePath, ctUid, lang + '.json')
|
|
777
|
+
let entries = helper.readFile(eFilePath)
|
|
778
|
+
|
|
779
|
+
let eUids = Object.keys(entries)
|
|
780
|
+
let batches = []
|
|
781
|
+
|
|
782
|
+
if (eUids.length > 0) {
|
|
783
|
+
for (let i = 0; i < eUids.length; i += entryBatchLimit) {
|
|
784
|
+
batches.push(eUids.slice(i, i + entryBatchLimit))
|
|
785
|
+
}
|
|
786
|
+
} else {
|
|
787
|
+
return
|
|
820
788
|
}
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
|
|
789
|
+
|
|
790
|
+
return Promise.map(batches, async function (batch) {
|
|
791
|
+
return Promise.map(batch, async function (eUid) {
|
|
792
|
+
let entry = entries[eUid]
|
|
793
|
+
let envId = []
|
|
794
|
+
let locales = []
|
|
795
|
+
if (entry.publish_details && entry.publish_details.length > 0) {
|
|
796
|
+
_.forEach(entries[eUid].publish_details, function (pubObject) {
|
|
797
|
+
if (self.environment.hasOwnProperty(pubObject.environment) && _.indexOf(envId, self.environment[pubObject.environment].name) === -1) {
|
|
798
|
+
envId.push(self.environment[pubObject.environment].name)
|
|
799
|
+
}
|
|
800
|
+
if (pubObject.locale) {
|
|
801
|
+
let idx = _.indexOf(locales, pubObject.locale)
|
|
802
|
+
if (idx === -1) {
|
|
803
|
+
locales.push(pubObject.locale)
|
|
804
|
+
}
|
|
805
|
+
}
|
|
806
|
+
})
|
|
807
|
+
|
|
808
|
+
let entryUid = entryMapper[eUid]
|
|
809
|
+
if (entryUid) {
|
|
810
|
+
requestObject.entry.environments = envId
|
|
811
|
+
requestObject.entry.locales = locales
|
|
812
|
+
let publishPromiseResult = new Promise((resolve, reject) => {
|
|
813
|
+
client.stack({api_key: config.target_stack, management_token: config.management_token}).contentType(ctUid).entry(entryUid).publish({publishDetails: requestObject.entry, locale: lang})
|
|
814
|
+
// eslint-disable-next-line max-nested-callbacks
|
|
815
|
+
.then(result => {
|
|
816
|
+
// addlogs(config, 'Entry ' + eUid + ' published successfully in ' + ctUid + ' content type', 'success')
|
|
817
|
+
console.log('Entry ' + eUid + ' published successfully in ' + ctUid + ' content type');
|
|
818
|
+
return resolve(result)
|
|
819
|
+
// eslint-disable-next-line max-nested-callbacks
|
|
820
|
+
}).catch(function (err) {
|
|
821
|
+
// addlogs(config, 'Entry ' + eUid + ' not published successfully in ' + ctUid + ' content type', 'error')
|
|
822
|
+
console.log('Entry ' + eUid + ' not published successfully in ' + ctUid + ' content type')
|
|
823
|
+
return reject(err.errorMessage)
|
|
824
|
+
})
|
|
825
|
+
})
|
|
826
|
+
return publishPromiseResult
|
|
827
|
+
}
|
|
828
|
+
} else {
|
|
829
|
+
return {}
|
|
830
|
+
}
|
|
831
|
+
}, {
|
|
832
|
+
concurrency: reqConcurrency,
|
|
833
|
+
}).then(function () {
|
|
834
|
+
}).catch(function (error) {
|
|
835
|
+
// error while executing entry in batch
|
|
836
|
+
addlogs(config, error, 'error')
|
|
837
|
+
return error
|
|
838
|
+
})
|
|
839
|
+
}, {
|
|
840
|
+
concurrency: 1,
|
|
841
|
+
}).then(function () {
|
|
842
|
+
// addlogs(config, 'Entries published successfully in ' + ctUid + ' content type', 'success')
|
|
843
|
+
console.log('Entries published successfully in ' + ctUid + ' content type')
|
|
844
|
+
}).catch(function (error) {
|
|
845
|
+
addlogs(config, 'Failed some of the Entry publishing in ' + ctUid + ' content type, go through logs for details.', 'error')
|
|
846
|
+
return error
|
|
847
|
+
})
|
|
848
|
+
}, {
|
|
849
|
+
concurrency: 1,
|
|
850
|
+
}).then(function () {
|
|
851
|
+
|
|
852
|
+
}).catch(function (error) {
|
|
853
|
+
return error
|
|
854
|
+
})
|
|
855
|
+
}, {
|
|
856
|
+
concurrency: 1,
|
|
857
|
+
}).then(function () {
|
|
829
858
|
return resolve()
|
|
830
|
-
})
|
|
831
|
-
.catch(function (err) {
|
|
832
|
-
let error = JSON.parse(err.message)
|
|
859
|
+
}).catch(error => {
|
|
833
860
|
return reject(error)
|
|
834
861
|
})
|
|
835
862
|
})
|
|
836
863
|
},
|
|
837
864
|
}
|
|
838
865
|
|
|
839
|
-
module.exports = new importEntries()
|
|
866
|
+
module.exports = new importEntries()
|