@contentstack/cli-cm-import 1.1.0 → 1.2.1

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.
@@ -9,11 +9,9 @@ var path = require('path');
9
9
  var mkdirp = require('mkdirp');
10
10
  var slice = Array.prototype.slice;
11
11
 
12
-
13
-
14
12
  const ansiRegexPattern = [
15
13
  '[\\u001B\\u009B][[\\]()#;?]*(?:(?:(?:(?:;[-a-zA-Z\\d\\/#&.:=?%@~_]+)*|[a-zA-Z\\d]+(?:;[-a-zA-Z\\d\\/#&.:=?%@~_]*)*)?\\u0007)',
16
- '(?:(?:\\d{1,4}(?:;\\d{0,4})*)?[\\dA-PR-TZcf-ntqry=><~]))'
14
+ '(?:(?:\\d{1,4}(?:;\\d{0,4})*)?[\\dA-PR-TZcf-ntqry=><~]))',
17
15
  ].join('|');
18
16
 
19
17
  function returnString(args) {
@@ -22,14 +20,18 @@ function returnString(args) {
22
20
  returnStr = args
23
21
  .map(function (item) {
24
22
  if (item && typeof item === 'object') {
25
- return JSON.stringify(item).replace(/authtoken\":\"blt................/g, 'authtoken":"blt....');
23
+ try {
24
+ return JSON.stringify(item).replace(/authtoken\":\"blt................/g, 'authtoken":"blt....');
25
+ } catch (error) {
26
+ return item.message;
27
+ }
26
28
  }
27
29
  return item;
28
30
  })
29
31
  .join(' ')
30
32
  .trim();
31
33
  }
32
- returnStr = returnStr.replace(new RegExp(ansiRegexPattern, 'g'), "").trim();
34
+ returnStr = returnStr.replace(new RegExp(ansiRegexPattern, 'g'), '').trim();
33
35
  return returnStr;
34
36
  }
35
37
 
@@ -38,7 +38,8 @@ module.exports = function (config) {
38
38
  client
39
39
  .stack({ api_key: config.target_stack, management_token: config.management_token })
40
40
  .fetch()
41
- .then(function () {
41
+ .then(function (stack) {
42
+ config.destinationStackName = stack.name
42
43
  return resolve();
43
44
  })
44
45
  .catch((error) => {
@@ -76,11 +76,15 @@ module.exports = function (data, mappedAssetUids, mappedAssetUrls, assetUidMappe
76
76
  findAssetIdsFromJsonCustomFields(data.entry, data.content_type.schema);
77
77
  } else if (schema[i].data_type === 'json' && schema[i].field_metadata.extension) {
78
78
  if (installedExtensions) {
79
- const marketplaceApps = helper.readFile(marketplaceAppPath);
79
+ const marketplaceApps = helper.readFileSync(marketplaceAppPath);
80
80
  const oldExt = _.find(marketplaceApps, { uid: schema[i].extension_uid });
81
81
 
82
82
  if (oldExt) {
83
- const ext = _.find(installedExtensions, { type: oldExt.type, title: oldExt.title, app_uid: oldExt.app_uid })
83
+ const ext = _.find(installedExtensions, {
84
+ type: oldExt.type,
85
+ title: oldExt.title,
86
+ app_uid: oldExt.app_uid,
87
+ });
84
88
 
85
89
  if (ext) {
86
90
  schema[i].extension_uid = ext.uid;
@@ -96,11 +100,15 @@ module.exports = function (data, mappedAssetUids, mappedAssetUrls, assetUidMappe
96
100
  if (row.data_type === 'json') {
97
101
  if (entryObj[row.uid] && row.field_metadata.extension && row.field_metadata.is_asset) {
98
102
  if (installedExtensions) {
99
- const marketplaceApps = helper.readFile(marketplaceAppPath);
103
+ const marketplaceApps = helper.readFileSync(marketplaceAppPath);
100
104
  const oldExt = _.find(marketplaceApps, { uid: row.extension_uid });
101
105
 
102
106
  if (oldExt) {
103
- const ext = _.find(installedExtensions, { type: oldExt.type, title: oldExt.title, app_uid: oldExt.app_uid })
107
+ const ext = _.find(installedExtensions, {
108
+ type: oldExt.type,
109
+ title: oldExt.title,
110
+ app_uid: oldExt.app_uid,
111
+ });
104
112
 
105
113
  if (ext) {
106
114
  row.extension_uid = ext.uid;
@@ -109,11 +117,15 @@ module.exports = function (data, mappedAssetUids, mappedAssetUrls, assetUidMappe
109
117
  }
110
118
 
111
119
  if (entryObj[row.uid].metadata && entryObj[row.uid].metadata.extension_uid) {
112
- const marketplaceApps = helper.readFile(marketplaceAppPath);
120
+ const marketplaceApps = helper.readFileSync(marketplaceAppPath);
113
121
  const oldExt = _.find(marketplaceApps, { uid: entryObj[row.uid].metadata.extension_uid });
114
122
 
115
123
  if (oldExt) {
116
- const ext = _.find(installedExtensions, { type: oldExt.type, title: oldExt.title, app_uid: oldExt.app_uid })
124
+ const ext = _.find(installedExtensions, {
125
+ type: oldExt.type,
126
+ title: oldExt.title,
127
+ app_uid: oldExt.app_uid,
128
+ });
117
129
 
118
130
  if (ext) {
119
131
  entryObj[row.uid].metadata.extension_uid = ext.uid;
@@ -238,7 +250,7 @@ module.exports = function (data, mappedAssetUids, mappedAssetUrls, assetUidMappe
238
250
  });
239
251
 
240
252
  if (matchedUids.length) {
241
- let matchedAssetUids = helper.readFile(path.join(assetUidMapperPath, 'matched-asset-uids.json'));
253
+ let matchedAssetUids = helper.readFileSync(path.join(assetUidMapperPath, 'matched-asset-uids.json'));
242
254
  matchedAssetUids = matchedAssetUids || {};
243
255
  if (matchedAssetUids.hasOwnProperty(data.content_type.uid)) {
244
256
  matchedAssetUids[data.content_type.uid][data.entry.uid] = matchedUids;
@@ -251,7 +263,7 @@ module.exports = function (data, mappedAssetUids, mappedAssetUrls, assetUidMappe
251
263
  }
252
264
 
253
265
  if (unmatchedUids.length) {
254
- let unmatchedAssetUids = helper.readFile(path.join(assetUidMapperPath, 'unmatched-asset-uids.json'));
266
+ let unmatchedAssetUids = helper.readFileSync(path.join(assetUidMapperPath, 'unmatched-asset-uids.json'));
255
267
  unmatchedAssetUids = unmatchedAssetUids || {};
256
268
  if (unmatchedAssetUids.hasOwnProperty(data.content_type.uid)) {
257
269
  unmatchedAssetUids[data.content_type.uid][data.entry.uid] = unmatchedUids;
@@ -264,7 +276,7 @@ module.exports = function (data, mappedAssetUids, mappedAssetUrls, assetUidMappe
264
276
  }
265
277
 
266
278
  if (unmatchedUrls.length) {
267
- let unmatchedAssetUrls = helper.readFile(path.join(assetUidMapperPath, 'unmatched-asset-urls.json'));
279
+ let unmatchedAssetUrls = helper.readFileSync(path.join(assetUidMapperPath, 'unmatched-asset-urls.json'));
268
280
  unmatchedAssetUrls = unmatchedAssetUrls || {};
269
281
  if (unmatchedAssetUrls.hasOwnProperty(data.content_type.uid)) {
270
282
  unmatchedAssetUrls[data.content_type.uid][data.entry.uid] = unmatchedUrls;
@@ -277,7 +289,7 @@ module.exports = function (data, mappedAssetUids, mappedAssetUrls, assetUidMappe
277
289
  }
278
290
 
279
291
  if (matchedUrls.length) {
280
- let matchedAssetUrls = helper.readFile(path.join(assetUidMapperPath, 'matched-asset-urls.json'));
292
+ let matchedAssetUrls = helper.readFileSync(path.join(assetUidMapperPath, 'matched-asset-urls.json'));
281
293
  matchedAssetUrls = matchedAssetUrls || {};
282
294
  if (matchedAssetUrls.hasOwnProperty(data.content_type.uid)) {
283
295
  matchedAssetUrls[data.content_type.uid][data.entry.uid] = matchedUrls;
@@ -22,35 +22,35 @@ module.exports = function (data, mappedUids, uidMapperPath) {
22
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
51
  const update = function (_parent, form_id, updateEntry) {
52
- let _entry = updateEntry
53
- let len = _parent.length
52
+ let _entry = updateEntry;
53
+ let len = _parent.length;
54
54
 
55
55
  for (let j = 0; j < len; j++) {
56
56
  if (_entry && _parent[j]) {
@@ -82,7 +82,7 @@ module.exports = function (data, mappedUids, uidMapperPath) {
82
82
  }
83
83
  } else {
84
84
  _entry = _entry[_parent[j]];
85
- let _keys = _.clone(_parent).splice((j+1), len);
85
+ let _keys = _.clone(_parent).splice(j + 1, len);
86
86
  if (Array.isArray(_entry)) {
87
87
  for (let i = 0, _i = _entry.length; i < _i; i++) {
88
88
  update(_keys, form_id, _entry[i]);
@@ -97,43 +97,43 @@ module.exports = function (data, mappedUids, uidMapperPath) {
97
97
  const find = function (schema, _entry) {
98
98
  for (let i = 0, _i = schema.length; i < _i; i++) {
99
99
  switch (schema[i].data_type) {
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
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;
134
134
  }
135
135
  }
136
- }
136
+ };
137
137
 
138
138
  function findEntryIdsFromJsonRte(entry, ctSchema) {
139
139
  for (const element of ctSchema) {
@@ -141,11 +141,11 @@ module.exports = function (data, mappedUids, uidMapperPath) {
141
141
  case 'blocks': {
142
142
  if (entry[element.uid]) {
143
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
- })
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
+ });
149
149
  }
150
150
  }
151
151
  break;
@@ -154,11 +154,11 @@ module.exports = function (data, mappedUids, uidMapperPath) {
154
154
  case 'group': {
155
155
  if (entry[element.uid]) {
156
156
  if (element.multiple) {
157
- entry[element.uid].forEach(e => {
158
- findEntryIdsFromJsonRte(e, element.schema)
159
- })
157
+ entry[element.uid].forEach((e) => {
158
+ findEntryIdsFromJsonRte(e, element.schema);
159
+ });
160
160
  } else {
161
- findEntryIdsFromJsonRte(entry[element.uid], element.schema)
161
+ findEntryIdsFromJsonRte(entry[element.uid], element.schema);
162
162
  }
163
163
  }
164
164
  break;
@@ -166,11 +166,11 @@ module.exports = function (data, mappedUids, uidMapperPath) {
166
166
  case 'json': {
167
167
  if (entry[element.uid] && element.field_metadata.rich_text_type) {
168
168
  if (element.multiple) {
169
- entry[element.uid].forEach(jsonRteData => {
170
- gatherJsonRteEntryIds(jsonRteData)
171
- })
169
+ entry[element.uid].forEach((jsonRteData) => {
170
+ gatherJsonRteEntryIds(jsonRteData);
171
+ });
172
172
  } else {
173
- gatherJsonRteEntryIds(entry[element.uid])
173
+ gatherJsonRteEntryIds(entry[element.uid]);
174
174
  }
175
175
  }
176
176
  break;
@@ -179,7 +179,7 @@ module.exports = function (data, mappedUids, uidMapperPath) {
179
179
  }
180
180
  }
181
181
 
182
- find(data.content_type.schema, data.entry)
182
+ find(data.content_type.schema, data.entry);
183
183
  if (isNewRefFields) {
184
184
  findUidsInNewRefFields(data.entry, uids);
185
185
  }
@@ -201,7 +201,7 @@ module.exports = function (data, mappedUids, uidMapperPath) {
201
201
  });
202
202
 
203
203
  if (unmapped.length > 0) {
204
- let unmappedUids = helper.readFile(path.join(uidMapperPath, 'unmapped-uids.json'));
204
+ let unmappedUids = helper.readFileSync(path.join(uidMapperPath, 'unmapped-uids.json'));
205
205
  unmappedUids = unmappedUids || {};
206
206
  if (unmappedUids.hasOwnProperty(data.content_type.uid)) {
207
207
  unmappedUids[data.content_type.uid][data.entry.uid] = unmapped;
@@ -215,7 +215,7 @@ module.exports = function (data, mappedUids, uidMapperPath) {
215
215
  }
216
216
 
217
217
  if (mapped.length > 0) {
218
- let _mappedUids = helper.readFile(path.join(uidMapperPath, 'mapped-uids.json'));
218
+ let _mappedUids = helper.readFileSync(path.join(uidMapperPath, 'mapped-uids.json'));
219
219
  _mappedUids = _mappedUids || {};
220
220
  if (_mappedUids.hasOwnProperty(data.content_type.uid)) {
221
221
  _mappedUids[data.content_type.uid][data.entry.uid] = mapped;
@@ -1,5 +1,6 @@
1
+ let config = require('../../config/default');
1
2
  const sdk = require('./contentstack-management-sdk');
2
- const { HttpClient } = require('@contentstack/cli-utilities');
3
+ const { cliux, HttpClient, configHandler } = require('@contentstack/cli-utilities');
3
4
 
4
5
  const getInstalledExtensions = (config) => {
5
6
  const client = sdk.Client(config)
@@ -19,15 +20,13 @@ const getInstalledExtensions = (config) => {
19
20
  .then(({ items }) => resolve(items))
20
21
  .catch(reject)
21
22
  } else if (api_key && auth_token) {
23
+ const { cma } = configHandler.get('region') || {};
22
24
  const headers = {
23
25
  api_key,
24
26
  authtoken: auth_token
25
27
  }
26
28
  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`)
29
+ httpClient.get(`${cma}/v3/extensions/?include_marketplace_extensions=true`)
31
30
  .then(({ data: { extensions } }) => resolve(extensions))
32
31
  } else {
33
32
  resolve([])
@@ -35,4 +34,24 @@ const getInstalledExtensions = (config) => {
35
34
  })
36
35
  }
37
36
 
38
- module.exports = { getInstalledExtensions }
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 }