@contentstack/cli-cm-import 1.0.1 → 1.1.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.
@@ -10,9 +10,12 @@ let _ = require('lodash');
10
10
  let { marked } = require('marked');
11
11
 
12
12
  let helper = require('./fs');
13
+ const { getConfig } = require('./');
14
+ let config = getConfig();
15
+ const marketplaceAppPath = path.resolve(config.data, 'marketplace_apps', 'marketplace_apps.json');
13
16
 
14
17
  // get assets object
15
- module.exports = function (data, mappedAssetUids, mappedAssetUrls, assetUidMapperPath) {
18
+ module.exports = function (data, mappedAssetUids, mappedAssetUrls, assetUidMapperPath, installedExtensions) {
16
19
  if (
17
20
  !_.has(data, 'entry') ||
18
21
  !_.has(data, 'content_type') ||
@@ -65,10 +68,65 @@ module.exports = function (data, mappedAssetUids, mappedAssetUrls, assetUidMappe
65
68
  findAssetIdsFromJsonRte(data.entry, data.content_type.schema);
66
69
  // maybe only one of these checks would be enough
67
70
  parent.pop();
71
+ } else if (
72
+ schema[i].data_type === 'json' &&
73
+ schema[i].field_metadata.extension &&
74
+ schema[i].field_metadata.is_asset
75
+ ) {
76
+ findAssetIdsFromJsonCustomFields(data.entry, data.content_type.schema);
77
+ } else if (schema[i].data_type === 'json' && schema[i].field_metadata.extension) {
78
+ if (installedExtensions) {
79
+ const marketplaceApps = helper.readFile(marketplaceAppPath);
80
+ const oldExt = _.find(marketplaceApps, { uid: schema[i].extension_uid });
81
+
82
+ if (oldExt) {
83
+ const ext = _.find(installedExtensions, { type: oldExt.type, title: oldExt.title, app_uid: oldExt.app_uid })
84
+
85
+ if (ext) {
86
+ schema[i].extension_uid = ext.uid;
87
+ }
88
+ }
89
+ }
68
90
  }
69
91
  }
70
92
  };
71
93
 
94
+ function findAssetIdsFromJsonCustomFields(entryObj, ctSchema) {
95
+ ctSchema.map((row) => {
96
+ if (row.data_type === 'json') {
97
+ if (entryObj[row.uid] && row.field_metadata.extension && row.field_metadata.is_asset) {
98
+ if (installedExtensions) {
99
+ const marketplaceApps = helper.readFile(marketplaceAppPath);
100
+ const oldExt = _.find(marketplaceApps, { uid: row.extension_uid });
101
+
102
+ if (oldExt) {
103
+ const ext = _.find(installedExtensions, { type: oldExt.type, title: oldExt.title, app_uid: oldExt.app_uid })
104
+
105
+ if (ext) {
106
+ row.extension_uid = ext.uid;
107
+ }
108
+ }
109
+ }
110
+
111
+ if (entryObj[row.uid].metadata && entryObj[row.uid].metadata.extension_uid) {
112
+ const marketplaceApps = helper.readFile(marketplaceAppPath);
113
+ const oldExt = _.find(marketplaceApps, { uid: entryObj[row.uid].metadata.extension_uid });
114
+
115
+ if (oldExt) {
116
+ const ext = _.find(installedExtensions, { type: oldExt.type, title: oldExt.title, app_uid: oldExt.app_uid })
117
+
118
+ if (ext) {
119
+ entryObj[row.uid].metadata.extension_uid = ext.uid;
120
+ }
121
+ }
122
+ }
123
+ }
124
+ }
125
+
126
+ return row;
127
+ });
128
+ }
129
+
72
130
  function findAssetIdsFromJsonRte(entryObj, ctSchema) {
73
131
  for (const element of ctSchema) {
74
132
  switch (element.data_type) {
@@ -154,7 +212,7 @@ module.exports = function (data, mappedAssetUids, mappedAssetUrls, assetUidMappe
154
212
  }
155
213
 
156
214
  find(data.content_type.schema, data.entry);
157
- updateFileFields(data.entry, data, null, mappedAssetUids, matchedUids, unmatchedUids);
215
+ updateFileFields(data.entry, data, null, mappedAssetUids, matchedUids, unmatchedUids, mappedAssetUrls);
158
216
  assetUids = _.uniq(assetUids);
159
217
  assetUrls = _.uniq(assetUrls);
160
218
  let entry = JSON.stringify(data.entry);
@@ -277,16 +335,49 @@ function findFileUrls(schema, _entry, assetUrls) {
277
335
  }
278
336
  }
279
337
 
280
- function updateFileFields(objekt, parent, pos, mappedAssetUids, matchedUids, unmatchedUids) {
338
+ function updateFileFields(objekt, parent, pos, mappedAssetUids, matchedUids, unmatchedUids, mappedAssetUrls) {
281
339
  if (_.isPlainObject(objekt) && _.has(objekt, 'filename') && _.has(objekt, 'uid')) {
282
340
  if (typeof pos !== 'undefined') {
283
341
  if (typeof pos === 'number' || typeof pos === 'string') {
284
- if (mappedAssetUids.hasOwnProperty(objekt.uid)) {
285
- parent[pos] = mappedAssetUids[objekt.uid];
286
- matchedUids.push(objekt.uid);
342
+ const replacer = () => {
343
+ if (mappedAssetUids.hasOwnProperty(objekt.uid)) {
344
+ parent[pos] = mappedAssetUids[objekt.uid];
345
+ matchedUids.push(objekt.uid);
346
+ } else {
347
+ parent[pos] = '';
348
+ unmatchedUids.push(objekt.uid);
349
+ }
350
+ };
351
+
352
+ if (parent.uid && mappedAssetUids[parent.uid]) {
353
+ parent.uid = mappedAssetUids[parent.uid];
354
+ }
355
+
356
+ if (
357
+ objekt &&
358
+ _.isObject(parent[pos]) &&
359
+ parent[pos].uid &&
360
+ parent[pos].url &&
361
+ _.has(parent, 'asset') &&
362
+ _.has(parent, '_content_type_uid') &&
363
+ parent._content_type_uid === 'sys_assets'
364
+ ) {
365
+ if (
366
+ _.has(parent, 'asset') &&
367
+ _.has(parent, '_content_type_uid') &&
368
+ parent._content_type_uid === 'sys_assets'
369
+ ) {
370
+ parent = _.omit(parent, ['asset']);
371
+ }
372
+
373
+ if (objekt.uid && mappedAssetUids && mappedAssetUids[objekt.uid]) {
374
+ objekt.uid = mappedAssetUids[objekt.uid];
375
+ }
376
+ if (objekt.url && mappedAssetUrls && mappedAssetUrls[objekt.url]) {
377
+ objekt.url = mappedAssetUrls[objekt.url];
378
+ }
287
379
  } else {
288
- parent[pos] = '';
289
- unmatchedUids.push(objekt.uid);
380
+ replacer();
290
381
  }
291
382
  }
292
383
  }
@@ -5,21 +5,21 @@
5
5
  */
6
6
 
7
7
  // eslint-disable-next-line unicorn/filename-case
8
- var path = require('path');
9
- var _ = require('lodash');
8
+ const path = require('path');
9
+ const _ = require('lodash');
10
10
 
11
- var util = require('.');
12
- var helper = require('./fs');
13
- var config = util.getConfig();
11
+ const util = require('.');
12
+ const helper = require('./fs');
13
+ const config = util.getConfig();
14
14
  // update references in entry object
15
15
  module.exports = function (data, mappedUids, uidMapperPath) {
16
- var parent = [];
17
- var uids = [];
18
- var unmapped = [];
19
- var mapped = [];
16
+ let parent = [];
17
+ let uids = [];
18
+ let unmapped = [];
19
+ let mapped = [];
20
20
 
21
- var isNewRefFields = false;
22
- var preserveStackVersion = config.preserveStackVersion;
21
+ let isNewRefFields = false;
22
+ let preserveStackVersion = config.preserveStackVersion;
23
23
 
24
24
  function gatherJsonRteEntryIds(jsonRteData) {
25
25
  jsonRteData.children.forEach(element => {
@@ -48,10 +48,11 @@ module.exports = function (data, mappedUids, uidMapperPath) {
48
48
  })
49
49
  }
50
50
 
51
- var update = function (_parent, form_id, updateEntry) {
52
- var _entry = updateEntry
53
- var len = _parent.length
54
- for (var j = 0; j < len; j++) {
51
+ const update = function (_parent, form_id, updateEntry) {
52
+ let _entry = updateEntry
53
+ let len = _parent.length
54
+
55
+ for (let j = 0; j < len; j++) {
55
56
  if (_entry && _parent[j]) {
56
57
  if (j === len - 1 && _entry[_parent[j]]) {
57
58
  if (form_id !== '_assets') {
@@ -71,9 +72,9 @@ module.exports = function (data, mappedUids, uidMapperPath) {
71
72
  });
72
73
  }
73
74
  } else if (Array.isArray(_entry[_parent[j]])) {
74
- for (var k = 0; k < _entry[_parent[j]].length; k++) {
75
- if (_entry[_parent[j]][k].uid.length) {
76
- uids.push(_entry[_parent[j]][k].uid);
75
+ for (const element of _entry[_parent[j]]) {
76
+ if (element.uid.length) {
77
+ uids.push(element.uid);
77
78
  }
78
79
  }
79
80
  } else if (_entry[_parent[j]].uid.length) {
@@ -81,9 +82,9 @@ module.exports = function (data, mappedUids, uidMapperPath) {
81
82
  }
82
83
  } else {
83
84
  _entry = _entry[_parent[j]];
84
- var _keys = _.clone(_parent).splice((j+1), len);
85
+ let _keys = _.clone(_parent).splice((j+1), len);
85
86
  if (Array.isArray(_entry)) {
86
- for (var i = 0, _i = _entry.length; i < _i; i++) {
87
+ for (let i = 0, _i = _entry.length; i < _i; i++) {
87
88
  update(_keys, form_id, _entry[i]);
88
89
  }
89
90
  } else if (!(_entry instanceof Object)) {
@@ -93,8 +94,8 @@ module.exports = function (data, mappedUids, uidMapperPath) {
93
94
  }
94
95
  }
95
96
  };
96
- var find = function (schema, _entry) {
97
- for (var i = 0, _i = schema.length; i < _i; i++) {
97
+ const find = function (schema, _entry) {
98
+ for (let i = 0, _i = schema.length; i < _i; i++) {
98
99
  switch (schema[i].data_type) {
99
100
  case 'reference':
100
101
  if (Array.isArray(schema[i].reference_to)) {
@@ -117,7 +118,7 @@ module.exports = function (data, mappedUids, uidMapperPath) {
117
118
  parent.pop()
118
119
  break
119
120
  case 'blocks':
120
- for (var j = 0, _j = schema[i].blocks.length; j < _j; j++) {
121
+ for (let j = 0, _j = schema[i].blocks.length; j < _j; j++) {
121
122
  parent.push(schema[i].uid)
122
123
  parent.push(schema[i].blocks[j].uid)
123
124
  find(schema[i].blocks[j].schema, _entry)
@@ -135,14 +136,14 @@ module.exports = function (data, mappedUids, uidMapperPath) {
135
136
  }
136
137
 
137
138
  function findEntryIdsFromJsonRte(entry, ctSchema) {
138
- for (let i = 0; i < ctSchema.length; i++) {
139
- switch (ctSchema[i].data_type) {
139
+ for (const element of ctSchema) {
140
+ switch (element.data_type) {
140
141
  case 'blocks': {
141
- if (entry[ctSchema[i].uid]) {
142
- if (ctSchema[i].multiple) {
143
- entry[ctSchema[i].uid].forEach(e => {
142
+ if (entry[element.uid]) {
143
+ if (element.multiple) {
144
+ entry[element.uid].forEach(e => {
144
145
  let key = Object.keys(e).pop()
145
- let subBlock = ctSchema[i].blocks.filter(e => e.uid === key).pop()
146
+ let subBlock = element.blocks.filter(e => e.uid === key).pop()
146
147
  findEntryIdsFromJsonRte(e[key], subBlock.schema)
147
148
  })
148
149
  }
@@ -151,25 +152,25 @@ module.exports = function (data, mappedUids, uidMapperPath) {
151
152
  }
152
153
  case 'global_field':
153
154
  case 'group': {
154
- if (entry[ctSchema[i].uid]) {
155
- if (ctSchema[i].multiple) {
156
- entry[ctSchema[i].uid].forEach(e => {
157
- findEntryIdsFromJsonRte(e, ctSchema[i].schema)
155
+ if (entry[element.uid]) {
156
+ if (element.multiple) {
157
+ entry[element.uid].forEach(e => {
158
+ findEntryIdsFromJsonRte(e, element.schema)
158
159
  })
159
160
  } else {
160
- findEntryIdsFromJsonRte(entry[ctSchema[i].uid], ctSchema[i].schema)
161
+ findEntryIdsFromJsonRte(entry[element.uid], element.schema)
161
162
  }
162
163
  }
163
164
  break;
164
165
  }
165
166
  case 'json': {
166
- if (entry[ctSchema[i].uid] && ctSchema[i].field_metadata.rich_text_type) {
167
- if (ctSchema[i].multiple) {
168
- entry[ctSchema[i].uid].forEach(jsonRteData => {
167
+ if (entry[element.uid] && element.field_metadata.rich_text_type) {
168
+ if (element.multiple) {
169
+ entry[element.uid].forEach(jsonRteData => {
169
170
  gatherJsonRteEntryIds(jsonRteData)
170
171
  })
171
172
  } else {
172
- gatherJsonRteEntryIds(entry[ctSchema[i].uid])
173
+ gatherJsonRteEntryIds(entry[element.uid])
173
174
  }
174
175
  }
175
176
  break;
@@ -189,7 +190,7 @@ module.exports = function (data, mappedUids, uidMapperPath) {
189
190
  }
190
191
 
191
192
  uids = _.uniq(uids);
192
- var entry = JSON.stringify(data.entry);
193
+ let entry = JSON.stringify(data.entry);
193
194
  uids.forEach(function (uid) {
194
195
  if (mappedUids.hasOwnProperty(uid)) {
195
196
  entry = entry.replace(new RegExp(uid, 'img'), mappedUids[uid]);
@@ -200,7 +201,7 @@ module.exports = function (data, mappedUids, uidMapperPath) {
200
201
  });
201
202
 
202
203
  if (unmapped.length > 0) {
203
- var unmappedUids = helper.readFile(path.join(uidMapperPath, 'unmapped-uids.json'));
204
+ let unmappedUids = helper.readFile(path.join(uidMapperPath, 'unmapped-uids.json'));
204
205
  unmappedUids = unmappedUids || {};
205
206
  if (unmappedUids.hasOwnProperty(data.content_type.uid)) {
206
207
  unmappedUids[data.content_type.uid][data.entry.uid] = unmapped;
@@ -214,7 +215,7 @@ module.exports = function (data, mappedUids, uidMapperPath) {
214
215
  }
215
216
 
216
217
  if (mapped.length > 0) {
217
- var _mappedUids = helper.readFile(path.join(uidMapperPath, 'mapped-uids.json'));
218
+ let _mappedUids = helper.readFile(path.join(uidMapperPath, 'mapped-uids.json'));
218
219
  _mappedUids = _mappedUids || {};
219
220
  if (_mappedUids.hasOwnProperty(data.content_type.uid)) {
220
221
  _mappedUids[data.content_type.uid][data.entry.uid] = mapped;
@@ -239,7 +240,7 @@ function findUidsInNewRefFields(entry, uids) {
239
240
  findUidsInNewRefFields(elem, uids);
240
241
  });
241
242
  } else if (Object.keys(entry).length) {
242
- for (var key in entry) {
243
+ for (let key in entry) {
243
244
  if (key) {
244
245
  findUidsInNewRefFields(entry[key], uids);
245
246
  }
@@ -0,0 +1,38 @@
1
+ const sdk = require('./contentstack-management-sdk');
2
+ const { HttpClient } = require('@contentstack/cli-utilities');
3
+
4
+ const getInstalledExtensions = (config) => {
5
+ const client = sdk.Client(config)
6
+
7
+ return new Promise((resolve, reject) => {
8
+ const queryRequestOptions = {
9
+ include_marketplace_extensions: true
10
+ }
11
+ const { target_stack: api_key, management_token, auth_token } = config || {}
12
+
13
+ if (api_key && management_token) {
14
+ return client
15
+ .stack({ api_key, management_token })
16
+ .extension()
17
+ .query(queryRequestOptions)
18
+ .find()
19
+ .then(({ items }) => resolve(items))
20
+ .catch(reject)
21
+ } else if (api_key && auth_token) {
22
+ const headers = {
23
+ api_key,
24
+ authtoken: auth_token
25
+ }
26
+ const httpClient = new HttpClient().headers(headers);
27
+ const baseUrl = config.host.startsWith('http')
28
+ ? config.host
29
+ : `https://${config.host}/v3`;
30
+ httpClient.get(`${baseUrl}/extensions/?include_marketplace_extensions=true`)
31
+ .then(({ data: { extensions } }) => resolve(extensions))
32
+ } else {
33
+ resolve([])
34
+ }
35
+ })
36
+ }
37
+
38
+ module.exports = { getInstalledExtensions }