@contentstack/cli-cm-import 1.0.1 → 1.2.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,77 @@ 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.readFileSync(marketplaceAppPath);
80
+ const oldExt = _.find(marketplaceApps, { uid: schema[i].extension_uid });
81
+
82
+ if (oldExt) {
83
+ const ext = _.find(installedExtensions, {
84
+ type: oldExt.type,
85
+ title: oldExt.title,
86
+ app_uid: oldExt.app_uid,
87
+ });
88
+
89
+ if (ext) {
90
+ schema[i].extension_uid = ext.uid;
91
+ }
92
+ }
93
+ }
68
94
  }
69
95
  }
70
96
  };
71
97
 
98
+ function findAssetIdsFromJsonCustomFields(entryObj, ctSchema) {
99
+ ctSchema.map((row) => {
100
+ if (row.data_type === 'json') {
101
+ if (entryObj[row.uid] && row.field_metadata.extension && row.field_metadata.is_asset) {
102
+ if (installedExtensions) {
103
+ const marketplaceApps = helper.readFileSync(marketplaceAppPath);
104
+ const oldExt = _.find(marketplaceApps, { uid: row.extension_uid });
105
+
106
+ if (oldExt) {
107
+ const ext = _.find(installedExtensions, {
108
+ type: oldExt.type,
109
+ title: oldExt.title,
110
+ app_uid: oldExt.app_uid,
111
+ });
112
+
113
+ if (ext) {
114
+ row.extension_uid = ext.uid;
115
+ }
116
+ }
117
+ }
118
+
119
+ if (entryObj[row.uid].metadata && entryObj[row.uid].metadata.extension_uid) {
120
+ const marketplaceApps = helper.readFileSync(marketplaceAppPath);
121
+ const oldExt = _.find(marketplaceApps, { uid: entryObj[row.uid].metadata.extension_uid });
122
+
123
+ if (oldExt) {
124
+ const ext = _.find(installedExtensions, {
125
+ type: oldExt.type,
126
+ title: oldExt.title,
127
+ app_uid: oldExt.app_uid,
128
+ });
129
+
130
+ if (ext) {
131
+ entryObj[row.uid].metadata.extension_uid = ext.uid;
132
+ }
133
+ }
134
+ }
135
+ }
136
+ }
137
+
138
+ return row;
139
+ });
140
+ }
141
+
72
142
  function findAssetIdsFromJsonRte(entryObj, ctSchema) {
73
143
  for (const element of ctSchema) {
74
144
  switch (element.data_type) {
@@ -154,7 +224,7 @@ module.exports = function (data, mappedAssetUids, mappedAssetUrls, assetUidMappe
154
224
  }
155
225
 
156
226
  find(data.content_type.schema, data.entry);
157
- updateFileFields(data.entry, data, null, mappedAssetUids, matchedUids, unmatchedUids);
227
+ updateFileFields(data.entry, data, null, mappedAssetUids, matchedUids, unmatchedUids, mappedAssetUrls);
158
228
  assetUids = _.uniq(assetUids);
159
229
  assetUrls = _.uniq(assetUrls);
160
230
  let entry = JSON.stringify(data.entry);
@@ -180,7 +250,7 @@ module.exports = function (data, mappedAssetUids, mappedAssetUrls, assetUidMappe
180
250
  });
181
251
 
182
252
  if (matchedUids.length) {
183
- let matchedAssetUids = helper.readFile(path.join(assetUidMapperPath, 'matched-asset-uids.json'));
253
+ let matchedAssetUids = helper.readFileSync(path.join(assetUidMapperPath, 'matched-asset-uids.json'));
184
254
  matchedAssetUids = matchedAssetUids || {};
185
255
  if (matchedAssetUids.hasOwnProperty(data.content_type.uid)) {
186
256
  matchedAssetUids[data.content_type.uid][data.entry.uid] = matchedUids;
@@ -193,7 +263,7 @@ module.exports = function (data, mappedAssetUids, mappedAssetUrls, assetUidMappe
193
263
  }
194
264
 
195
265
  if (unmatchedUids.length) {
196
- let unmatchedAssetUids = helper.readFile(path.join(assetUidMapperPath, 'unmatched-asset-uids.json'));
266
+ let unmatchedAssetUids = helper.readFileSync(path.join(assetUidMapperPath, 'unmatched-asset-uids.json'));
197
267
  unmatchedAssetUids = unmatchedAssetUids || {};
198
268
  if (unmatchedAssetUids.hasOwnProperty(data.content_type.uid)) {
199
269
  unmatchedAssetUids[data.content_type.uid][data.entry.uid] = unmatchedUids;
@@ -206,7 +276,7 @@ module.exports = function (data, mappedAssetUids, mappedAssetUrls, assetUidMappe
206
276
  }
207
277
 
208
278
  if (unmatchedUrls.length) {
209
- let unmatchedAssetUrls = helper.readFile(path.join(assetUidMapperPath, 'unmatched-asset-urls.json'));
279
+ let unmatchedAssetUrls = helper.readFileSync(path.join(assetUidMapperPath, 'unmatched-asset-urls.json'));
210
280
  unmatchedAssetUrls = unmatchedAssetUrls || {};
211
281
  if (unmatchedAssetUrls.hasOwnProperty(data.content_type.uid)) {
212
282
  unmatchedAssetUrls[data.content_type.uid][data.entry.uid] = unmatchedUrls;
@@ -219,7 +289,7 @@ module.exports = function (data, mappedAssetUids, mappedAssetUrls, assetUidMappe
219
289
  }
220
290
 
221
291
  if (matchedUrls.length) {
222
- let matchedAssetUrls = helper.readFile(path.join(assetUidMapperPath, 'matched-asset-urls.json'));
292
+ let matchedAssetUrls = helper.readFileSync(path.join(assetUidMapperPath, 'matched-asset-urls.json'));
223
293
  matchedAssetUrls = matchedAssetUrls || {};
224
294
  if (matchedAssetUrls.hasOwnProperty(data.content_type.uid)) {
225
295
  matchedAssetUrls[data.content_type.uid][data.entry.uid] = matchedUrls;
@@ -277,16 +347,49 @@ function findFileUrls(schema, _entry, assetUrls) {
277
347
  }
278
348
  }
279
349
 
280
- function updateFileFields(objekt, parent, pos, mappedAssetUids, matchedUids, unmatchedUids) {
350
+ function updateFileFields(objekt, parent, pos, mappedAssetUids, matchedUids, unmatchedUids, mappedAssetUrls) {
281
351
  if (_.isPlainObject(objekt) && _.has(objekt, 'filename') && _.has(objekt, 'uid')) {
282
352
  if (typeof pos !== 'undefined') {
283
353
  if (typeof pos === 'number' || typeof pos === 'string') {
284
- if (mappedAssetUids.hasOwnProperty(objekt.uid)) {
285
- parent[pos] = mappedAssetUids[objekt.uid];
286
- matchedUids.push(objekt.uid);
354
+ const replacer = () => {
355
+ if (mappedAssetUids.hasOwnProperty(objekt.uid)) {
356
+ parent[pos] = mappedAssetUids[objekt.uid];
357
+ matchedUids.push(objekt.uid);
358
+ } else {
359
+ parent[pos] = '';
360
+ unmatchedUids.push(objekt.uid);
361
+ }
362
+ };
363
+
364
+ if (parent.uid && mappedAssetUids[parent.uid]) {
365
+ parent.uid = mappedAssetUids[parent.uid];
366
+ }
367
+
368
+ if (
369
+ objekt &&
370
+ _.isObject(parent[pos]) &&
371
+ parent[pos].uid &&
372
+ parent[pos].url &&
373
+ _.has(parent, 'asset') &&
374
+ _.has(parent, '_content_type_uid') &&
375
+ parent._content_type_uid === 'sys_assets'
376
+ ) {
377
+ if (
378
+ _.has(parent, 'asset') &&
379
+ _.has(parent, '_content_type_uid') &&
380
+ parent._content_type_uid === 'sys_assets'
381
+ ) {
382
+ parent = _.omit(parent, ['asset']);
383
+ }
384
+
385
+ if (objekt.uid && mappedAssetUids && mappedAssetUids[objekt.uid]) {
386
+ objekt.uid = mappedAssetUids[objekt.uid];
387
+ }
388
+ if (objekt.url && mappedAssetUrls && mappedAssetUrls[objekt.url]) {
389
+ objekt.url = mappedAssetUrls[objekt.url];
390
+ }
287
391
  } else {
288
- parent[pos] = '';
289
- unmatchedUids.push(objekt.uid);
392
+ replacer();
290
393
  }
291
394
  }
292
395
  }
@@ -5,53 +5,54 @@
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
- jsonRteData.children.forEach(element => {
25
+ jsonRteData.children.forEach((element) => {
26
26
  if (element.type) {
27
27
  switch (element.type) {
28
28
  case 'a':
29
29
  case 'p': {
30
30
  if (element.children && element.children.length > 0) {
31
- gatherJsonRteEntryIds(element)
31
+ gatherJsonRteEntryIds(element);
32
32
  }
33
33
  break;
34
34
  }
35
35
  case 'reference': {
36
- if (Object.keys(element.attrs).length > 0 && element.attrs.type === "entry") {
36
+ if (Object.keys(element.attrs).length > 0 && element.attrs.type === 'entry') {
37
37
  if (uids.indexOf(element.attrs['entry-uid']) === -1) {
38
- uids.push(element.attrs['entry-uid'])
38
+ uids.push(element.attrs['entry-uid']);
39
39
  }
40
40
  }
41
41
  if (element.children && element.children.length > 0) {
42
- gatherJsonRteEntryIds(element)
42
+ gatherJsonRteEntryIds(element);
43
43
  }
44
44
  break;
45
45
  }
46
46
  }
47
47
  }
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,83 +94,83 @@ 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
- case 'reference':
100
- if (Array.isArray(schema[i].reference_to)) {
101
- isNewRefFields = true
102
- schema[i].reference_to.forEach(reference => {
103
- parent.push(schema[i].uid)
104
- update(parent, reference, _entry)
105
- parent.pop()
106
- })
107
- } else {
108
- parent.push(schema[i].uid)
109
- update(parent, schema[i].reference_to, _entry)
110
- parent.pop()
111
- }
112
- break
113
- case 'global_field':
114
- case 'group':
115
- parent.push(schema[i].uid)
116
- find(schema[i].schema, _entry)
117
- parent.pop()
118
- break
119
- case 'blocks':
120
- for (var j = 0, _j = schema[i].blocks.length; j < _j; j++) {
121
- parent.push(schema[i].uid)
122
- parent.push(schema[i].blocks[j].uid)
123
- find(schema[i].blocks[j].schema, _entry)
124
- parent.pop()
125
- parent.pop()
126
- }
127
- break
128
- case 'json':
129
- if (schema[i].field_metadata.rich_text_type) {
130
- findEntryIdsFromJsonRte(data.entry, data.content_type.schema)
131
- }
132
- break
100
+ case 'reference':
101
+ if (Array.isArray(schema[i].reference_to)) {
102
+ isNewRefFields = true;
103
+ schema[i].reference_to.forEach((reference) => {
104
+ parent.push(schema[i].uid);
105
+ update(parent, reference, _entry);
106
+ parent.pop();
107
+ });
108
+ } else {
109
+ parent.push(schema[i].uid);
110
+ update(parent, schema[i].reference_to, _entry);
111
+ parent.pop();
112
+ }
113
+ break;
114
+ case 'global_field':
115
+ case 'group':
116
+ parent.push(schema[i].uid);
117
+ find(schema[i].schema, _entry);
118
+ parent.pop();
119
+ break;
120
+ case 'blocks':
121
+ for (let j = 0, _j = schema[i].blocks.length; j < _j; j++) {
122
+ parent.push(schema[i].uid);
123
+ parent.push(schema[i].blocks[j].uid);
124
+ find(schema[i].blocks[j].schema, _entry);
125
+ parent.pop();
126
+ parent.pop();
127
+ }
128
+ break;
129
+ case 'json':
130
+ if (schema[i].field_metadata.rich_text_type) {
131
+ findEntryIdsFromJsonRte(data.entry, data.content_type.schema);
132
+ }
133
+ break;
133
134
  }
134
135
  }
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 => {
144
- let key = Object.keys(e).pop()
145
- let subBlock = ctSchema[i].blocks.filter(e => e.uid === key).pop()
146
- findEntryIdsFromJsonRte(e[key], subBlock.schema)
147
- })
142
+ if (entry[element.uid]) {
143
+ if (element.multiple) {
144
+ entry[element.uid].forEach((e) => {
145
+ let key = Object.keys(e).pop();
146
+ let subBlock = element.blocks.filter((e) => e.uid === key).pop();
147
+ findEntryIdsFromJsonRte(e[key], subBlock.schema);
148
+ });
148
149
  }
149
150
  }
150
151
  break;
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)
158
- })
155
+ if (entry[element.uid]) {
156
+ if (element.multiple) {
157
+ entry[element.uid].forEach((e) => {
158
+ findEntryIdsFromJsonRte(e, element.schema);
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 => {
169
- gatherJsonRteEntryIds(jsonRteData)
170
- })
167
+ if (entry[element.uid] && element.field_metadata.rich_text_type) {
168
+ if (element.multiple) {
169
+ entry[element.uid].forEach((jsonRteData) => {
170
+ gatherJsonRteEntryIds(jsonRteData);
171
+ });
171
172
  } else {
172
- gatherJsonRteEntryIds(entry[ctSchema[i].uid])
173
+ gatherJsonRteEntryIds(entry[element.uid]);
173
174
  }
174
175
  }
175
176
  break;
@@ -178,7 +179,7 @@ module.exports = function (data, mappedUids, uidMapperPath) {
178
179
  }
179
180
  }
180
181
 
181
- find(data.content_type.schema, data.entry)
182
+ find(data.content_type.schema, data.entry);
182
183
  if (isNewRefFields) {
183
184
  findUidsInNewRefFields(data.entry, uids);
184
185
  }
@@ -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.readFileSync(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.readFileSync(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,57 @@
1
+ let config = require('../../config/default');
2
+ const sdk = require('./contentstack-management-sdk');
3
+ const { cliux, HttpClient, configHandler } = require('@contentstack/cli-utilities');
4
+
5
+ const getInstalledExtensions = (config) => {
6
+ const client = sdk.Client(config)
7
+
8
+ return new Promise((resolve, reject) => {
9
+ const queryRequestOptions = {
10
+ include_marketplace_extensions: true
11
+ }
12
+ const { target_stack: api_key, management_token, auth_token } = config || {}
13
+
14
+ if (api_key && management_token) {
15
+ return client
16
+ .stack({ api_key, management_token })
17
+ .extension()
18
+ .query(queryRequestOptions)
19
+ .find()
20
+ .then(({ items }) => resolve(items))
21
+ .catch(reject)
22
+ } else if (api_key && auth_token) {
23
+ const { cma } = configHandler.get('region') || {};
24
+ const headers = {
25
+ api_key,
26
+ authtoken: auth_token
27
+ }
28
+ const httpClient = new HttpClient().headers(headers);
29
+ httpClient.get(`${cma}/v3/extensions/?include_marketplace_extensions=true`)
30
+ .then(({ data: { extensions } }) => resolve(extensions))
31
+ } else {
32
+ resolve([])
33
+ }
34
+ })
35
+ }
36
+
37
+ const getDeveloperHubUrl = async () => {
38
+ const { cma, name } = configHandler.get('region') || {};
39
+ let developerHubBaseUrl = config.developerHubUrls[cma];
40
+
41
+ if (!developerHubBaseUrl) {
42
+ developerHubBaseUrl = await cliux.inquire({
43
+ type: 'input',
44
+ name: 'name',
45
+ validate: (url) => {
46
+ if (!url) return "Developer-hub URL can't be empty.";
47
+
48
+ return true;
49
+ },
50
+ message: `Enter the developer-hub base URL for the ${name} region - `,
51
+ });
52
+ }
53
+
54
+ return developerHubBaseUrl.startsWith('http') ? developerHubBaseUrl : `https://${developerHubBaseUrl}`;
55
+ }
56
+
57
+ module.exports = { getInstalledExtensions, getDeveloperHubUrl }